diff options
author | Jeongmo Yang <jm80.yang@samsung.com> | 2012-09-07 21:59:49 +0900 |
---|---|---|
committer | Jeongmo Yang <jm80.yang@samsung.com> | 2012-09-07 22:00:07 +0900 |
commit | 8665a5303d5fa3daaba53cff8f0758e0fc859636 (patch) | |
tree | 1f047a01969b70f244d6c0b4381a34e5b7b64355 /camerasrc/src | |
parent | 4e9e23ca3164c203a46c205da5af49e354dbbdd2 (diff) | |
download | gst-plugins-s5pc2xx-master.tar.gz gst-plugins-s5pc2xx-master.tar.bz2 gst-plugins-s5pc2xx-master.zip |
Initial releaseHEADsubmit/master/20120920.1509352.0_alphamaster2.0alpha
Change-Id: I0736f126be88ae94efb6698b91de356c16357e52
Diffstat (limited to 'camerasrc/src')
-rw-r--r-- | camerasrc/src/Makefile.am | 30 | ||||
-rw-r--r-- | camerasrc/src/camerasrc-internal.c | 1463 | ||||
-rw-r--r-- | camerasrc/src/camerasrc.c | 4870 | ||||
-rw-r--r-- | camerasrc/src/gstcamerasrc.c | 3894 | ||||
-rw-r--r-- | camerasrc/src/gstcamerasrccolorbalance.c | 117 | ||||
-rw-r--r-- | camerasrc/src/gstcamerasrccontrol.c | 1098 | ||||
-rw-r--r-- | camerasrc/src/include/camerasrc-common.h | 408 | ||||
-rw-r--r-- | camerasrc/src/include/camerasrc-error.h | 140 | ||||
-rw-r--r-- | camerasrc/src/include/camerasrc-internal.h | 247 | ||||
-rw-r--r-- | camerasrc/src/include/camerasrc.h | 1488 | ||||
-rw-r--r-- | camerasrc/src/include/gstcamerasrc.h | 175 | ||||
-rw-r--r-- | camerasrc/src/include/gstcamerasrccolorbalance.h | 93 | ||||
-rw-r--r-- | camerasrc/src/include/gstcamerasrccontrol.h | 384 |
13 files changed, 14407 insertions, 0 deletions
diff --git a/camerasrc/src/Makefile.am b/camerasrc/src/Makefile.am new file mode 100644 index 0000000..ecec7e7 --- /dev/null +++ b/camerasrc/src/Makefile.am @@ -0,0 +1,30 @@ +# plugindir is set in configure + +plugin_LTLIBRARIES = libgstcamerasrc.la + +# sources used to compile this plug-in +libgstcamerasrc_la_SOURCES = gstcamerasrccontrol.c \ + gstcamerasrccolorbalance.c \ + camerasrc.c \ + camerasrc-internal.c \ + gstcamerasrc.c + +libgstcamerasrc_la_CFLAGS = $(GST_CFLAGS) \ + $(GST_BASE_CFLAGS) \ + $(GST_VIDEO_FLAGS) \ + $(MMTA_CFLAGS) \ + -I$(srcdir)/include \ + $(GST_INTERFACES_CFLAGS) + + +libgstcamerasrc_la_LIBADD = $(GST_LIBS) \ + $(GST_BASE_LIBS) \ + $(GST_VIDEO_LIBS) \ + $(MMTA_LIBS) \ + -ldl -lpthread -lrt -lm \ + $(GST_INTERFACES_LIBS) + +libgstcamerasrc_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) + +libgstcamerasrc_la_CPPFLAGS = -I$(srcdir)/include + diff --git a/camerasrc/src/camerasrc-internal.c b/camerasrc/src/camerasrc-internal.c new file mode 100644 index 0000000..e820bd9 --- /dev/null +++ b/camerasrc/src/camerasrc-internal.c @@ -0,0 +1,1463 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include <math.h> +#include "camerasrc-internal.h" + +/*#define USE_IOCTL_DEBUG*/ +#if defined (USE_IOCTL_DEBUG) +static char* get_request_name(int request, char* res_str) { + switch (request) { + case VIDIOC_QBUF: + sprintf(res_str, "[VIDIOC_QBUF]"); + break; + case VIDIOC_DQBUF: + sprintf(res_str, "[VIDIOC_DQBUF]"); + break; + case VIDIOC_S_INPUT: + sprintf(res_str, "[VIDIOC_S_INPUT]"); + break; + case VIDIOC_G_INPUT: + sprintf(res_str, "[VIDIOC_G_INPUT]"); + break; + case VIDIOC_S_PARM: + sprintf(res_str, "[VIDIOC_S_PARM]"); + break; + case VIDIOC_G_PARM: + sprintf(res_str, "[VIDIOC_G_PARM]"); + break; + case VIDIOC_S_FMT: + sprintf(res_str, "[VIDIOC_S_FMT]"); + break; + case VIDIOC_G_FMT: + sprintf(res_str, "[VIDIOC_G_FMT]"); + break; + case VIDIOC_REQBUFS: + sprintf(res_str, "[VIDIOC_REQBUFS]"); + break; + case VIDIOC_QUERYBUF: + sprintf(res_str, "[VIDIOC_QUERYBUF]"); + break; + case VIDIOC_STREAMON: + sprintf(res_str, "[VIDIOC_STREAMON]"); + break; + case VIDIOC_STREAMOFF: + sprintf(res_str, "[VIDIOC_STREAMOFF]"); + break; + case VIDIOC_S_CTRL: + sprintf(res_str, "[VIDIOC_S_CTRL] "); + break; + case VIDIOC_G_CTRL: + sprintf(res_str, "[VIDIOC_G_CTRL]"); + break; + case VIDIOC_ENUMINPUT: + sprintf(res_str, "[VIDIOC_ENUMINPUT]"); + break; + case VIDIOC_S_JPEGCOMP: + sprintf(res_str, "[VIDIOC_S_JPEGCOMP]"); + break; + case VIDIOC_G_JPEGCOMP: + sprintf(res_str, "[VIDIOC_G_JPEGCOMP]"); + break; + /* Extension */ + case VIDIOC_S_STROBE: + sprintf(res_str, "[VIDIOC_S_STROBE]"); + break; + case VIDIOC_G_STROBE: + sprintf(res_str, "[VIDIOC_G_STROBE]"); + break; + case VIDIOC_S_RECOGNITION: + sprintf(res_str, "[VIDIOC_S_RECOGNITION]"); + break; + case VIDIOC_G_RECOGNITION: + sprintf(res_str, "[VIDIOC_G_RECOGNITION]"); + break; + case VIDIOC_G_EXIF: + sprintf(res_str, "[VIDIOC_G_EXIF]"); + break; + default: + sprintf(res_str, "[UNKNOWN IOCTL(%x)]", request); + break; + } + + return 0; +} + +#define PRINT_IOCTL_INFO(request, arg) {\ + char res_str[255];\ + get_request_name(request, res_str);\ + camsrc_info("[request : %s, argument address : %x]", res_str, arg);\ +} +#else + +#define PRINT_IOCTL_INFO(request, arg) + +#endif + + +#define LOCK(x) {\ + if(0 != pthread_mutex_lock(&(x->mutex))) {\ + camsrc_error("Mutex lock error");\ + camsrc_assert(0);\ + }\ +} + +#define UNLOCK(x) {\ + if(0 != pthread_mutex_unlock(&(x->mutex))) {\ + camsrc_error("Mutex unlock error");\ + camsrc_assert(0);\ + }\ +} + +#define AF_MUT_LOCK(p) {\ + if(0 != pthread_mutex_lock(&(p->af_mutex))) {\ + camsrc_error("AF Mutex locking error");\ + camsrc_assert(0);\ + }\ +} + +#define AF_MUT_UNLOCK(p) {\ + if(0 != pthread_mutex_unlock(&(p->af_mutex))) {\ + camsrc_error("AF Mutex unlocking error");\ + camsrc_assert(0);\ + }\ +} + +#define SET_CTRL_VAL(cid, in_value) {\ + int err = CAMERASRC_ERR_UNKNOWN;\ + struct v4l2_control control;\ + control.id = cid;\ + control.value = in_value;\ + camsrc_info("[VIDIOC_S_CTRL] >> [%x] request with value %d", cid, in_value); \ + err = _camerasrc_ioctl(handle, VIDIOC_S_CTRL, &control);\ + if(err != CAMERASRC_SUCCESS) {\ + return err;\ + }\ +} + +#define GET_CTRL_VAL(cid, ret_value) {\ + int err = CAMERASRC_ERR_UNKNOWN;\ + struct v4l2_control control;\ + control.id = cid;\ + err = _camerasrc_ioctl(handle, VIDIOC_G_CTRL, &control);\ + if(err != CAMERASRC_SUCCESS) {\ + return err;\ + }\ + ret_value = control.value;\ + camsrc_info("[VIDIOC_G_CTRL] << [%x] request with value %d", cid, ret_value); \ +} + +#define SET_CTRL_VAL_ERR(cid, in_value, err) {\ + struct v4l2_control control;\ + control.id = cid;\ + control.value = in_value;\ + camsrc_info("[VIDIOC_S_CTRL] >> [%x] request with value %d", cid, in_value); \ + _camerasrc_ioctl_with_err(handle, VIDIOC_S_CTRL, &control, &err);\ +} + +#define GET_CTRL_VAL_ERR(cid, ret_value, err) {\ + struct v4l2_control control;\ + control.id = cid;\ + _camerasrc_ioctl_with_err(handle, VIDIOC_G_CTRL, &control, &err);\ + ret_value = control.value;\ + camsrc_info("[VIDIOC_G_CTRL] << [%x] request with value %d", cid, ret_value); \ +} + +/* AF function */ +static int _camerasrc_start_autofocusing(camerasrc_handle_t *handle); +static int _camerasrc_stop_autofocusing(camerasrc_handle_t *handle); +static int _camerasrc_destroy_autofocusing(camerasrc_handle_t *handle); +static int _camerasrc_release_autofocusing(camerasrc_handle_t *handle); + + +int static af_retry_cnt = 0; + +static int _camerasrc_ioctl(camerasrc_handle_t *handle, int request, void *arg) +{ + int fd = handle->dev_fd; + int err; + int nAgain = 10; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + LOCK(handle); + + if (handle->check_esd == 1) { + camsrc_warning("ESD shock occured. All device control will success"); + UNLOCK(handle); + return CAMERASRC_SUCCESS; + } + + if (handle->dev_fd == CAMERASRC_DEV_FD_EMERGENCY_CLOSED) { + camsrc_warning("Device emergency closed. All device control will success"); + UNLOCK(handle); + return CAMERASRC_SUCCESS; + } + + PRINT_IOCTL_INFO(request, arg); + +again: + do { + err = ioctl (fd, request, arg); + } while (-1 == err && EINTR == errno); + + if (err != 0) { + handle->errnum = errno; + err = errno; + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("ioctl[%x] err : %s", request, err_msg); + if (err == EEXIST) { + camsrc_info("EEXIST occured, but can go."); + err = 0; + } else if (err == ENOENT) { + camsrc_info("ENOENT occured, but can go."); + err = 0; +#if defined (ENABLE_Q_ERROR) +#warning "ENABLE_Q_ERROR enabled" + } else if (request == VIDIOC_DQBUF) { + goto DQ_ERROR; + } else if (request == VIDIOC_QBUF) { + goto ENQ_ERROR; +#endif + } else if (err == EINVAL) { + camsrc_error("EINVAL occured, Shutdown"); + UNLOCK(handle); + return CAMERASRC_ERR_INVALID_PARAMETER; + } else if (err == EBUSY) { + camsrc_error("EBUSY occured, Shutdown"); + UNLOCK(handle); + return CAMERASRC_ERR_PRIVILEGE; + } else if (err == EAGAIN && nAgain--) { + goto again; + } else { + /* Why does this return SUCCESS? */ + camsrc_error("Unhandled exception occured on IOCTL"); + } + } + + UNLOCK(handle); + + return CAMERASRC_SUCCESS; + +#if defined (ENABLE_Q_ERROR) +DQ_ERROR: + camsrc_error("DQ Frame error occured"); + printf("DQ Frame error occured"); + UNLOCK(handle); + return CAMERASRC_ERR_INTERNAL; + +ENQ_ERROR: + camsrc_error("Q Frame error occured"); + printf("Q Frame error occured"); + UNLOCK(handle); + return CAMERASRC_ERR_INTERNAL; +#endif +} + + +static int _camerasrc_ioctl_with_err(camerasrc_handle_t *handle, int request, void *arg, int *error) +{ + int fd = handle->dev_fd; + int err; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + LOCK(handle); + + *error = 0; + + if (handle->check_esd == 1) { + camsrc_warning("ESD shock occured. All device control will success"); + UNLOCK(handle); + return CAMERASRC_SUCCESS; + } + + if (handle->dev_fd == CAMERASRC_DEV_FD_EMERGENCY_CLOSED) { + camsrc_warning("Device emergency closed. All device control will success"); + UNLOCK(handle); + return CAMERASRC_SUCCESS; + } + + PRINT_IOCTL_INFO(request, arg); + + do { + err = ioctl (fd, request, arg); + } while (-1 == err && EINTR == errno); + + if (err != 0) { + handle->errnum = errno; + *error = errno; + strerror_r(*error, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("ioctl[%x] err : %s", request, err_msg); + UNLOCK(handle); + return CAMERASRC_ERR_IO_CONTROL; + } + + UNLOCK(handle); + + return CAMERASRC_SUCCESS; +} + + +static int _camerasrc_ioctl_once(camerasrc_handle_t *handle, int request, void *arg) +{ + int fd = handle->dev_fd; + int err = -1; + int nAgain = 10; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + LOCK(handle); + + if (handle->check_esd == 1) { + camsrc_warning("ESD shock occured. All device control will success"); + UNLOCK(handle); + return CAMERASRC_SUCCESS; + } + + if (handle->dev_fd == CAMERASRC_DEV_FD_EMERGENCY_CLOSED) { + camsrc_warning("Device emergency closed. All device control will success"); + UNLOCK(handle); + return CAMERASRC_SUCCESS; + } + + PRINT_IOCTL_INFO(request, arg); + +again: + err = ioctl (fd, request, arg); + + if (err != 0) { + handle->errnum = errno; + err = errno; + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("ioctl[%x] err : %s", request, err_msg); + if (err == EEXIST) { + camsrc_info("EEXIST occured, but can go."); + err = 0; + } else if (err == ENOENT) { + camsrc_info("ENOENT occured, but can go."); + err = 0; +#if defined (ENABLE_Q_ERROR) +#warning "ENABLE_Q_ERROR enabled" + } else if (request == VIDIOC_DQBUF) { + goto DQ_ERROR; + } else if (request == VIDIOC_QBUF) { + goto ENQ_ERROR; +#endif + } else if (err == EINVAL) { + camsrc_error("EINVAL occured, Shutdown"); + UNLOCK(handle); + return CAMERASRC_ERR_INVALID_PARAMETER; + } else if (err == EAGAIN && nAgain--) { + goto again; + } else { + camsrc_error("Unhandled exception occured on IOCTL"); + } + } + + UNLOCK(handle); + + return CAMERASRC_SUCCESS; + +#if defined (ENABLE_Q_ERROR) +DQ_ERROR: + camsrc_error("DQ Frame error occured"); + printf("DQ Frame error occured"); + UNLOCK(handle); + return CAMERASRC_ERR_INTERNAL; + +ENQ_ERROR: + camsrc_error("Q Frame error occured"); + printf("Q Frame error occured"); + UNLOCK(handle); + return CAMERASRC_ERR_INTERNAL; +#endif +} + + +static int _camerasrc_skip_frame(camerasrc_handle_t *handle, long int timeout, int skip_frame) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + fd_set fds; + struct timeval tv; + int r; + int skip_frame_num = skip_frame; + + camsrc_error("enter"); + + p = handle; + + while (skip_frame_num--) { + camsrc_error("SKIP FRAME #%d", skip_frame-skip_frame_num+1); + + struct v4l2_buffer buf; + + FD_ZERO (&fds); + FD_SET (p->dev_fd, &fds); + + camsrc_error("************Still capture wait frame start"); + + /* Timeout. */ + tv.tv_sec = (long int)timeout/1000; + tv.tv_usec = timeout - tv.tv_sec * 1000; + + r = select (p->dev_fd + 1, &fds, NULL, NULL, &tv); + + if (-1 == r) { + if (EINTR == errno) { + return CAMERASRC_SUCCESS; + } + camsrc_error("select() failed."); + return CAMERASRC_ERR_INTERNAL; + } + + if (0 == r) { + camsrc_error("select() timeout."); + return CAMERASRC_ERR_DEVICE_WAIT_TIMEOUT; + } + + /**@note from samsung camera sample. *****************************************/ + /*SKIP FRAME START*/ + CLEAR(buf); + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_MMAP; + buf.index = p->buffer_idx; + + if (-1 == _camerasrc_ioctl (p, VIDIOC_DQBUF, &buf)) { + switch (errno) { + case EAGAIN: + camsrc_info("VIDIOC_DQBUF [EAGAIN]"); + return CAMERASRC_SUCCESS; + case EIO: + /* Could ignore EIO, see spec. */ + /* fall through */ + camsrc_info("VIDIOC_DQBUF [EIO]"); + default: + camsrc_info("VIDIOC_DQBUF [%d]", errno); + return CAMERASRC_ERR_IO_CONTROL; + } + } + + if (-1 == _camerasrc_ioctl (p, VIDIOC_QBUF, &buf)) { + return CAMERASRC_ERR_IO_CONTROL; + } + } + + camsrc_error("leave"); + err = CAMERASRC_SUCCESS; + + return err; +} + +static int _camerasrc_init_autofocusing_mode(camerasrc_handle_t *handle) +{ + int ctrl_id = V4L2_CID_FOCUS_AUTO_MODE; + int mode; + + camsrc_info("enter"); + + if (handle->af_status == CAMERASRC_AUTO_FOCUS_STATUS_ONGOING) { + camsrc_info("Dev BUSY. Init failed."); + return CAMERASRC_ERR_INVALID_STATE; + } + + switch (handle->cur_af_mode) { + case CAMERASRC_AF_MODE_AUTO: + mode = V4L2_FOCUS_AUTO_NORMAL; + camsrc_info("ON AUTOFOCUSING...BY AUTO"); + break; + case CAMERASRC_AF_MODE_MANUAL: + ctrl_id = V4L2_CID_FOCUS_MODE; + mode = V4L2_FOCUS_MODE_MANUAL; + camsrc_info("ON AUTOFOCUSING...BY MANUAL"); + break; + case CAMERASRC_AF_MODE_CONTINUOUS: + mode = V4L2_FOCUS_AUTO_CONTINUOUS; + camsrc_info("ON AUTOFOCUSING...BY CONTINUOUS"); + if (handle->format.pix_format != CAMERASRC_PIX_NV12 && + handle->format.pix_format != CAMERASRC_PIX_SN12) { + camsrc_info("DO NOT CONTINUOUS AF when NV12 or SN12 format"); + return CAMERASRC_SUCCESS; + } + break; + case CAMERASRC_AF_MODE_TOUCH_AUTO: + mode = V4L2_FOCUS_AUTO_RECTANGLE; + camsrc_info("ON AUTOFOCUSING...BY TOUCH AUTO"); + break; + case CAMERASRC_AF_MODE_PAN: + camsrc_warning("PAN MODE focusing is not supported."); + default: + camsrc_warning("Unsupported AF mode[%d]", handle->cur_af_mode ); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + + /* MACRO is prior than other setting. FIXME */ + if (handle->cur_af_range == CAMERASRC_AF_RANGE_MACRO) { + mode = V4L2_FOCUS_AUTO_MACRO; + camsrc_info("ON AUTOFOCUSING...BY MACRO"); + } + + SET_CTRL_VAL(ctrl_id, mode); + + /* Start AF if continuous mode and preview is running */ + if (mode == V4L2_FOCUS_AUTO_CONTINUOUS && + handle->cur_state >= CAMERASRC_STATE_PREVIEW) { + _camerasrc_start_autofocusing(handle); + } + + return CAMERASRC_SUCCESS; +} + +static void __CAMERASRC_FUJITSU_GET_AF_CTRL_VAL(camerasrc_handle_t* handle, int* status) +{ + int err = 0; + + if (af_retry_cnt >= (int)CAMERASRC_AF_TOTALTIME/CAMERASRC_AF_INTERVAL) { + *status = CAMERASRC_SENSOR_AF_STATUS_FAILED; + af_retry_cnt = 0; + return; + } else { + af_retry_cnt++; + camsrc_info("retry count = %d", af_retry_cnt); + } + + SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 1, err); + switch (err) { + case 0: + camsrc_info("Succeeded"); + *status = CAMERASRC_SENSOR_AF_STATUS_FOCUSED; + af_retry_cnt = 0; + break; + case EBUSY: + camsrc_info("Busy"); + *status = CAMERASRC_SENSOR_AF_STATUS_ONGOING; + break; + default: + camsrc_info("Failed"); + *status = CAMERASRC_SENSOR_AF_STATUS_FAILED; + af_retry_cnt = 0; + break; + } + + return; +} + +static void* _camerasrc_run_autofocusing(camerasrc_handle_t *handle) +{ + int err = 0; + int sensor_status = 0; + + camsrc_info("enter"); + + while(1) { + AF_MUT_LOCK(handle); + switch (handle->af_cmd) { + case CAMERASRC_AUTO_FOCUS_CMD_START: + { + camsrc_info("AF CMD:START"); + __CAMERASRC_FUJITSU_GET_AF_CTRL_VAL(handle, &sensor_status); + + switch (sensor_status) { + case CAMERASRC_SENSOR_AF_STATUS_ONGOING: + camsrc_info("FOCUSING. IN PROGRESS..."); + handle->af_status = CAMERASRC_AUTO_FOCUS_STATUS_ONGOING; + break; + case CAMERASRC_SENSOR_AF_STATUS_FOCUSED: /* focus operation success. Go to Null. */ + camsrc_info("FOCUSED. stop autofocusing..."); + + handle->af_status = CAMERASRC_AUTO_FOCUS_STATUS_RELEASED; + handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_NULL; + + CAMERASRC_SET_STATE(handle, CAMERASRC_STATE_PREVIEW); + + if (handle->af_cb != NULL) { + if (handle->af_usr_data != NULL) { + handle->af_cb(handle, CAMERASRC_AUTO_FOCUS_RESULT_FOCUSED, handle->af_usr_data); + } else { + handle->af_cb(handle, CAMERASRC_AUTO_FOCUS_RESULT_FOCUSED, NULL); + } + } + + AF_MUT_UNLOCK(handle); + continue; + case CAMERASRC_SENSOR_AF_STATUS_FAILED: /* focus operation failed. Set stop and go to NULL. */ + camsrc_info("FOCUSING FAILED"); + + if (handle->cur_af_mode != CAMERASRC_AF_MODE_CONTINUOUS ) { + SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 0, err); + camsrc_info("Stopping AF done. err = %d", err); + } + + handle->af_status = CAMERASRC_AUTO_FOCUS_STATUS_RELEASED; + handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_NULL; + + CAMERASRC_SET_STATE(handle, CAMERASRC_STATE_PREVIEW); + + if (handle->af_cb != NULL) { + if(handle->af_usr_data != NULL) { + handle->af_cb(handle, CAMERASRC_AUTO_FOCUS_RESULT_FAILED, handle->af_usr_data); + } else { + handle->af_cb(handle, CAMERASRC_AUTO_FOCUS_RESULT_FAILED, NULL); + } + } + + AF_MUT_UNLOCK(handle); + continue; + default: + camsrc_error("Unrecognizable status"); + break; + } + break; + } + case CAMERASRC_AUTO_FOCUS_CMD_STOP: + { + camsrc_info("AF CMD:STOP"); + SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 0, err); + handle->af_status = CAMERASRC_AUTO_FOCUS_STATUS_RELEASED; + handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_NULL; + camsrc_info("Stopping AF done. err = %d", err); + AF_MUT_UNLOCK(handle); + continue; + } + case CAMERASRC_AUTO_FOCUS_CMD_KILL: + { + camsrc_info("AF CMD:KILL"); + + SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 0, err); + camsrc_info("Stopping AF done. err = %d", err); + + handle->af_status = CAMERASRC_AUTO_FOCUS_STATUS_RELEASED; + handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_NULL; + + AF_MUT_UNLOCK(handle); + goto OUT_OF_LOOP; + } + case CAMERASRC_AUTO_FOCUS_CMD_NULL: + default: + { + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + camsrc_info("AF CMD:NULL...."); + err = pthread_cond_wait(&handle->af_wait_cond, &handle->af_mutex); + if (err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("AF CMD pthread_cond_wait - err:(%s)", err_msg); + } + + af_retry_cnt = 0; + + /* Start AF delay for AE */ + if (handle->af_cmd == CAMERASRC_AUTO_FOCUS_CMD_START) { + struct timeval cur_time; + int sec = 0; + int usec = 0; + int diff = 0; /* msec */ + int sleep_time = 0; /* usec */ + + SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 0, err); + + camsrc_info("Check set AF area Time(Delay:%d)", CAMERASRC_AF_DELAY_AFTER_SET_AF_AREA); + + if (handle->set_af_area_time.tv_sec != 0) { + gettimeofday(&cur_time, NULL); + sec = cur_time.tv_sec - handle->set_af_area_time.tv_sec; + usec = cur_time.tv_usec - handle->set_af_area_time.tv_usec; + + camsrc_info("diff sec:%d, usec:%d", sec, usec); + diff = (sec * 1000) + (usec / 1000); + + /* Skip sleep if diff is less than zero or greater than minimum exposure time */ + if (diff < 0 || diff > CAMERASRC_AF_DELAY_AFTER_SET_AF_AREA) { + camsrc_info("no need to sleep(diff: %d ms)", diff); + } else { + sleep_time = (CAMERASRC_AF_DELAY_AFTER_SET_AF_AREA - diff) * 1000; + camsrc_info("usleep(%d)", sleep_time); + usleep(sleep_time); + } + } + } + + AF_MUT_UNLOCK(handle); + continue; + } + } + + AF_MUT_UNLOCK(handle); + usleep(CAMERASRC_AF_INTERVAL); + } + +OUT_OF_LOOP: + camsrc_info("AF thread is finished."); + return NULL; +} + +static int _camerasrc_start_autofocusing(camerasrc_handle_t *handle) +{ + int err; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + camsrc_info("enter"); + + AF_MUT_LOCK(handle); + + handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_START; + err = pthread_cond_signal(&handle->af_wait_cond); + if (err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("AF wait cond err(%s)", err_msg); + } + + AF_MUT_UNLOCK(handle); + + return CAMERASRC_SUCCESS; +} + +static int _camerasrc_stop_autofocusing(camerasrc_handle_t *handle) +{ + int err; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + camsrc_info("enter"); + + AF_MUT_LOCK(handle); + + handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_STOP; + err = pthread_cond_signal(&handle->af_wait_cond); + if (err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("AF wait cond err(%s)", err_msg); + } + + AF_MUT_UNLOCK(handle); + + return CAMERASRC_SUCCESS; +} + +static int _camerasrc_destroy_autofocusing(camerasrc_handle_t *handle) +{ + int err; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + camsrc_info("enter"); + + AF_MUT_LOCK(handle); + + handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_KILL; + err = pthread_cond_signal(&handle->af_wait_cond); + if (err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("AF wait cond err(%s)", err_msg); + } + + AF_MUT_UNLOCK(handle); + + return CAMERASRC_SUCCESS; +} + +static int _camerasrc_release_autofocusing(camerasrc_handle_t *handle) +{ + int err; + camsrc_info("enter"); + + SET_CTRL_VAL_ERR(V4L2_CID_CAMERA_SET_AUTO_FOCUS, 0, err); + + return CAMERASRC_SUCCESS; +} + +static int _camerasrc_copy_frame(camerasrc_handle_t *handle, camerasrc_buffer_t *src_buffer, camerasrc_buffer_t *dst_buffer, int isThumbnail) +{ + if (handle == NULL || src_buffer == NULL || dst_buffer == NULL) { + camsrc_error("handle[%p], src_buffer[%p], dst_buffer[%p]", + handle, src_buffer, dst_buffer); + return CAMERASRC_ERR_NULL_POINTER; + } + + if (src_buffer->start == NULL || dst_buffer->start == NULL) { + camsrc_error("src_buffer->start[%p], dst_buffer->start[%p]", + src_buffer->start, dst_buffer->start); + return CAMERASRC_ERR_NULL_POINTER; + } + + if (handle->format.colorspace == CAMERASRC_COL_RAW) { + int cpy_len; + + if (handle->format.pix_format == CAMERASRC_PIX_YUV422P || + handle->format.pix_format == CAMERASRC_PIX_YUY2 || + handle->format.pix_format == CAMERASRC_PIX_UYVY) { + cpy_len = (YUV422_SIZE(handle)>=src_buffer->length)?src_buffer->length:YUV422_SIZE(handle); + } else if (handle->format.pix_format == CAMERASRC_PIX_YUV420P || + handle->format.pix_format == CAMERASRC_PIX_YUV420 || + handle->format.pix_format == CAMERASRC_PIX_NV12) { + cpy_len = (YUV420_SIZE(handle)>=src_buffer->length)?src_buffer->length:YUV420_SIZE(handle); + } else { + camsrc_error("UNSUPPORTED format [%x]", handle->format.pix_format ); + return CAMERASRC_ERR_INVALID_FORMAT; + } + +#if defined (USE_FRAME_COPY_BOUNDARY_CHECK) + camsrc_info("Boundary check %p ~ %p ... [length = %d] ", dst_buffer->start, dst_buffer->start+cpy_len, cpy_len); + memset(dst_buffer->start, 0, cpy_len); + camsrc_info("Boundary check OK"); +#endif + + camsrc_info("RAW frame dequeing..."); + dst_buffer->length = cpy_len; + + if (handle->format.pix_format == CAMERASRC_PIX_SN12) { + camsrc_error("Can't copy the frame with SN12 option"); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } else if (handle->format.pix_format == CAMERASRC_PIX_ST12) { + camsrc_error("Can't copy the frame with ST12 option"); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + + camsrc_error("format[%d] copy occured. copy len = %d", handle->format.pix_format, cpy_len); + memcpy(dst_buffer->start, src_buffer->start, cpy_len ); + } else if (handle->format.colorspace == CAMERASRC_COL_JPEG) { +#if defined (USE_FRAME_COPY_BOUNDARY_CHECK) + camsrc_info("Boundary check %p ~ %p ...", dst_buffer->start, dst_buffer->start+src_buffer->length); + memset(dst_buffer->start, 0, src_buffer->length); + camsrc_info("Boundary check OK"); +#endif + + if (!isThumbnail) { + if (src_buffer->length <= 0) { + camsrc_error("length[%d] is too small", src_buffer->length ); + return CAMERASRC_ERR_INVALID_VALUE; + } + + dst_buffer->length = src_buffer->length; + camsrc_info("JPEG main frame copy... img length = %d", dst_buffer->length); + memcpy(dst_buffer->start, src_buffer->start, dst_buffer->length); + camsrc_info("JPEG main frame copy done."); + } else { + if (handle->format.pix_format == CAMERASRC_PIX_RGGB8) { + /* JPEG + JPEG */ + if (src_buffer->length <= 0) { + camsrc_error("length[%d] is too small", src_buffer->length ); + return CAMERASRC_ERR_INVALID_VALUE; + } + + dst_buffer->length = src_buffer->length; + camsrc_info("JPEG thm frame(JPEG) copy... img length = %d", dst_buffer->length); + memcpy(dst_buffer->start, src_buffer->start, dst_buffer->length); + camsrc_info("JPEG thm frame(JPEG) copy done."); + } else if (handle->format.pix_format == CAMERASRC_PIX_RGGB10) { + /* JPEG + YUV */ + dst_buffer->length = CAMERASRC_YUV_THMBNL_SIZE; + camsrc_info("JPEG thm frame(YUV) copy... img length = %d", CAMERASRC_YUV_THMBNL_SIZE); + memcpy(dst_buffer->start, src_buffer->start, CAMERASRC_YUV_THMBNL_SIZE); + camsrc_info("JPEG thm frame(YUV) copy done."); + } else { + camsrc_info("Unknown format [%d]", handle->format.pix_format ); + return CAMERASRC_ERR_INVALID_FORMAT; + } + } + } + + return CAMERASRC_SUCCESS; +} + + +static int _camerasrc_set_thumbnail_size(camerasrc_handle_t *handle) +{ + camsrc_info("enter"); + + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +} + +static int _camerasrc_start_facedetection (camerasrc_handle_t *handle) +{ + camsrc_info("enter"); + camsrc_info("FACE DETECTION START!"); + SET_CTRL_VAL(V4L2_CID_FACE_DETECTION, CAM_FACE_DETECTION_ON); + camsrc_info("leave"); + return CAMERASRC_SUCCESS; +} + + +static int _camerasrc_stop_facedetection (camerasrc_handle_t *handle) +{ + camsrc_info("enter"); + camsrc_info("FACE DETECTION STOP!"); + SET_CTRL_VAL(V4L2_CID_FACE_DETECTION, CAM_FACE_DETECTION_OFF); + camsrc_info("leave"); + return CAMERASRC_SUCCESS; +} + + +static int _camerasrc_check_emp_shock(camerasrc_handle_t *handle, int *check_val) +{ + camsrc_info("enter"); + int ret_value = 0; + + GET_CTRL_VAL(V4L2_CID_ESD_INT, ret_value); + *check_val = ret_value; + + camsrc_info("leave"); + return CAMERASRC_SUCCESS; +} + + +static int _camerasrc_get_frame_data(camerasrc_handle_t *handle, camerasrc_frame_data_t *data) +{ + /*camsrc_info("enter");*/ + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_control control; + + /* Get Y physical address */ + /*camsrc_info("[VIDIOC_G_CTRL] << [V4L2_CID_PADDR_Y] request with value");*/ + control.id = V4L2_CID_PADDR_Y; + control.value = data->index; + err = _camerasrc_ioctl(handle, VIDIOC_S_CTRL, &control); + if (err != CAMERASRC_SUCCESS) { + return err; + } + + data->phyAddrY = control.value; + + /* Get CbCr physical address */ + /*camsrc_info("[VIDIOC_G_CTRL] << [V4L2_CID_PADDR_CBCR] request with value");*/ + control.id = V4L2_CID_PADDR_CBCR; + control.value = data->index; + err = _camerasrc_ioctl(handle, VIDIOC_S_CTRL, &control); + if (err != CAMERASRC_SUCCESS) { + return err; + } + + data->phyAddrCbCr = control.value; + + /* Distance between Y ptr and CbCr ptr of virtual address is same with that of Physical one. */ + data->virAddrY = (unsigned int)handle->buffer[data->index].start; + data->virAddrCbCr = data->virAddrY + (data->phyAddrCbCr - data->phyAddrY); + + /*camsrc_info("virAddrY(%p), virAddrCbCr(%p)", data->virAddrY, data->virAddrCbCr);*/ + + return CAMERASRC_SUCCESS; +} + + +static void _dump_exif_info(camerasrc_exif_t *exif_struct) +{ + camsrc_info("== Dynamic value =="); + camsrc_info("unsigned int exposure_time_numerator = %d", exif_struct->exposure_time_numerator); + camsrc_info("unsigned int exposure_time_denominator = %d", exif_struct->exposure_time_denominator); + camsrc_info("int shutter_speed_numerator = %d", exif_struct->shutter_speed_numerator); + camsrc_info("int shutter_speed_denominator = %d", exif_struct->shutter_speed_denominator); + camsrc_info("int brigtness_numerator = %d", exif_struct->brigtness_numerator); + camsrc_info("int brightness_denominator = %d", exif_struct->brightness_denominator); + camsrc_info("unsigned short int iso = %d", exif_struct->iso); + camsrc_info("unsigned short int flash = %d", exif_struct->flash); + camsrc_info("int metering_mode = %d", exif_struct->metering_mode); + camsrc_info("int exif_image_width = %d", exif_struct->exif_image_width); + camsrc_info("int exif_image_height = %d", exif_struct->exif_image_height); + camsrc_info("int exposure_bias_in_APEX = %d", exif_struct->exposure_bias_in_APEX); + camsrc_info("int software_used = %d", exif_struct->software_used); + camsrc_info("int focal_len_numerator = %d", exif_struct->focal_len_numerator); + camsrc_info("int focal_len_denominator = %d", exif_struct->focal_len_denominator); + camsrc_info("int aperture_f_num_numerator = %d", exif_struct->aperture_f_num_numerator); + camsrc_info("int aperture_f_num_denominator = %d", exif_struct->aperture_f_num_denominator); + camsrc_info("int aperture_in_APEX = %d", exif_struct->aperture_in_APEX); + camsrc_info("int max_lens_aperture_in_APEX = %d", exif_struct->max_lens_aperture_in_APEX); + + camsrc_info("== Fixed value =="); + camsrc_info("int component_configuration = %x", exif_struct->component_configuration); + camsrc_info("int colorspace = %d", exif_struct->colorspace); + + return; +} + + +static int _camerasrc_get_exif_info (camerasrc_handle_t *handle, camerasrc_exif_t *exif_struct) +{ + int photometry_mode = V4L2_PHOTOMETRY_MULTISEG; + + if (exif_struct == NULL) { + return CAMERASRC_ERR_INVALID_PARAMETER; + } + + /** Dynamic value **/ + + /* exposure time */ + GET_CTRL_VAL(V4L2_CID_CAMERA_EXIF_EXPTIME, exif_struct->exposure_time_numerator); + exif_struct->exposure_time_denominator = 1; + + /* shutter speed */ + GET_CTRL_VAL(V4L2_CID_CAMERA_EXIF_TV, exif_struct->shutter_speed_numerator); + exif_struct->shutter_speed_denominator = 1; + + /* brightness */ + GET_CTRL_VAL(V4L2_CID_CAMERA_EXIF_BV, exif_struct->brigtness_numerator); + exif_struct->brightness_denominator = 1; + + /* iso */ + GET_CTRL_VAL(V4L2_CID_CAMERA_EXIF_ISO, exif_struct->iso); + ISO_APPROXIMATE_VALUE(exif_struct->iso, exif_struct->iso); + + /* flash */ + GET_CTRL_VAL(V4L2_CID_CAMERA_EXIF_FLASH, exif_struct->flash); + + /* image size */ + exif_struct->exif_image_width = handle->format.img_size.dim.width; + exif_struct->exif_image_height = handle->format.img_size.dim.height; + + /*FIXME focal length - Set fixed value at gst_camerasrc_control_get_exif_info function */ + exif_struct->focal_len_numerator = 1; + exif_struct->focal_len_denominator = 1; + + /*FIXME f number - Set fixed value at gst_camerasrc_control_get_exif_info function */ + exif_struct->aperture_f_num_numerator = 1; + exif_struct->aperture_f_num_denominator = 1; + + /* FIXME APEX */ + exif_struct->aperture_in_APEX \ + = CAMERASRC_EXIF_APERTURE_VALUE_IN_APEX(exif_struct->aperture_f_num_numerator, exif_struct->aperture_f_num_denominator); + exif_struct->max_lens_aperture_in_APEX = 1; + /*= CAMERASRC_EXIF_APERTURE_VALUE_IN_APEX(exif_info.max_aperture_f_num_numerator, exif_info.max_aperture_f_num_denominator);*/ + exif_struct->exposure_bias_in_APEX = exif_struct->aperture_in_APEX \ + + CAMERASRC_EXIF_SHUTTERSPEED_VALUE_IN_APEX(exif_struct->exposure_time_numerator, exif_struct->exposure_time_denominator); + + /* Get the value using CID */ + /* Not implemented yet + GET_CTRL_VAL(V4L2_CID_FW_VERSION, exif_struct->software_used); + */ + GET_CTRL_VAL(V4L2_CID_PHOTOMETRY, photometry_mode); + PHOTOMETRY_MODE_TO_METERING_MODE(photometry_mode, exif_struct->metering_mode); + + /** Fixed value **/ + exif_struct->component_configuration = _CAMERASRC_EXIF_COMP_CONF; + exif_struct->colorspace = _CAMERASRC_EXIF_COLORSPACE; + + _dump_exif_info(exif_struct); + + return CAMERASRC_SUCCESS; +} + +static int fd_on = 0; + +static int _camerasrc_set_cmd(camerasrc_handle_t *handle, _camsrc_cmd_t cmd, void *value) +{ + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_jpegcompression comp_arg; + + switch (cmd) { + case _CAMERASRC_CMD_STROBE_MODE: + { + camerasrc_strobe_mode_t *mode = (camerasrc_strobe_mode_t*)value; + + camsrc_info("[_CAMERASRC_CMD_STROBE_MODE] cmd set value %d", *mode ); + + SET_CTRL_VAL(V4L2_CID_CAMERA_FLASH_MODE, *mode); + } + break; + case _CAMERASRC_CMD_FACEDETECTION: + { + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_recognition recog; + + camsrc_info("[_CAMERASRC_CMD_FACEDETECTION] cmd set"); + if (((int)value) == _CAMERASRC_FACEDETECTION_START) { + camsrc_info("[_CAMERASRC_CMD_FACEDETECTION] start"); + recog.mode = V4L2_RECOGNITION_MODE_ON; + recog.pattern = V4L2_RECOG_PATTERN_FACE; + recog.obj_num = 10; + /*recog.detect_idx = 0;*/ + recog.action= V4L2_RECOGNITION_ACTION_NONE; + fd_on = 1; + } else if (((int)value) == _CAMERASRC_FACEDETECTION_STOP) { + camsrc_error("[_CAMERASRC_CMD_FACEDETECTION] stop"); + recog.mode = V4L2_RECOGNITION_MODE_OFF; + recog.pattern = V4L2_RECOG_PATTERN_FACE; + recog.obj_num = 10; + /*recog.detect_idx = 0;*/ + recog.action= V4L2_RECOGNITION_ACTION_NONE; + fd_on = 0; + } else { + camsrc_error("[_CAMERASRC_CMD_FACEDETECTION] not support cmd"); + } + + err = _camerasrc_ioctl(handle, VIDIOC_S_RECOGNITION, &recog); + if (err != CAMERASRC_SUCCESS) { + goto ERROR; + } + } + break; + case _CAMERASRC_CMD_SHUTTER_SPEED: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_SHUTTER_SPEED] cmd set"); + break; + case _CAMERASRC_CMD_JPEG_LENGTH: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_JPEG_LENGTH] cmd set"); + break; + case _CAMERASRC_CMD_JPEG_THMBNL_LENGTH: + camsrc_info("[_CAMERASRC_CMD_JPEG_THMBNL_LENGTH] cmd set"); + err = _camerasrc_set_thumbnail_size(handle); + if (err != CAMERASRC_SUCCESS) { + goto ERROR; + } + break; + case _CAMERASRC_CMD_EXPOSURE_VALUE: + camsrc_info("[_CAMERASRC_CMD_EXPOSURE_VALUE] cmd set"); + /* WRITEME */ + break; + case _CAMERASRC_CMD_ESD_CHECK: + camsrc_info("[_CAMERASRC_CMD_ESD_CHECK] cmd set"); + /* WRITEME */ + break; + case _CAMERASRC_CMD_CTRL: + camsrc_info("[_CAMERASRC_CMD_CTRL] cmd set"); + SET_CTRL_VAL(_CAMERASRC_GET_CID(((_camerasrc_ctrl_t *) value)->cid, handle->cur_dev_id), ((_camerasrc_ctrl_t *) value)->value); + break; + case _CAMERASRC_CMD_SUPPORT_EMBED_EXIF: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_SUPPORT_EMBED_EXIF] cmd set"); + break; + case _CAMERASRC_CMD_SUPPORT_JPEG_ENCODING: + camsrc_warning("[_CAMERASRC_CMD_SUPPORT_JPEG_ENCODING] cmd set isn't supported"); + break; + case _CAMERASRC_CMD_AF_CONTROL: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_AF_CONTROL] cmd set(value=%d)", value); + + /* FIXME : Please fix whole AF implementation!!! */ + switch ((int)value) { + case _CAMERASRC_AF_START: + _camerasrc_start_autofocusing(handle); + break; + case _CAMERASRC_AF_STOP: + _camerasrc_stop_autofocusing(handle); + break; + case _CAMERASRC_AF_DESTROY: + _camerasrc_destroy_autofocusing(handle); + break; + case _CAMERASRC_AF_RELEASE: + case _CAMERASRC_AF_INIT: + default: + _camerasrc_init_autofocusing_mode(handle); + break; + } + break; + case _CAMERASRC_CMD_AF_AREA: + { + camerasrc_rect_t *rect = (camerasrc_rect_t *)value; + + camsrc_info("[_CAMERASRC_CMD_AF_AREA] cmd set (%d,%d,%dx%d)", + rect->x, rect->y, rect->width, rect->height); + + SET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_LEFT, rect->x); + SET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_TOP, rect->y); + SET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_WIDTH, 0);/*rect->width); Not supported */ + SET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_HEIGHT, 0);/*rect->height); Not supported */ + + if (gettimeofday(&handle->set_af_area_time, NULL) == 0) { + camsrc_info("[_CAMERASRC_CMD_AF_AREA] Get Time Success" ); + } else { + handle->set_af_area_time.tv_sec = 0; + handle->set_af_area_time.tv_usec = 0; + camsrc_warning("[_CAMERASRC_CMD_AF_AREA] Get Time Failed" ); + } + break; + } + case _CAMERASRC_CMD_FRAME_DATA: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_FRAME_DATA] cmd set"); + break; + case _CAMERASRC_CMD_EXIF_INFO: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_EXIF_INFO] cmd set"); + break; + case _CAMERASRC_CMD_CHECK_ESD: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_CHECK_ESD] cmd set"); + break; + case _CAMERASRC_CMD_JPEG_COMPRESS_RATIO: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_JPEG_COMPRESS_RATIO] cmd set, val = %d", (int)(*((unsigned int*) value))); + if (handle->cur_dev_id == CAMERASRC_DEV_ID_PRIMARY) { + comp_arg.quality = (int)(*((unsigned int*) value)); + err = _camerasrc_ioctl(handle, VIDIOC_S_JPEGCOMP, &comp_arg); + if (err != CAMERASRC_SUCCESS) { + goto ERROR; + } + } else if (handle->cur_dev_id == CAMERASRC_DEV_ID_SECONDARY) { + err = CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + goto ERROR; + } + break; + case _CAMERASRC_CMD_ROTATION: + { + int *rotate = (int *)value; + camsrc_info("[_CAMERASRC_CMD_ROTATION] cmd set : %d", *rotate); + SET_CTRL_VAL(V4L2_CID_ROTATION, (int)*rotate); + } + break; + case _CAMERASRC_CMD_SENSOR_MODE: + { + camerasrc_sensor_mode_t *mode = (camerasrc_sensor_mode_t *)value; + camsrc_info("[_CAMERASRC_CMD_SENSOR_MODE] cmd set : %d", *mode); + SET_CTRL_VAL(V4L2_CID_CAMERA_SENSOR_MODE, (int)*mode); + } + break; + case _CAMERASRC_CMD_VFLIP: + { + int *vflip = (int *)value; + camsrc_info("[_CAMERASRC_CMD_VFLIP] cmd set : %d", *vflip); + SET_CTRL_VAL(V4L2_CID_VFLIP, (int)*vflip); + } + break; + case _CAMERASRC_CMD_HFLIP: + { + int *hflip = (int *)value; + camsrc_info("[_CAMERASRC_CMD_HFLIP] cmd set : %d", *hflip); + SET_CTRL_VAL(V4L2_CID_HFLIP, (int)*hflip); + } + break; + default: + camsrc_error("[_CAMERASRC_CMD_UNKNOWN] cmd set"); + err = CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + goto ERROR; + } + + return CAMERASRC_SUCCESS; + +ERROR: + camsrc_error("cmd execution error occured"); + + return err; +} + +static int _camerasrc_get_cmd(camerasrc_handle_t *handle, _camsrc_cmd_t cmd, void *value) +{ + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_jpegcompression comp_arg; + + if (!value) { + camsrc_error("value is NULL"); + return CAMERASRC_ERR_NULL_POINTER; + } + + switch (cmd) { + case _CAMERASRC_CMD_SUPPORT_EMBED_EXIF: + camsrc_info("[_CAMERASRC_CMD_SUPPORT_EMBED_EXIF] cmd get"); + *((int*)value) = _CAMERASRC_CMD_SUPPORT_EMBED_EXIF_DEF; + break; + case _CAMERASRC_CMD_SUPPORT_JPEG_ENCODING: + camsrc_info("[_CAMERASRC_CMD_SUPPORT_JPEG_ENCODING] cmd get(cur_dev_id=%d)", handle->cur_dev_id); + if (handle->cur_dev_id == CAMERASRC_DEV_ID_PRIMARY) { + *((int*)value) = 1; + } else { + *((int*)value) = 0; + } + break; + case _CAMERASRC_CMD_STROBE_MODE: + { + camerasrc_strobe_mode_t mode; + + GET_CTRL_VAL(V4L2_CID_CAMERA_FLASH_MODE, mode); + *((camerasrc_strobe_mode_t*)value) = mode; + + camsrc_info("[_CAMERASRC_CMD_STROBE_MODE] cmd get - %d", mode); + } + break; + case _CAMERASRC_CMD_FACEDETECTION: + camsrc_info("[_CAMERASRC_CMD_FACEDETECTION] cmd get - %d", fd_on); + *(int *)value = fd_on; + break; + case _CAMERASRC_CMD_SHUTTER_SPEED: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_SHUTTER_SPEED] cmd get"); + break; + case _CAMERASRC_CMD_JPEG_LENGTH: + { + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_control ctrl; + + ctrl.id = V4L2_CID_CAM_JPEG_MAIN_SIZE; + err = _camerasrc_ioctl( handle, VIDIOC_G_CTRL, &ctrl ); + if (err != CAMERASRC_SUCCESS) { + *((int*)value) = 0; + goto ERROR; + } + + *((int*)value) = ctrl.value; + camsrc_info("[_CAMERASRC_CMD_JPEG_LENGTH] cmd get"); + break; + } + case _CAMERASRC_CMD_JPEG_THMBNL_LENGTH: + { + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_control ctrl; + + ctrl.id = V4L2_CID_CAM_JPEG_THUMB_SIZE; + err = _camerasrc_ioctl( handle, VIDIOC_G_CTRL, &ctrl ); + if (err != CAMERASRC_SUCCESS) { + *((int*)value) = 0; + goto ERROR; + } + + *((int*)value) = ctrl.value; + camsrc_info("[_CAMERASRC_CMD_JPEG_THMBNL_LENGTH] cmd get : %d", *((int*)value)); + break; + } + case _CAMERASRC_CMD_JPEG_THMBNL_OFFSET: + { + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_control ctrl; + ctrl.id = V4L2_CID_CAM_JPEG_THUMB_OFFSET; + + err = _camerasrc_ioctl( handle, VIDIOC_G_CTRL, &ctrl ); + if(err != CAMERASRC_SUCCESS) + { + *((int*)value) = 0; + goto ERROR; + } + + *((int*)value) = ctrl.value; + camsrc_info("[_CAMERASRC_CMD_JPEG_THMBNL_OFFSET] cmd get : %x", *((int*)value)); + break; + } + case _CAMERASRC_CMD_JPEG_SCRNL_LENGTH: + { + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_control ctrl; + ctrl.id = V4L2_CID_CAM_JPEG_POSTVIEW_SIZE; + + err = _camerasrc_ioctl(handle, VIDIOC_G_CTRL, &ctrl); + if (err != CAMERASRC_SUCCESS) { + *((int*)value) = 0; + goto ERROR; + } + + *((int*)value) = ctrl.value; + camsrc_info("[_CAMERASRC_CMD_JPEG_SCRNL_LENGTH] cmd get : %x", *((int*)value)); + break; + } + case _CAMERASRC_CMD_JPEG_SCRNL_OFFSET: + { + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_control ctrl; + ctrl.id = V4L2_CID_CAM_JPEG_POSTVIEW_OFFSET; + + err = _camerasrc_ioctl( handle, VIDIOC_G_CTRL, &ctrl ); + if (err != CAMERASRC_SUCCESS) { + *((int*)value) = 0; + goto ERROR; + } + + *((int*)value) = ctrl.value; + camsrc_info("[_CAMERASRC_CMD_JPEG_SCRNL_OFFSET] cmd get : %x", *((int*)value)); + break; + } + case _CAMERASRC_CMD_EXPOSURE_VALUE: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_EXPOSURE_VALUE] cmd get"); + break; + case _CAMERASRC_CMD_ESD_CHECK: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_ESD_CHECK] cmd get"); + break; + case _CAMERASRC_CMD_CTRL: + camsrc_info("[_CAMERASRC_CMD_CTRL] cmd get"); + GET_CTRL_VAL(_CAMERASRC_GET_CID(((_camerasrc_ctrl_t *) value)->cid, handle->cur_dev_id), ((_camerasrc_ctrl_t *) value)->value); + break; + case _CAMERASRC_CMD_AF_CONTROL: + /* WRITEME */ + camsrc_info("[_CAMERASRC_CMD_AF_CONTROL] cmd get"); + break; + case _CAMERASRC_CMD_AF_AREA: + { + camerasrc_rect_t* rect = (camerasrc_rect_t*)value; + + GET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_LEFT, rect->x); + GET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_TOP, rect->y); + GET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_WIDTH, rect->width); + GET_CTRL_VAL(V4L2_CID_FOCUS_AUTO_RECTANGLE_HEIGHT, rect->height); + + camsrc_info("[_CAMERASRC_CMD_AF_AREA] cmd get (%d,%d,%dx%d)", + rect->x, rect->y, rect->width, rect->height); + break; + } + case _CAMERASRC_CMD_FRAME_DATA: + /* WRITEME */ + /*camsrc_info("[_CAMERASRC_CMD_FRAME_DATA] cmd get");*/ + err = _camerasrc_get_frame_data(handle, (camerasrc_frame_data_t*)value); + if (err != CAMERASRC_SUCCESS) { + goto ERROR; + } + break; + case _CAMERASRC_CMD_EXIF_INFO: + camsrc_info("[_CAMERASRC_CMD_EXIF_INFO] cmd get"); + err = _camerasrc_get_exif_info (handle, (camerasrc_exif_t*)value); + if (err != CAMERASRC_SUCCESS) { + goto ERROR; + } + break; + case _CAMERASRC_CMD_CHECK_ESD: + camsrc_info("[_CAMERASRC_CMD_CHECK_ESD] cmd get"); + GET_CTRL_VAL(V4L2_CID_ESD_INT, *((int *) value)); + break; + case _CAMERASRC_CMD_JPEG_COMPRESS_RATIO: + camsrc_info("[_CAMERASRC_CMD_JPEG_COMPRESS_RATIO] cmd get"); + err = _camerasrc_ioctl(handle, VIDIOC_G_JPEGCOMP, &comp_arg); + if (err != CAMERASRC_SUCCESS) { + goto ERROR; + } + *((unsigned int*)value) = (int)comp_arg.quality; + break; + case _CAMERASRC_CMD_ROTATION: + camsrc_info("[_CAMERASRC_CMD_ROTATION] cmd get"); + GET_CTRL_VAL(V4L2_CID_ROTATION, *(int*)value); + break; + case _CAMERASRC_CMD_SENSOR_MODE: + camsrc_info("[_CAMERASRC_CMD_SENSOR_MODE] cmd get"); + GET_CTRL_VAL(V4L2_CID_CAMERA_SENSOR_MODE, *(int*)value); + break; + case _CAMERASRC_CMD_VFLIP: + camsrc_info("[_CAMERASRC_CMD_VFLIP] cmd get"); + GET_CTRL_VAL(V4L2_CID_VFLIP, *(int*)value); + break; + case _CAMERASRC_CMD_HFLIP: + camsrc_info("[_CAMERASRC_CMD_HFLIP] cmd get"); + GET_CTRL_VAL(V4L2_CID_HFLIP, *(int*)value); + break; + default: + camsrc_error("[_CAMERASRC_CMD_UNKNOWN] cmd get"); + err = CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + goto ERROR; + } + + return CAMERASRC_SUCCESS; + +ERROR: + camsrc_error("cmd execution error occured"); + + return err; +} + +static const CAMERASRC_DEV_DEPENDENT_MISC_FUNC dev_misc_functions = { + ._ioctl = _camerasrc_ioctl, + ._ioctl_once = _camerasrc_ioctl_once, + ._run_autofocusing = _camerasrc_run_autofocusing, + ._skip_frame = _camerasrc_skip_frame, + ._copy_frame = _camerasrc_copy_frame, + ._set_cmd = _camerasrc_set_cmd, + ._get_cmd = _camerasrc_get_cmd, +}; + +const CAMERASRC_DEV_DEPENDENT_MISC_FUNC *dev_misc_func = &dev_misc_functions; diff --git a/camerasrc/src/camerasrc.c b/camerasrc/src/camerasrc.c new file mode 100644 index 0000000..00d5de9 --- /dev/null +++ b/camerasrc/src/camerasrc.c @@ -0,0 +1,4870 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "camerasrc-common.h" +#include "camerasrc-internal.h" +#include <semaphore.h> + +#define BUF_LEN 64 +#define ITLV_DATA_INFO_OFFSET 0x1000 +#define EMBEDDED_OFFSET (2040*2+4) + +/* extended V4L2 API */ +#ifndef V4L2_PIX_FMT_INTERLEAVED +#define V4L2_PIX_FMT_INTERLEAVED v4l2_fourcc('I', 'T', 'L', 'V') +#endif /* V4L2_PIX_FMT_INTERLEAVED */ + +#ifndef V4L2_CID_IS_S_SCENARIO_MODE +#define V4L2_CID_IS_S_SCENARIO_MODE (V4L2_CID_FIMC_IS_BASE + 15) +#define V4L2_CID_IS_S_FORMAT_SCENARIO (V4L2_CID_FIMC_IS_BASE + 16) +enum scenario_mode { + IS_MODE_PREVIEW_STILL, + IS_MODE_PREVIEW_VIDEO, + IS_MODE_CAPTURE_STILL, + IS_MODE_CAPTURE_VIDEO, + IS_MODE_MAX +}; +#endif /* V4L2_CID_IS_S_SCENARIO_MODE */ + +#ifndef V4L2_CID_EMBEDDEDDATA_ENABLE +#define V4L2_CID_EMBEDDEDDATA_ENABLE (V4L2_CID_PRIVATE_BASE + 130) +#endif /* V4L2_CID_EMBEDDEDDATA_ENABLE */ + +/* + * LOCAL DEFINITIONS + */ +#ifndef EXPORT_API +#define EXPORT_API __attribute__((__visibility__("default"))) +#endif + +#define LOCK(p) {\ + if(0 != pthread_mutex_lock(&(p->mutex))) {\ + camsrc_error("Mutex locking error");\ + camsrc_assert(0);\ + }\ +} + +#define UNLOCK(p) {\ + if(0 != pthread_mutex_unlock(&(p->mutex))) {\ + camsrc_error("Mutex unlocking error");\ + camsrc_assert(0);\ + }\ +} + +extern const CAMERASRC_DEV_DEPENDENT_MISC_FUNC *dev_misc_func; + +/** proto type of internal function **/ +int _camerasrc_get_JPG_length(camerasrc_handle_t *handle); +int _camerasrc_get_thumbnail_JPG_length(camerasrc_handle_t *handle); +int _camerasrc_get_thumbnail_JPG_offset(camerasrc_handle_t *handle); +int _camerasrc_get_screennail_JPG_length(camerasrc_handle_t *handle); +int _camerasrc_get_screennail_JPG_offset(camerasrc_handle_t *handle); +int _camerasrc_set_thumbnail_size(camerasrc_handle_t *handle); +int _camerasrc_check_emp_shock(camerasrc_handle_t *handle, int *check_val); +int _camerasrc_set_jpeg_compress_ratio(camerasrc_handle_t *handle, unsigned int quality); +int _camerasrc_ioctl(camerasrc_handle_t *handle, int request, void *arg); +int _camerasrc_ioctl_once(camerasrc_handle_t *handle, int request, void *arg); +void *_camerasrc_run_autofocusing(camerasrc_handle_t *handle); +int _camerasrc_skip_frame(camerasrc_handle_t *handle, long int timeout, int skip_frame); + +/**** A U T O F O C U S I N G F U N C T I O N S ****/ +int _camerasrc_set_autofocusing_area(camerasrc_handle_t *handle, camerasrc_rect_t *rect); +int _camerasrc_get_autofocusing_area(camerasrc_handle_t *handle, camerasrc_rect_t *rect); +int _camerasrc_start_autofocusing(camerasrc_handle_t *handle); +int _camerasrc_stop_autofocusing(camerasrc_handle_t *handle); +int _camerasrc_release_autofocusing(camerasrc_handle_t *handle); +int _camerasrc_destroy_autofocusing(camerasrc_handle_t *handle); +int _camerasrc_init_autofocusing_mode(camerasrc_handle_t *handle); + +int _camerasrc_get_frame_data(camerasrc_handle_t *handle, camerasrc_frame_data_t *data); +int _camerasrc_get_exif_info(camerasrc_handle_t *handle, camerasrc_buffer_t *exif_string); +int _camerasrc_copy_frame(camerasrc_handle_t *handle, camerasrc_buffer_t *src_buffer, camerasrc_buffer_t *dst_buffer, int isThumbnail); +int _camerasrc_query_ctrl_menu(camerasrc_handle_t *handle, struct v4l2_queryctrl queryctrl, camerasrc_ctrl_info_t *ctrl_info); + + +#if 1 +typedef struct { + char *key_path; + int key_prefix; +} camerasrc_sync_param_t; + +int camsrc_create_sync(const camerasrc_sync_param_t *param) +{ + sem_t *sem = NULL; + + sem = sem_open(param->key_path, O_CREAT, 0666, 1); + if (sem == SEM_FAILED) { + switch (errno) { + case EACCES: + camsrc_error("The semaphore already exist, but caller does not have permission %s", param->key_path); + break; + case ENOMEM: + camsrc_error("Insufficient memory"); + break; + case ENFILE: + camsrc_error("Too many open files in system"); + break; + default: + camsrc_critical("Semaphore create fail! (name:%s, errno %d)", param->key_path, errno); + } + + return CAMERASRC_ERR_INTERNAL; + } + + return CAMERASRC_SUCCESS; +} + +int camsrc_remove_sync(const camerasrc_sync_param_t *param) +{ + int err = 0; + + err = sem_unlink(param->key_path); + if (err == -1) { + camsrc_critical("Semaphore destroy Fail! (name:%s, errno %d)", param->key_path, errno); + return CAMERASRC_ERR_INTERNAL; + } + + return CAMERASRC_SUCCESS; +} + +int camsrc_lock_sync(const camerasrc_sync_param_t *param) +{ + sem_t *sem = NULL; + int ret; + int err = CAMERASRC_SUCCESS; + int sem_value = -1; + struct timespec wait_time; + + sem = sem_open(param->key_path, O_CREAT, 0666, 1); + if (sem == SEM_FAILED) + { + camsrc_critical("Semaphore open Fail! (name:%s, errno %d)", param->key_path, errno); + return CAMERASRC_ERR_INTERNAL; + } + +retry_lock: + wait_time.tv_sec = (long int)(time(NULL)) + 6; + wait_time.tv_nsec = 0; + + ret = sem_timedwait(sem, &wait_time); + if (ret == -1) { + switch (errno) { + case EINTR: + camsrc_critical("Lock RETRY LOCK"); + goto retry_lock; + break; + case EINVAL: + camsrc_critical("Invalid semaphore"); + err = CAMERASRC_ERR_INTERNAL; + break; + case EAGAIN: + camsrc_critical("EAGAIN"); + err = CAMERASRC_ERR_INTERNAL; + break; + case ETIMEDOUT: + camsrc_critical("sem_wait leached %d seconds timeout.", 6); + /* Recovery of sem_wait lock....in abnormal condition */ + if (0 == sem_getvalue(sem, &sem_value)) { + camsrc_critical("%s sem value is %d",param->key_path, sem_value); + if (sem_value == 0) { + ret = sem_post(sem); + if (ret == -1) { + camsrc_critical("sem_post error %s : %d", param->key_path, sem_value); + } else { + camsrc_critical("lock recovery success...try lock again"); + goto retry_lock; + } + } else { + camsrc_critical("sem value is not 0. but failed sem_timedwait so retry.. : %s",param->key_path); + usleep(5); + goto retry_lock; + } + } else { + camsrc_critical("sem_getvalue failed : %s",param->key_path); + } + err = CAMERASRC_ERR_INTERNAL; + break; + } + } + sem_close(sem); + return err; +} + +int camsrc_unlock_sync(const camerasrc_sync_param_t *param) +{ + sem_t *sem = NULL; + int ret; + int err = CAMERASRC_SUCCESS; + + sem = sem_open(param->key_path, O_CREAT, 0666, 1); + if (sem == SEM_FAILED) { + camsrc_critical("Semaphore open Fail! (name:%s, errno %d)", param->key_path, errno); + return CAMERASRC_ERR_INTERNAL; + } + + ret = sem_post(sem); + if (ret == -1) { + camsrc_critical("UNLOCK FAIL"); + err = CAMERASRC_ERR_INTERNAL; + } + + sem_close(sem); + return err; +} +#endif + + +#if defined(USE_CAMERASRC_FRAME_DUMP) +#define WRITE_UNIT 4096 +static int seq_no = 0; + +static void write_buffer_into_path(camerasrc_buffer_t *buffer, char *path, char *name) +{ + int fd = -1; + int write_len = 0; + char temp_fn[255]; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + sprintf(temp_fn, "%s/%s", path, name); + + fd = open(temp_fn, O_CREAT | O_WRONLY | O_SYNC); + + if(fd == -1) { + strerror_r(errno, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("OPEN ERR!!!! : %s", err_msg); + camsrc_assert(0); + } else { + camsrc_info("Open success, FD = %d, seq = %d", fd, seq_no); + } + + int write_remain = buffer->length; + int offset = 0; + + while (write_remain > 0) { + write_len = write(fd, buffer->start + offset, write_remain<WRITE_UNIT?write_remain:WRITE_UNIT); + if (write_len < 0) { + strerror_r(errno, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("WRITE ERR!!!! : %s", err_msg); + camsrc_assert(0); + } else { + write_remain -= write_len; + offset+= write_len; + camsrc_info("%d written, %d left", write_len, write_remain); + } + } + + close(fd); +} +#endif + +#define CAMERASRC_SET_CMD(cmd, value) _camerasrc_set_cmd(handle, cmd, (void*)value); +#define CAMERASRC_GET_CMD(cmd, value) _camerasrc_get_cmd(handle, cmd, (void*)value); + + +static int _camerasrc_set_cmd(camsrc_handle_t handle, _camsrc_cmd_t cmd, void *value) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (dev_misc_func->_set_cmd == NULL) { + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + + err = dev_misc_func->_set_cmd(p, cmd, value); + + return err; +} + +static int _camerasrc_get_cmd(camsrc_handle_t handle, _camsrc_cmd_t cmd, void *value) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if(dev_misc_func->_get_cmd == NULL) + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + + err = dev_misc_func->_get_cmd(p, cmd, value); + + return err; +} + + +int camerasrc_support_embed_exif(camsrc_handle_t handle, int* support_exif) +{ + int err = CAMERASRC_ERR_UNKNOWN; + + err = CAMERASRC_GET_CMD(_CAMERASRC_CMD_SUPPORT_EMBED_EXIF, support_exif); + if(err != CAMERASRC_SUCCESS) *support_exif = -1; + + return err; +} + + +int camerasrc_support_jpeg_encoding(camsrc_handle_t handle, int *support_jpeg_encoding) +{ + int err = CAMERASRC_ERR_UNKNOWN; + + err = CAMERASRC_GET_CMD(_CAMERASRC_CMD_SUPPORT_JPEG_ENCODING, support_jpeg_encoding); + if (err != CAMERASRC_SUCCESS) { + *support_jpeg_encoding = 0; + } + + return err; +} + + +int camerasrc_get_screennail_buffer(camsrc_handle_t handle, camerasrc_buffer_t *scrnl_buf) +{ + camerasrc_handle_t *p = NULL; + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + if (scrnl_buf == NULL) { + camsrc_error("scrnl_buf is null"); + return CAMERASRC_ERR_INVALID_PARAMETER; + } + + p = CAMERASRC_HANDLE(handle); + + scrnl_buf->start = p->scrnl_buf.start; + scrnl_buf->length = p->scrnl_buf.length; + + camsrc_info("screennail ptr[%p],length[%d]", scrnl_buf->start, scrnl_buf->length); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_start_facedetection(camsrc_handle_t handle) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_FACEDETECTION, _CAMERASRC_FACEDETECTION_START); +} + + +int camerasrc_stop_facedetection(camsrc_handle_t handle) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_FACEDETECTION, _CAMERASRC_FACEDETECTION_STOP); +} + + +int camerasrc_get_facedetection(camsrc_handle_t handle, int *is_detecting) +{ + return CAMERASRC_GET_CMD(_CAMERASRC_CMD_FACEDETECTION, is_detecting); +} + + +int camerasrc_set_shutter_speed(camsrc_handle_t handle, camerasrc_frac_t frac) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_SHUTTER_SPEED, &frac); +} + + +int camerasrc_get_shutter_speed(camsrc_handle_t handle, camerasrc_frac_t *frac) +{ + return CAMERASRC_GET_CMD(_CAMERASRC_CMD_SHUTTER_SPEED, frac); +} + + +int camerasrc_set_exposure_value(camsrc_handle_t handle, camerasrc_frac_t frac) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_EXPOSURE_VALUE, &frac); +} + + +int camerasrc_get_exposure_value(camsrc_handle_t handle, camerasrc_frac_t *frac) +{ + return CAMERASRC_GET_CMD(_CAMERASRC_CMD_EXPOSURE_VALUE, frac); +} + + +int camerasrc_set_strobe_mode(camsrc_handle_t handle, camerasrc_strobe_mode_t mode) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_STROBE_MODE, &mode); +} + + +int camerasrc_get_strobe_mode(camsrc_handle_t handle, camerasrc_strobe_mode_t *mode) +{ + return CAMERASRC_GET_CMD(_CAMERASRC_CMD_STROBE_MODE, mode); +} + + +int camerasrc_set_control(camsrc_handle_t handle, camerasrc_ctrl_t ctrl_id, int value) +{ + _camerasrc_ctrl_t ctrl; + + CLEAR(ctrl); + + ctrl.cid = ctrl_id; + ctrl.value = value; + + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_CTRL, &ctrl); +} + + +int camerasrc_get_control(camsrc_handle_t handle, camerasrc_ctrl_t ctrl_id, int *value) +{ + int err = CAMERASRC_ERR_UNKNOWN; + _camerasrc_ctrl_t ctrl; + + CLEAR(ctrl); + + ctrl.cid = ctrl_id; + + err = CAMERASRC_GET_CMD(_CAMERASRC_CMD_CTRL, &ctrl); + if (err != CAMERASRC_SUCCESS) { + return err; + } + + *value = ctrl.value; + + return err; +} + + +int camerasrc_query_control(camsrc_handle_t handle, camerasrc_ctrl_t ctrl_id, camerasrc_ctrl_info_t *ctrl_info) +{ + struct v4l2_queryctrl queryctrl; + + if (ctrl_id < CAMERASRC_CTRL_BRIGHTNESS || ctrl_id >= CAMERASRC_CTRL_NUM) { + camsrc_warning("invalid ctrl_id [%d]", ctrl_id); + return CAMERASRC_ERR_INVALID_PARAMETER; + } + + CLEAR(queryctrl); + + queryctrl.id = _CAMERASRC_GET_CID(ctrl_id, CAMERASRC_HANDLE(handle)->cur_dev_id); + if (queryctrl.id == -1 ) { + camsrc_warning("not supported ctrl_id [%d]", ctrl_id); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + + if (0 == _camerasrc_ioctl( handle, VIDIOC_QUERYCTRL, &queryctrl)) { + if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) { + camsrc_info("ID [%8x] disabled", queryctrl.id); + } + + ctrl_info->v4l2_ctrl_id = queryctrl.id; + ctrl_info->camsrc_ctrl_id = ctrl_id; + + switch (queryctrl.type) { + case V4L2_CTRL_TYPE_INTEGER: + ctrl_info->ctrl_type = CTRL_TYPE_RANGE; + break; + case V4L2_CTRL_TYPE_BOOLEAN: + ctrl_info->ctrl_type = CTRL_TYPE_BOOL; + break; + case V4L2_CTRL_TYPE_MENU: + ctrl_info->ctrl_type = CTRL_TYPE_ARRAY; + _camerasrc_query_ctrl_menu (handle, queryctrl, ctrl_info); + break; + default: + ctrl_info->ctrl_type = CTRL_TYPE_UNKNOWN; + break; + } + + memcpy(ctrl_info->ctrl_name, queryctrl.name, MAX_SZ_CTRL_NAME_STRING); + ctrl_info->min = queryctrl.minimum; + ctrl_info->max = queryctrl.maximum; + ctrl_info->step = queryctrl.step; + ctrl_info->default_val = queryctrl.default_value; + + camsrc_info("Control name : %s, type %d, min %d, max %d, step %d, default %d", + queryctrl.name, queryctrl.type, + queryctrl.minimum, queryctrl.maximum, + queryctrl.step, queryctrl.default_value); + } else { + camsrc_error("VIDIOC_QUERYCTRL error"); + + return CAMERASRC_ERR_IO_CONTROL; + } + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_support_control(camsrc_handle_t handle, camerasrc_ctrl_t ctrl_id) +{ + return CAMERASRC_SUCCESS; +} + + +int _camerasrc_get_JPG_length(camerasrc_handle_t *handle) +{ + int jpg_length; + + CAMERASRC_GET_CMD(_CAMERASRC_CMD_JPEG_LENGTH, &jpg_length); + + return jpg_length; +} + + +int _camerasrc_get_thumbnail_JPG_length(camerasrc_handle_t *handle) +{ + int jpg_length; + + CAMERASRC_GET_CMD(_CAMERASRC_CMD_JPEG_THMBNL_LENGTH, &jpg_length); + + return jpg_length; +} + + +int _camerasrc_get_thumbnail_JPG_offset(camerasrc_handle_t *handle) +{ + int thumb_offset; + + CAMERASRC_GET_CMD(_CAMERASRC_CMD_JPEG_THMBNL_OFFSET, &thumb_offset); + + return thumb_offset; +} + + +int _camerasrc_get_screennail_JPG_length(camerasrc_handle_t *handle) +{ + int length; + + CAMERASRC_GET_CMD(_CAMERASRC_CMD_JPEG_SCRNL_LENGTH, &length); + + return length; +} + + +int _camerasrc_get_screennail_JPG_offset(camerasrc_handle_t *handle) +{ + int offset; + + CAMERASRC_GET_CMD(_CAMERASRC_CMD_JPEG_SCRNL_OFFSET, &offset); + + return offset; +} + + +int _camerasrc_set_thumbnail_size(camerasrc_handle_t *handle) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_JPEG_THMBNL_LENGTH, NULL); +} + + +int _camerasrc_check_emp_shock(camerasrc_handle_t *handle, int *check_val) +{ + return CAMERASRC_GET_CMD(_CAMERASRC_CMD_ESD_CHECK, check_val); +} + + +int _camerasrc_set_jpeg_compress_ratio(camerasrc_handle_t *handle, unsigned int quality) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_JPEG_COMPRESS_RATIO, &quality); +} + + +int _camerasrc_ioctl(camerasrc_handle_t *handle, int request, void *arg) +{ + if (dev_misc_func->_ioctl == NULL) { +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + } + + return dev_misc_func->_ioctl(handle, request, arg); +} + + +int _camerasrc_ioctl_once(camerasrc_handle_t *handle, int request, void *arg) +{ + if (dev_misc_func->_ioctl_once == NULL) { +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + } + + return dev_misc_func->_ioctl_once(handle, request, arg); +} + + +void *_camerasrc_run_autofocusing(camerasrc_handle_t *handle) +{ + if (dev_misc_func->_run_autofocusing != NULL) { + dev_misc_func->_run_autofocusing(handle); + } + + return NULL; +} + + +int _camerasrc_skip_frame(camerasrc_handle_t *handle, long int timeout, int skip_frame) +{ + if (dev_misc_func->_skip_frame == NULL) { +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + } + + return dev_misc_func->_skip_frame(handle, timeout, skip_frame); +} + +/**** A U T O F O C U S I N G F U N C T I O N S ****/ +int _camerasrc_set_autofocusing_area(camerasrc_handle_t *handle, camerasrc_rect_t *rect) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_AF_AREA, rect); +} + +int _camerasrc_get_autofocusing_area(camerasrc_handle_t *handle, camerasrc_rect_t *rect) +{ + return CAMERASRC_GET_CMD(_CAMERASRC_CMD_AF_AREA, rect); +} + +int _camerasrc_start_autofocusing(camerasrc_handle_t *handle) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_AF_CONTROL, _CAMERASRC_AF_START); +} + +int _camerasrc_stop_autofocusing(camerasrc_handle_t *handle) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_AF_CONTROL, _CAMERASRC_AF_STOP); +} + +int _camerasrc_release_autofocusing(camerasrc_handle_t *handle) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_AF_CONTROL, _CAMERASRC_AF_RELEASE); +} + +int _camerasrc_destroy_autofocusing(camerasrc_handle_t *handle) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_AF_CONTROL, _CAMERASRC_AF_DESTROY); +} + +int _camerasrc_init_autofocusing_mode(camerasrc_handle_t *handle) +{ + return CAMERASRC_SET_CMD(_CAMERASRC_CMD_AF_CONTROL, _CAMERASRC_AF_INIT); +} + + +int _camerasrc_get_frame_data(camerasrc_handle_t *handle, camerasrc_frame_data_t *data) +{ + return CAMERASRC_GET_CMD(_CAMERASRC_CMD_FRAME_DATA, data); +} + +int _camerasrc_get_exif_info(camerasrc_handle_t *handle, camerasrc_buffer_t *exif_string) +{ + return CAMERASRC_GET_CMD(_CAMERASRC_CMD_EXIF_INFO, exif_string); +} + + +int _camerasrc_copy_frame(camerasrc_handle_t *handle, camerasrc_buffer_t *src_buffer, camerasrc_buffer_t *dst_buffer, int isThumbnail) +{ + if (dev_misc_func->_copy_frame == NULL) { +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + } + + return dev_misc_func->_copy_frame(handle, src_buffer, dst_buffer, isThumbnail); +} + + + +static int _camerasrc_chk_opened(char *conf_filename) +{ + camsrc_info("enter"); + + char buf[CAMERASRC_MAX_FILENAME_LEN] = {'\0',}; + char proc_buf[CAMERASRC_MAX_FILENAME_LEN] = {'\0',}; + int fd = -1; + int len = 0; + int buf_len = 0; + int pid = 0; + char access[9]; + int err_fd = -1; + int err = CAMERASRC_SUCCESS; + + fd = open(conf_filename, O_RDONLY); + if (fd == -1 && errno == ENOENT) { + camsrc_info("File not exist!!, first open from this boot. open..."); + return CAMERASRC_SUCCESS; + } else if (fd == -1 && errno != ENOENT) { + camsrc_info("File open error. log file open error!"); + return CAMERASRC_ERR_INTERNAL; + } else { + camsrc_info("Dev has been opened. start check..."); + } + + len = read(fd, buf, CAMERASRC_MAX_FILENAME_LEN); + sscanf(buf, "%2d:%5d:%64s", &buf_len, &pid, access); + + camsrc_info("%d : %d : %s", buf_len, pid, access); + if (buf_len == 0) { + camsrc_info("Wrong written check file!"); + close(fd); + + return CAMERASRC_ERR_INTERNAL; + } + + if (!strcmp(access, CAMERASRC_DEV_RELEASED_STR)) { + camsrc_info("Has opened but closed properly!"); + close(fd); + + return CAMERASRC_SUCCESS; + } + + if (pid == getpid()) { + camsrc_info("Double open!! You can't go. Investigate the PID's FD list"); + close(fd); + + return CAMERASRC_ERR_DEVICE_BUSY; + } + + snprintf(proc_buf, CAMERASRC_MAX_FILENAME_LEN, "/proc/%d", pid); + err_fd = open(proc_buf, O_RDONLY | O_NONBLOCK, 0); + if (err_fd == -1) { + camsrc_info("The proc node [%s] are not exist it's fake node", proc_buf); + err = CAMERASRC_SUCCESS; + } else { + camsrc_info("The proc node [%s] are loaded. almost opened...", proc_buf); + close(err_fd); + + err = CAMERASRC_ERR_DEVICE_BUSY; + } + + close(fd); + + return err; +} + + +static int __camerasrc_open_device(camerasrc_handle_t *p) +{ + #define MAX_DEVICE_COUNT 4 + + int i = 0; + int ret = CAMERASRC_ERR_DEVICE_NOT_FOUND; + char device_name[MAX_SZ_DEV_NAME_STRING] = {'\0',}; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + struct v4l2_capability capability; + + for (i = 0 ; i < MAX_DEVICE_COUNT ; i++) { + snprintf(device_name, MAX_SZ_DEV_NAME_STRING, "%s%d", CAMERASRC_DEV_NODE_PREFIX, i); + camsrc_info("try to open %s", device_name); + + p->dev_fd = open(device_name, O_RDWR | O_NONBLOCK, 0); + if (p->dev_fd < 0) { + /* open failed */ + strerror_r(errno, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("Device open fail [%d][%s]", errno, err_msg); + + switch (errno) { + case EBUSY: + ret = CAMERASRC_ERR_DEVICE_BUSY; + break; + case ENOENT: + case ENODEV: + ret = CAMERASRC_ERR_DEVICE_NOT_FOUND; + break; + default: + ret = CAMERASRC_ERR_DEVICE_OPEN; + break; + } + + break; + } else { + int ioctl_ret = 0; + + camsrc_info("opened device fd %d", p->dev_fd); + + /* open success and check capabilities */ + ioctl_ret = _camerasrc_ioctl((camerasrc_handle_t *)p, VIDIOC_QUERYCAP, &capability); + if (ioctl_ret == CAMERASRC_SUCCESS) { + /* succeeded : VIDIOC_QUERYCAP */ + camsrc_info("capabilities %x", capability.capabilities); + if (capability.capabilities & V4L2_CAP_VIDEO_CAPTURE) { + camsrc_info("found capture device %s", device_name); + return CAMERASRC_SUCCESS; + } + } else { + camsrc_warning("VIDIOC_QUERYCAP error %x", ioctl_ret); + } + + close(p->dev_fd); + p->dev_fd = -1; + } + } + + camsrc_error("failed to find capture device %x", ret); + + return ret; +} + +static int _camerasrc_seamless_record_onaccess_open(char *conf_filename, camerasrc_handle_t *p) +{ + camsrc_info("enter"); + + int fd = -1; + int len = 0; + int ret = CAMERASRC_SUCCESS; + char buf[BUF_LEN]; + char tot_buf[BUF_LEN]; + camerasrc_sync_param_t syncparam = {CAMERASRC_SYNC_KEY_PATH, CAMERASRC_SYNC_KEY_PREFIX}; + + camsrc_lock_sync(&syncparam); + + if (p->dev_fd == -1) { + ret = __camerasrc_open_device(p); + if (ret != CAMERASRC_SUCCESS) { + camsrc_error("open failed %x", ret); + camsrc_unlock_sync(&syncparam); + return ret; + } + + p->is_async_open = 0; + } else { + p->is_async_open = 1; + } + + /* Write open status */ + fd = open(conf_filename, O_RDWR | O_NONBLOCK | O_CREAT, 0666); + if (fd == -1) { + camsrc_info("failed to open ", conf_filename); + /* Release camera device */ + close(p->dev_fd); + p->dev_fd = -1; + return CAMERASRC_ERR_INTERNAL; + } + + /** + * record like below format + * A : (integer) length of this info without this number and semicolon + * B : (integer) Process ID of process that open the device + * C : (string) Status of camera device. ONACCESS / RELEASED (9 words) + * format : "%d:%d:%s", A, B, C + */ + len = snprintf(buf, BUF_LEN, "%d:%s", getpid(), CAMERASRC_DEV_ON_ACCESS_STR); + len = snprintf(tot_buf, BUF_LEN, "%d:%s", len, buf); + tot_buf[len] = '\0'; + len = write(fd, tot_buf, len+1); + + close(fd); + fd = -1; + + camsrc_unlock_sync(&syncparam); + + return CAMERASRC_SUCCESS; +} + + +static int _camerasrc_seamless_delete_onaccess(char *conf_filename, camerasrc_handle_t *p) +{ + camsrc_info("enter"); + + int fd = p->dev_fd; + int chk_fd = 0; + int len = 0; + int err = CAMERASRC_ERR_UNKNOWN; + char buf[BUF_LEN]; + char tot_buf[BUF_LEN]; + camerasrc_sync_param_t param = {CAMERASRC_SYNC_KEY_PATH, CAMERASRC_SYNC_KEY_PREFIX}; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + camsrc_info("Start delete..."); + + /* TODO : Semaphore lock */ + camsrc_lock_sync(¶m); + + if (!(p->is_async_open)) { + close(fd); + } + + chk_fd = open(conf_filename, O_WRONLY | O_NONBLOCK); + if (chk_fd == -1) { + camsrc_info("FD = %d", fd); + return CAMERASRC_ERR_INTERNAL; + } + + len = snprintf(buf, BUF_LEN, "%d:%s", getpid(), CAMERASRC_DEV_RELEASED_STR); + camsrc_info("using string : %s", buf); + + len = snprintf(tot_buf, BUF_LEN, "%d:%s", len, buf); + camsrc_info("write string final : %s", tot_buf); + + len = write(chk_fd, tot_buf, len); + if (len == -1) { + strerror_r(errno, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("WRITE ERROR!!!!, error = %s, fd = %d", err_msg, chk_fd); + } + + close(chk_fd); + + camsrc_unlock_sync(¶m); + /* TODO : Semaphore unlock */ + err = CAMERASRC_SUCCESS; + camsrc_info("End delete..."); + + return err; +} + + +static int _camerasrc_initialize_handle(camerasrc_handle_t *handle) +{ + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + /* Initialize handle variables */ + handle->buffer = NULL; + handle->is_async_open = 0; + handle->dev_fd = -1; + handle->cur_dev_id = CAMERASRC_DEV_ID_UNKNOWN; + handle->check_esd = 0; + handle->is_preset = 0; + handle->resolution = 0; + handle->buffer_idx = 0; + handle->num_buffers = 0; + handle->buffer = NULL; + handle->present_buf = NULL; + handle->io_method = CAMERASRC_IO_METHOD_MMAP; + handle->af_status = CAMERASRC_AUTO_FOCUS_STATUS_RELEASED; + handle->af_cmd = CAMERASRC_AUTO_FOCUS_CMD_NULL; + handle->cur_af_mode = CAMERASRC_AF_MODE_AUTO; + handle->cur_af_range = CAMERASRC_AF_RANGE_NORMAL; + handle->af_usr_data = NULL; + handle->skip_frame_func = NULL; + handle->isAutoexposure = 1; + handle->af_cb = NULL; + handle->first_frame = 1; + handle->af_init_called = 0; + handle->hold_af_after_capturing = 0; + + if (gettimeofday(&handle->set_af_area_time, NULL) != 0) { + handle->set_af_area_time.tv_sec = 0; + handle->set_af_area_time.tv_usec = 0; + camsrc_warning("[init handle] gettimeofday Failed"); + } + + CLEAR(handle->queued_buf_list); + CLEAR(handle->format); + + return CAMERASRC_SUCCESS; +} + + +static int _camerasrc_wait_frame_available(camerasrc_handle_t *handle, long int timeout) +{ + camerasrc_handle_t *p = handle; + fd_set fds; + struct timeval tv; + int r; + +#if defined (USE_SKIP_FRAME) + int skip_frame = CAMERASRC_JPG_STILL_SKIP_FRAME; +#endif + + if (p->dev_fd == CAMERASRC_DEV_FD_EMERGENCY_CLOSED) { + /* Wait time FPS time */ + if (p->timeperframe.denominator == 0 && p->timeperframe.numerator == 0) { + usleep((int)1000000/30); /* supposed to 1/30 sec */ + } else { + usleep((int)(1000000 * p->timeperframe.numerator / p->timeperframe.denominator)); + } + + camsrc_info("Device emergency closed. wait will return proper time"); + + return CAMERASRC_SUCCESS; + } + + /** + * @note In preview or video mode, skip_frame_func, signaling_func will be NULL + * If unnecessary, just input them to NULL in start_still/preview/video_stream() + */ + + FD_ZERO(&fds); + FD_SET(p->dev_fd, &fds); + + /* skip frame */ +#if defined (USE_SKIP_FRAME) + if(p->skip_frame_func != NULL) { + camsrc_info("Skip frame function enabled!"); + err = p->skip_frame_func(p, timeout, skip_frame); + } +#endif + + /* set timeout */ + tv.tv_sec = (long int)timeout/1000; + tv.tv_usec = timeout * 1000 - tv.tv_sec * 1000000; + + /* select waiting */ + r = select(p->dev_fd + 1, &fds, NULL, NULL, &tv); + if (-1 == r) { + if (EINTR == errno) { + return CAMERASRC_SUCCESS; + } + camsrc_error("select() failed."); + + return CAMERASRC_ERR_INTERNAL; + } + + if (0 == r) { + return CAMERASRC_ERR_DEVICE_WAIT_TIMEOUT; + } + + return CAMERASRC_SUCCESS; +} + + +struct v4l2_buffer *_camerasrc_get_correct_buf(camerasrc_handle_t *handle, struct v4l2_buffer *queued_buf_list, camerasrc_buffer_t *buffer) +{ + unsigned int num_buffers = 0; + int cnt = 0; + camerasrc_get_num_buffer(handle, &num_buffers); + + for (cnt = 0 ; cnt < num_buffers ; cnt++) { + if (queued_buf_list[cnt].m.userptr == (unsigned long)buffer->start) { + return &(queued_buf_list[cnt]); + } + } + + return NULL; +} + + +static int _camerasrc_queue_buffer(camerasrc_handle_t *handle, int buf_index, camerasrc_buffer_t *buffer) +{ + camerasrc_handle_t *p = handle; + struct v4l2_buffer vbuf; + struct v4l2_buffer *pvbuf; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + if (p->dev_fd == CAMERASRC_DEV_FD_EMERGENCY_CLOSED) { + camsrc_info("Device emergency closed. Q will return successfully"); + return CAMERASRC_SUCCESS; + } + + if (buf_index >= p->num_buffers) { + camsrc_info("Late buffer won't be queued, buf_index = %d, p->num_buffers = %d", buf_index, p->num_buffers); + return CAMERASRC_SUCCESS; + } + + CLEAR(vbuf); + + switch (p->io_method) { + case CAMERASRC_IO_METHOD_MMAP: + if (p->buffer[buf_index].queued_status == CAMERASRC_BUFFER_QUEUED) { + camsrc_info("buffer[index:%d] is already queued. return success.", buf_index); + return CAMERASRC_SUCCESS; + } + + vbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + vbuf.memory = V4L2_MEMORY_MMAP; + vbuf.index = buf_index; + pvbuf = &vbuf; + break; + case CAMERASRC_IO_METHOD_USRPTR: + if (&(p->queued_buf_list[buf_index])) { + pvbuf = _camerasrc_get_correct_buf(handle, p->queued_buf_list, buffer); + } + + if (pvbuf == NULL) { + camsrc_error("Wrong address tried to queueing!, It'll be queued as input index.."); + pvbuf = &(p->queued_buf_list[buf_index]); + } + break; + case CAMERASRC_IO_METHOD_READ: + default: + camsrc_error("Device not support that io-method"); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + } + + if (CAMERASRC_STATE(handle) == CAMERASRC_STATE_READY) { + camsrc_warning("Q after ready state will success"); + return CAMERASRC_SUCCESS; + } + + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_QBUF, pvbuf)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("VIDIOC_QBUF failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + + LOCK(p); + if (p->io_method == CAMERASRC_IO_METHOD_MMAP) { + p->buffer[buf_index].queued_status = CAMERASRC_BUFFER_QUEUED; + p->queued_buffer_count++; + } + UNLOCK(p); + + return CAMERASRC_SUCCESS; +} + + +static int _camerasrc_get_proper_index(camerasrc_handle_t *handle, void *data) +{ + int i = 0; + + for (i = 0; i < handle->num_buffers; i++) { + if (handle->buffer[i].start == data) { + return i; + } + } + + camsrc_info("Index NOT found. new buffer... go to error"); + + return -1; +} + + +static int _camerasrc_dequeue_buffer(camerasrc_handle_t *handle, int *buf_index, camerasrc_buffer_t *buffer, camerasrc_buffer_t *thm_buffer) +{ + camerasrc_handle_t *p = handle; + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_buffer vbuf; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + if (p->dev_fd == CAMERASRC_DEV_FD_EMERGENCY_CLOSED) { + buf_index = 0; + buffer = &p->alter_frame; + buffer = NULL; + camsrc_info("Device emergency closed. DQ return fake frame"); + return CAMERASRC_SUCCESS; + } + + switch(p->io_method) { + case CAMERASRC_IO_METHOD_MMAP: + CLEAR(vbuf); + vbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + vbuf.memory = V4L2_MEMORY_MMAP; + break; + case CAMERASRC_IO_METHOD_USRPTR: + CLEAR(vbuf); + vbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + vbuf.memory = V4L2_MEMORY_USERPTR; + break; + case CAMERASRC_IO_METHOD_READ: + default: + camsrc_error("Device not support that io-method"); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + } + + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_DQBUF, &vbuf)) { + switch (p->errnum) { + case EAGAIN: + camsrc_info("VIDIOC_DQBUF [EAGAIN]"); + return CAMERASRC_SUCCESS; + case EIO: + /* Could ignore EIO, see spec. */ + /* fall through */ + camsrc_info("VIDIOC_DQBUF [EIO]"); + break; + case EINVAL: + camsrc_info("VIDIOC_DQBUF [EINVAL]"); + printf("VIDIOC_DQBUF [EINVAL]"); + return CAMERASRC_ERR_INVALID_VALUE; + default: + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("VIDIOC_DQBUF [%s]", err_msg); + break; + } + } + + /* A F I N I T */ + if (CAMERASRC_STATE(p) == CAMERASRC_STATE_PREVIEW && + p->first_frame && _CAMERASRC_SUPPORT_AF(p->cur_dev_id)) { + camsrc_info("AF init at first frame, hold_Af %d", p->hold_af_after_capturing); + if (!(p->af_init_called) || !(p->hold_af_after_capturing)) { + err = _camerasrc_init_autofocusing_mode(p); + if (err != CAMERASRC_SUCCESS) { + camsrc_warning("AF init failed"); + } else { + p->af_init_called = 1; + } + } else { + camsrc_info("AF init SKIP!"); + } + + p->first_frame = 0; + } + + if (CAMERASRC_STATE(p) == CAMERASRC_STATE_PREVIEW || CAMERASRC_STATE(p) == CAMERASRC_STATE_VIDEO || + (CAMERASRC_STATE(p) == CAMERASRC_STATE_STILL && p->format.colorspace == CAMERASRC_COL_RAW) || + (CAMERASRC_STATE(p) == CAMERASRC_STATE_AF_IN_PROGRESS && p->format.colorspace == CAMERASRC_COL_RAW)) { + switch(p->io_method) { + case CAMERASRC_IO_METHOD_MMAP: + *buf_index = vbuf.index; + buffer->start = p->buffer[vbuf.index].start; + LOCK(p); + p->buffer[vbuf.index].queued_status = CAMERASRC_BUFFER_DEQUEUED; + p->queued_buffer_count--; + UNLOCK(p); +#if 1 + switch (p->format.pix_format ) { + case CAMERASRC_PIX_YUV422P: + case CAMERASRC_PIX_YUY2: + case CAMERASRC_PIX_UYVY: + case CAMERASRC_PIX_RGB565: + buffer->length = YUV422_SIZE(p); + break; + case CAMERASRC_PIX_YUV420P: + case CAMERASRC_PIX_SN12: + case CAMERASRC_PIX_ST12: + case CAMERASRC_PIX_NV12: + buffer->length = YUV420_SIZE(p); + break; + case CAMERASRC_PIX_INTERLEAVED: + { + unsigned char *p_size_info = NULL; +#if 0 + if (vbuf.index == 2) { + FILE *fp = fopen("./test2.dump", "w+"); + fwrite(buffer->start, p->buffer[vbuf.index].length, 1, fp); + fflush(fp); + fclose(fp); + } +#endif + p_size_info = (unsigned char *)(buffer->start + (p->buffer[vbuf.index].length - ITLV_DATA_INFO_OFFSET)); + buffer->length = (unsigned int)(p_size_info[EMBEDDED_OFFSET]<<24) + (p_size_info[EMBEDDED_OFFSET+1]<<16) + (p_size_info[EMBEDDED_OFFSET+2]<<8) + p_size_info[EMBEDDED_OFFSET+3]; + camsrc_info("total %d, addr %x, data size %d", + (int)(p->buffer[vbuf.index].length), buffer->start, buffer->length); + break; + } + case CAMERASRC_PIX_RGGB10: + case CAMERASRC_PIX_RGGB8: + default: + buffer->length = -1; + break; + } +#else + buffer->length = vbuf.bytesused; +#endif + break; + case CAMERASRC_IO_METHOD_USRPTR: + { + int i = 0; + int dequeued = 0; + + for (i = 0; i < p->present_buf->num_buffer; ++i) { +#if defined (USE_USERPTR_DEBUG) + camsrc_info("buffers[%d].start: %p", i, p->buffer[i].start); + camsrc_info("buffers[%d].length: %d", i, p->buffer[i].length); + camsrc_info("buf.m.userptr: %p", vbuf.m.userptr); + camsrc_info("buf.length: %d", vbuf.length); +#endif + if (vbuf.m.userptr == (unsigned long) p->buffer[i].start) { + dequeued = 1; + break; + } + } + + if (!dequeued) { + camsrc_error("Dequeued buffer not matched to user ptr!"); + return CAMERASRC_ERR_IO_CONTROL; + } + + *buf_index = _camerasrc_get_proper_index(p, (void*)vbuf.m.userptr); + buffer->start = (unsigned char*)vbuf.m.userptr; + buffer->length = vbuf.length; + + break; + } + case CAMERASRC_IO_METHOD_READ: + default: + camsrc_error("Device not support that io-method"); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + break; + } + } else if (CAMERASRC_STATE(p) == CAMERASRC_STATE_STILL && p->format.colorspace == CAMERASRC_COL_JPEG) { + /* JPEG Capture */ + switch (p->io_method) { + case CAMERASRC_IO_METHOD_MMAP: + camsrc_info("JPEG frame dequeueing.."); + *buf_index = vbuf.index; + buffer->start = p->buffer[vbuf.index].start; + buffer->length = _camerasrc_get_JPG_length(p); + LOCK(p); + p->buffer[vbuf.index].queued_status = CAMERASRC_BUFFER_DEQUEUED; + p->queued_buffer_count--; + UNLOCK(p); + camsrc_info("JPEG frame length = %d", buffer->length); + /* If splitted thumbnail jpeg, it should know about both sizes */ + if (thm_buffer != NULL) { + if(p->format.pix_format == CAMERASRC_PIX_RGGB10) { + thm_buffer->start = p->buffer[vbuf.index].start + CAMERASRC_RAW_STILL_THUMBNAIL_OFFSET; + thm_buffer->length = CAMERASRC_YUV_THMBNL_SIZE; + camsrc_info("Thumbnail JPEG frame length = %d", thm_buffer->length); + } + else if(p->format.pix_format == CAMERASRC_PIX_RGGB8) { + unsigned int thumb_offset = 0, thumb_length = 0; + + thumb_offset = _camerasrc_get_thumbnail_JPG_offset(p); + thumb_length = _camerasrc_get_thumbnail_JPG_length(p); + + if( thumb_offset == 0 || thumb_length == 0 ) + { + thm_buffer->start = NULL; + thm_buffer->length = 0; + camsrc_info("Get Thumbnail JPEG frame failed.(offset:0x%x,length:%d)", thumb_offset, thumb_length); + } + else + { + thm_buffer->start = p->buffer[vbuf.index].start + thumb_offset; + thm_buffer->length = thumb_length; + camsrc_info("Thumbnail JPEG frame %p (offset 0x%x, length %d)", thm_buffer->start, thumb_offset, thumb_length); + } + } + else { + camsrc_error("Unknown image format... "); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + } + } + + /* Get screennail buffer */ + p->scrnl_buf.start = p->buffer[vbuf.index].start + _camerasrc_get_screennail_JPG_offset(p); + p->scrnl_buf.length = _camerasrc_get_screennail_JPG_length(p); + + /** + * In some module(or along firmware), it doesn't support jpg image + * splitted with thumbnail And its size always reported as bytesused field. + * "buffer->length == 0" means it won't support splitted jpg output + */ + camsrc_info("buffer->length = %d, vbuf.bytesused=%d", buffer->length, vbuf.bytesused); + if (buffer->length == 0) { + camsrc_info("it'll be copied as size = %d", vbuf.bytesused); + buffer->length = vbuf.bytesused; + } + + break; + case CAMERASRC_IO_METHOD_USRPTR: + { + int i = 0; + int dequeued = 0; + + for (i = 0; i < p->present_buf->num_buffer; ++i) { +#if defined (USE_USERPTR_DEBUG) + camsrc_info("buffers[%d].start: %d", i, p->buffer[i].start); + camsrc_info("buffers[%d].length: %d", i, p->buffer[i].length); + camsrc_info("buf.m.userptr: %d", vbuf.m.userptr); + camsrc_info("buf.length: %d", vbuf.length); +#endif + if (vbuf.m.userptr == (unsigned long) p->buffer[i].start) { + dequeued = 1; + break; + } + } + + if (!dequeued) { + camsrc_error("Dequeued buffer not matched to user ptr!"); + return CAMERASRC_ERR_IO_CONTROL; + } + + camsrc_info("JPEG frame dequeueing.."); + *buf_index = vbuf.index; + buffer->start = (unsigned char*)vbuf.m.userptr; + buffer->length = _camerasrc_get_JPG_length(p); + if (buffer->length == 0) { + return CAMERASRC_ERR_INVALID_PARAMETER; + } + camsrc_info("JPEG frame length = %d", buffer->length); + + if (thm_buffer != NULL) { + if (p->format.pix_format == CAMERASRC_PIX_RGGB10) { + /* JPEG + YUV thumbnail */ + thm_buffer->start = p->buffer[i].start + CAMERASRC_RAW_STILL_THUMBNAIL_OFFSET; + thm_buffer->length = CAMERASRC_YUV_THMBNL_SIZE; + camsrc_info("Thumbnail JPEG frame length = %d", thm_buffer->length); + } else if(p->format.pix_format == CAMERASRC_PIX_RGGB8) { + /* JPEG + JPEG thumbnail */ + thm_buffer->start = p->buffer[i].start + CAMERASRC_JPG_STILL_THUMBNAIL_OFFSET; + thm_buffer->length = _camerasrc_get_thumbnail_JPG_length(p); + if (buffer->length == 0) { + return CAMERASRC_ERR_INVALID_PARAMETER; + } + camsrc_info("Thumbnail JPEG frame length = %d", thm_buffer->length); + } else { + camsrc_error("Unknown image format... "); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + } + } + break; + } + case CAMERASRC_IO_METHOD_READ: + default: + camsrc_error("Device not support that io-method"); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + break; + } + } else { + camsrc_error("Invalid state transition"); + return CAMERASRC_ERR_INVALID_STATE; + } + + return CAMERASRC_SUCCESS; +} + +static int _camerasrc_dump_format(camsrc_handle_t handle); + +/**** M A I N O P E R A T I O N ****/ +int camerasrc_create(camsrc_handle_t *phandle) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + camsrc_info("enter"); + + if (phandle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = (camerasrc_handle_t *)malloc(sizeof(camerasrc_handle_t)); + if(p == NULL) { + camsrc_error("malloc fail"); + return CAMERASRC_ERR_ALLOCATION; + } + + memset(p, '\0', sizeof(camerasrc_handle_t)); + + /* STATE TO MEANINGFUL STATE */ + p->cur_state = CAMERASRC_STATE_NONE; + CAMERASRC_SET_STATE(p, CAMERASRC_STATE_CREATED); + CAMERASRC_SET_PHASE(p, CAMERASRC_PHASE_NON_RUNNING); + camsrc_info("Transit to non-running phase"); + + /* INIT VARIABLES */ + err = _camerasrc_initialize_handle(p); + if(err != CAMERASRC_SUCCESS) { + camsrc_error("Invalid handle"); + return CAMERASRC_ERR_INVALID_HANDLE; + } + + err = pthread_mutex_init(&(p->mutex), NULL); + if(err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("Mutex creating status : %s", err_msg); + } + + err = pthread_mutex_init(&(p->af_mutex), NULL); + if(err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("AF Mutex creating status : %s", err_msg); + } + + err = pthread_cond_init(&(p->af_wait_cond), NULL); + if(err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("AF Condition creating status : %s", err_msg); + } + + camsrc_info("Thread creation start.."); + err = pthread_create(&p->focusing_thread, NULL, (void*)_camerasrc_run_autofocusing, p); + if(err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("AF Thread creating status : %s", err_msg); + } + + camsrc_info("Thread creation end.."); + + *phandle = (camsrc_handle_t)p; + err = CAMERASRC_SUCCESS; + return err; +} + + +int camerasrc_destroy(camsrc_handle_t handle) +{ + camerasrc_handle_t* p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if(CAMERASRC_STATE(p) != CAMERASRC_STATE_UNREALIZED) { + camsrc_warning("Invalid state transition"); + } + + /* Remove AF thread */ + _camerasrc_destroy_autofocusing(p); + + camsrc_info("Thread join wait start.."); + err = pthread_join(p->focusing_thread, NULL); + if(err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("AF Thread join status : %s", err_msg); + } + + camsrc_info("Thread join wait end.."); + + err = pthread_cond_destroy(&p->af_wait_cond); + if(err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("AF Condition destroying error : %s", err_msg); + } + + err = pthread_mutex_destroy(&(p->af_mutex)); + if(err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("AF Mutex destroying error : %s", err_msg); + } + + err = pthread_mutex_destroy(&(p->mutex)); + if(err) { + strerror_r(err, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_info("Mutex destroying error : %s", err_msg); + } + + free((void*)p); + handle = NULL; + err = CAMERASRC_SUCCESS; + return err; +} + + +int camerasrc_close_device(camsrc_handle_t handle) +{ + camerasrc_handle_t* p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + p->is_async_open = 0; + +#if defined(USE_OPEN_CHK) + if(!CAMERASRC_IS_DEV_CLOSED(p)) { + err = _camerasrc_seamless_delete_onaccess(CAMERASRC_OPENED_CHK_FILENAME, p); + if(err != CAMERASRC_SUCCESS) { + return CAMERASRC_ERR_INTERNAL; + } + p->dev_fd = CAMERASRC_DEV_FD_EMERGENCY_CLOSED; + } +#else + if(!CAMERASRC_IS_DEV_CLOSED(p)) { + + if( !(p->is_async_open) ) { + close(p->dev_fd); + } + + p->dev_fd = CAMERASRC_DEV_FD_EMERGENCY_CLOSED; + } + /* prepare "Kagemusha" frame data */ + LOCK(p); + memcpy(p->alter_frame.start, p->buffer[0].start, p->alter_frame.length); + UNLOCK(p); +#endif + return CAMERASRC_SUCCESS; +} + + +int camerasrc_get_state(camsrc_handle_t handle, camerasrc_state_t* state) +{ + camerasrc_handle_t* p = NULL; + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + *state = CAMERASRC_STATE(p); + return CAMERASRC_SUCCESS; +} + + +int camerasrc_realize(camsrc_handle_t handle) +{ + camerasrc_handle_t* p = NULL; + int ret = CAMERASRC_SUCCESS; + + camerasrc_sync_param_t param = {CAMERASRC_SYNC_KEY_PATH, CAMERASRC_SYNC_KEY_PREFIX}; + mode_t mask = 0; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + /* STATE OR PHASE CHECKING */ + if(CAMERASRC_STATE(p) == CAMERASRC_STATE_REALIZED) { + return CAMERASRC_SUCCESS; + } + if(CAMERASRC_STATE(p) != CAMERASRC_STATE_CREATED && + CAMERASRC_STATE(p) != CAMERASRC_STATE_UNREALIZED) { + camsrc_warning("Invalid state transition"); + } + + mask = umask(0); + + /* CREATE SEMAPHORE */ + camsrc_create_sync(¶m); + + /* O P E N D E V I C E */ +#if defined(USE_OPEN_CHK) + ret = _camerasrc_chk_opened(CAMERASRC_OPENED_CHK_FILENAME); + if (ret == CAMERASRC_SUCCESS) { + ret = _camerasrc_seamless_record_onaccess_open(CAMERASRC_OPENED_CHK_FILENAME, p); + if (ret != CAMERASRC_SUCCESS) { + camsrc_error("_camerasrc_seamless_record_onaccess_open failed %x", ret); + } + } +#else /* USE_OPEN_CHK */ + if (!CAMERASRC_IS_DEV_CLOSED(p)) { + ret = __camerasrc_open_device(p); + if (ret != CAMERASRC_SUCCESS) { + camsrc_error("camera open failed %x", ret); + } else { + p->is_async_open = 0; + } + } else { + p->is_async_open = 1; + } +#endif /* USE_OPEN_CHK */ + + umask(mask); + + if (ret == CAMERASRC_SUCCESS) { + CAMERASRC_SET_STATE(p, CAMERASRC_STATE_REALIZED); + } + + return ret; +} + + +int camerasrc_unrealize(camsrc_handle_t handle) +{ + camerasrc_handle_t* p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if(CAMERASRC_STATE(p) == CAMERASRC_STATE_UNREALIZED || + CAMERASRC_STATE(p) == CAMERASRC_STATE_DESTROYED) { + return CAMERASRC_SUCCESS; + } + if(CAMERASRC_STATE(p) != CAMERASRC_STATE_READY) { + camsrc_warning("Invalid state transition"); + } + +#if defined(USE_OPEN_CHK) + if(!CAMERASRC_IS_DEV_CLOSED(p)) { + err = _camerasrc_seamless_delete_onaccess(CAMERASRC_OPENED_CHK_FILENAME, p); + if(err != CAMERASRC_SUCCESS) { + return CAMERASRC_ERR_INTERNAL; + } + } +#else /* USE_OPEN_CHK */ + if(!CAMERASRC_IS_DEV_CLOSED(p)) { + + if( !(p->is_async_open) ) { + close(p->dev_fd); + } + } +#endif /* USE_OPEN_CHK */ + + p->dev_fd = -1; + p->check_esd = 0; + + CAMERASRC_SET_STATE(p, CAMERASRC_STATE_UNREALIZED); + CAMERASRC_SET_PHASE(p, CAMERASRC_PHASE_NON_RUNNING); + camsrc_info("Transit to non-running phase"); + err = CAMERASRC_SUCCESS; + return err; +} + + +int camerasrc_start(camsrc_handle_t handle) +{ + camerasrc_handle_t* p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + /* STATE OR PHASE CHECKING */ + if(CAMERASRC_STATE(p) == CAMERASRC_STATE_READY) { + return CAMERASRC_SUCCESS; + } + if(CAMERASRC_STATE(p) != CAMERASRC_STATE_REALIZED) { + camsrc_warning("Invalid state transition"); + } + + CAMERASRC_SET_STATE(p, CAMERASRC_STATE_READY); + CAMERASRC_SET_PHASE(p, CAMERASRC_PHASE_RUNNING); + + camsrc_info("Transit to running phase"); + err = CAMERASRC_SUCCESS; + return err; +} + + +int camerasrc_present_usr_buffer(camsrc_handle_t handle, camerasrc_usr_buf_t *present_buf, camerasrc_io_method_t io_method) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_READY) { + camsrc_warning("Invalid state transition"); + } + + switch(io_method) { + case CAMERASRC_IO_METHOD_MMAP: + camsrc_info("MMAP mode"); + if (present_buf != NULL) { + camsrc_error("present_buf should be NULL in MMAP Mode"); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + } + p->io_method = CAMERASRC_IO_METHOD_MMAP; + break; + case CAMERASRC_IO_METHOD_USRPTR: + camsrc_info("USRPTR mode"); + if (present_buf == NULL) { + camsrc_error("present_buf should NOT be NULL in USRPTR Mode"); + return CAMERASRC_ERR_ALLOCATION; + } + + if (present_buf->num_buffer > CAMERASRC_USRPTR_MAX_BUFFER_NUM) { + camsrc_warning("Use up tp MAX %d buffer only", CAMERASRC_USRPTR_MAX_BUFFER_NUM); + present_buf->num_buffer = CAMERASRC_USRPTR_MAX_BUFFER_NUM; + } + p->present_buf = present_buf; + p->io_method = CAMERASRC_IO_METHOD_USRPTR; + break; + case CAMERASRC_IO_METHOD_READ: + default: +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + break; + } + + err = CAMERASRC_SUCCESS; + return err; +} + + +int camerasrc_get_num_buffer(camsrc_handle_t handle, unsigned int *num_buffer) +{ + camerasrc_handle_t *p = NULL; + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + camerasrc_state_t state = CAMERASRC_STATE_NONE; + camerasrc_get_state(handle, &state); + + switch(state) { + case CAMERASRC_STATE_NONE: + case CAMERASRC_STATE_CREATED: + case CAMERASRC_STATE_REALIZED: + case CAMERASRC_STATE_READY: + case CAMERASRC_STATE_UNREALIZED: + case CAMERASRC_STATE_DESTROYED: + camsrc_warning("Current buffer number not initialized."); + *num_buffer = 0; + break; + case CAMERASRC_STATE_PREVIEW: + case CAMERASRC_STATE_STILL: + case CAMERASRC_STATE_VIDEO: + case CAMERASRC_STATE_AF_IN_PROGRESS: + *num_buffer = p->num_buffers; + break; + default: + camsrc_error("Unknown state"); + *num_buffer = -1; + return CAMERASRC_ERR_INVALID_STATE; + } + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_get_io_method(camsrc_handle_t handle, camerasrc_io_method_t* io_method) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + *io_method = p->io_method; + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_start_still_stream(camsrc_handle_t handle) +{ + camerasrc_handle_t* p = NULL; + struct v4l2_streamparm vstreamparm; + struct v4l2_format vformat; + struct v4l2_requestbuffers vreq_bufs; + enum v4l2_buf_type vtype; + int cnt = 0; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) == CAMERASRC_STATE_STILL) { + return CAMERASRC_SUCCESS; + } + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_READY) { + camsrc_warning("Invalid state transition"); + } + + /* S T I L L M O D E S E T T I N G */ + /* + * If driver can control thmbnl size, we do that here, if not support, it's OK + * Driver should change jpeg compress ration before start stream + */ + CLEAR(vstreamparm); + vstreamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + __ta__( " Stillshot VIDIOC_G_PARM", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_G_PARM, &vstreamparm)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_G_PARAM failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + if (p->format.is_highquality_mode == 1) { + camsrc_info("High quality frame will be captured"); + vstreamparm.parm.capture.capturemode = V4L2_MODE_HIGHQUALITY; + } else if(p->format.is_highquality_mode == 0) { + camsrc_info("Normal quality frame will be captured"); + vstreamparm.parm.capture.capturemode = 0; + } else { + camsrc_info("Quality not initialized, or incorrect value. set high quality..."); + vstreamparm.parm.capture.capturemode = V4L2_MODE_HIGHQUALITY; + } + + vstreamparm.parm.capture.timeperframe.numerator = p->timeperframe.numerator; + vstreamparm.parm.capture.timeperframe.denominator = p->timeperframe.denominator; + camsrc_info("[FPS] timeperframe.numerator = %d", p->timeperframe.numerator); + camsrc_info("[FPS] timeperframe.denominator = %d", p->timeperframe.denominator); + camsrc_info("[QUA] vstreamparm.parm.capture.capturemode = %d", vstreamparm.parm.capture.capturemode); + __ta__( " Stillshot VIDIOC_S_PARM", + if(CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_S_PARM, &vstreamparm)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_S_PARAM failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + /* S T I L L F O R M A T S E T T I N G */ + CLEAR(vformat); + _camerasrc_dump_format(p); + vformat.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + switch (p->format.pix_format) { + case CAMERASRC_PIX_YUV420P: + camsrc_info("420P capture activated.."); + vformat.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; + break; + case CAMERASRC_PIX_YUV422P: + camsrc_info("422P capture activated.."); + vformat.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P; + break; + case CAMERASRC_PIX_SN12: + case CAMERASRC_PIX_NV12: + camsrc_info("NV12(SN12) capture activated.."); + vformat.fmt.pix.pixelformat = V4L2_PIX_FMT_NV12; + break; + case CAMERASRC_PIX_ST12: + camsrc_info("NV12T capture activated.."); + vformat.fmt.pix.pixelformat = V4L2_PIX_FMT_NV12T; + break; + case CAMERASRC_PIX_YUY2: + camsrc_info("YUY2 capture activated.."); + vformat.fmt.pix.pixelformat = CAMERASRC_V4L2_PREVIEW_PIX_FMT_DEFAULT; + break; + /* Kessler capture path */ + case CAMERASRC_PIX_RGGB8: + camsrc_info("JPEG+JPEG capture activated.."); + vformat.fmt.pix.pixelformat = CAMERASRC_V4L2_JPEG_CAPTURE_PIX_FMT_DEFAULT; + break; + case CAMERASRC_PIX_RGGB10: + camsrc_info("JPEG+YUV capture activated.."); + vformat.fmt.pix.pixelformat = CAMERASRC_V4L2_JPG_YUV_CAPTURE_PIX_FMT_DEFAULT; + break; + default: + camsrc_error("Not supported format!"); + break; + } + + camsrc_info("JPG unified size capture avtivated!"); + vformat.fmt.pix.width = p->format.img_size.dim.width; /*capture width*/ + vformat.fmt.pix.height = p->format.img_size.dim.height; /*capture height*/ + vformat.fmt.pix_mp.field = V4L2_FIELD_NONE; + __ta__( " Stillshot VIDIOC_S_FMT", + if(CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_S_FMT, &vformat)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_S_FMT failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + camsrc_info("Stillshot VIDIOC_S_FMT done"); + + CLEAR(vreq_bufs); + + /* M E M O R Y M A P P I N G */ + switch (p->io_method) { + case CAMERASRC_IO_METHOD_MMAP: + camsrc_info("MMAP mode"); + + if (vformat.fmt.pix.pixelformat == CAMERASRC_V4L2_JPEG_CAPTURE_PIX_FMT_DEFAULT ) { + vreq_bufs.count = CAMERASRC_STILL_BUFFER_NUM; + } else { + vreq_bufs.count = CAMERASRC_PREVIEW_BUFFER_NUM; + } + vreq_bufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + vreq_bufs.memory = V4L2_MEMORY_MMAP; + + camsrc_info("VIDIOC_REQBUFS count:%d, type:%d, memory:%d", + vreq_bufs.count, vreq_bufs.type, vreq_bufs.memory); + + __ta__( " Stillshot VIDIOC_REQBUFS", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_REQBUFS, &vreq_bufs)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_REQBUFS failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + camsrc_info("Stillshot VIDIOC_REQBUFS done"); + camsrc_assert(vreq_bufs.count >= 1); + + p->num_buffers = vreq_bufs.count; + p->buffer = calloc (vreq_bufs.count, sizeof (camerasrc_buffer_t)); + + if (!p->buffer) { + camsrc_error("calloc() failed"); + return CAMERASRC_ERR_ALLOCATION; + } + + __ta__( " Stillshot VIDIOC_QUERYBUF,mmap", + for (p->buffer_idx = 0 ; p->buffer_idx < vreq_bufs.count ; ++(p->buffer_idx)) { + struct v4l2_buffer vbuf; + CLEAR(vbuf); + + vbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + vbuf.memory = V4L2_MEMORY_MMAP; + vbuf.index = p->buffer_idx; + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_QUERYBUF, &vbuf)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_QUERYBUF failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + + p->buffer[p->buffer_idx].length = vbuf.length; + p->buffer[p->buffer_idx].start = mmap(NULL /* start anywhere */, + vbuf.length, + PROT_READ | PROT_WRITE, + MAP_SHARED, + p->dev_fd, vbuf.m.offset); + if (MAP_FAILED == p->buffer[p->buffer_idx].start) { + camsrc_error("mmap failed."); + return CAMERASRC_ERR_ALLOCATION; + } + + camsrc_info("MMAP BUF: addr[%p] size[%d]", + p->buffer[p->buffer_idx].start, p->buffer[p->buffer_idx].length); + } + ); + + camsrc_info("Stillshot VIDIOC_QUERYBUF and mmap done"); + break; + + case CAMERASRC_IO_METHOD_USRPTR: + camsrc_info("USRPTR mode"); + + vreq_bufs.count = p->present_buf->num_buffer; + vreq_bufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + vreq_bufs.memory = V4L2_MEMORY_USERPTR; + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_REQBUFS, &vreq_bufs)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_REQBUFS failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + + camsrc_assert(vreq_bufs.count >= 1); + + p->num_buffers = vreq_bufs.count; + p->buffer = calloc (vreq_bufs.count, sizeof (camerasrc_buffer_t)); + + if (!p->buffer) { + camsrc_error("calloc() failed"); + return CAMERASRC_ERR_ALLOCATION; + } + + if (!p->present_buf->present_buffer) { + camsrc_error("No Usrptr buffer presented!"); + return CAMERASRC_ERR_ALLOCATION; + } + + for (p->buffer_idx = 0; p->buffer_idx < vreq_bufs.count; ++(p->buffer_idx)) { + p->buffer[p->buffer_idx].length = p->present_buf->present_buffer[p->buffer_idx].length; + p->buffer[p->buffer_idx].start = p->present_buf->present_buffer[p->buffer_idx].start; + + if (!p->buffer[p->buffer_idx].start) { + camsrc_error("presented usrptr buffer is NULL"); + return CAMERASRC_ERR_ALLOCATION; + } + + camsrc_info("USERPTR BUF: addr[%p] size[%d]", + p->buffer[p->buffer_idx].start, p->buffer[p->buffer_idx].length); + } + break; + + case CAMERASRC_IO_METHOD_READ: + default: + camsrc_error("Device not support that io-method"); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + break; + } + + /* calibrate rotation - kernel of c210 could not support V4L2_CID_PHYSICAL_ROTATION */ + if (p->cur_dev_id == CAMERASRC_DEV_ID_SECONDARY) { + p->format.rotation += 180; + p->format.rotation = p->format.rotation % 360; + camsrc_info("Calibrate rotate : secondary camera +180 degree"); + } + + /* Rotation set */ + __ta__( " _CAMERASRC_CMD_ROTATION", + CAMERASRC_SET_CMD(_CAMERASRC_CMD_ROTATION, &(p->format.rotation)); + ); + + /* Sensor flip */ + __ta__( " _CAMERASRC_CMD_VFLIP", + CAMERASRC_SET_CMD(_CAMERASRC_CMD_VFLIP, &(p->vflip)); + ); + + __ta__( " _CAMERASRC_CMD_HFLIP", + CAMERASRC_SET_CMD(_CAMERASRC_CMD_HFLIP, &(p->hflip)); + ); + + /* set JPEG quality */ + if (p->format.pix_format == CAMERASRC_PIX_RGGB8) { + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_control control; + + control.id = V4L2_CID_CAM_JPEG_QUALITY; + control.value = p->format.quality; + camsrc_info("[VIDIOC_S_CTRL] V4L2_CID_CAM_JPEG_QUALITY -> %d", control.value); + __ta__( " Stillshot V4L2_CID_CAM_JPEG_QUALITY", + err = _camerasrc_ioctl(p, VIDIOC_S_CTRL, &control); + ); + if (err != CAMERASRC_SUCCESS) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("V4L2_CID_CAMERA_CAPTURE failed : %s", err_msg); + return err; + } + } + + /* A C T U A L S T A R T S T R E A M */ + switch(p->io_method) { + case CAMERASRC_IO_METHOD_MMAP: + camsrc_info("MMAP mode"); + + p->queued_buffer_count = 0; + for (cnt = 0; cnt < p->num_buffers; cnt++) { + struct v4l2_buffer lvbuf; + CLEAR(lvbuf); + + lvbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + lvbuf.memory = V4L2_MEMORY_MMAP; + lvbuf.index = cnt; + __ta__( " Stillshot VIDIOC_QBUF", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_QBUF, &lvbuf)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_QBUF failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + LOCK(p); + p->buffer[lvbuf.index].queued_status = CAMERASRC_BUFFER_QUEUED; + p->queued_buffer_count++; + UNLOCK(p); + } + + camsrc_info("Stillshot VIDIOC_QBUF done"); + + vtype = V4L2_BUF_TYPE_VIDEO_CAPTURE; + __ta__( " Stillshot VIDIOC_STREAMON", + if( CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_STREAMON, &vtype)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_STREAMON failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + camsrc_info("Stillshot VIDIOC_STREAMON done"); + break; + + case CAMERASRC_IO_METHOD_USRPTR: + camsrc_info("USRPTR mode"); + for (cnt = 0; cnt < p->present_buf->num_buffer; cnt++) { + struct v4l2_buffer lvbuf; + CLEAR(lvbuf); + + lvbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + lvbuf.memory = V4L2_MEMORY_USERPTR; + lvbuf.index = cnt; + lvbuf.m.userptr = (unsigned long)p->buffer[cnt].start; + lvbuf.length = p->buffer[cnt].length; + + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_QBUF, &lvbuf)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_QBUF failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + } + + camsrc_info("Stillshot VIDIOC_QBUF done"); + + vtype = V4L2_BUF_TYPE_VIDEO_CAPTURE; + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_STREAMON, &vtype)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_STREAMON failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + camsrc_info("Stillshot VIDIOC_STREAMON done"); + break; + + case CAMERASRC_IO_METHOD_READ: + default: + camsrc_error("Device not support that io-method"); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + break; + } + + /* C210 Legacy V4L2 API for capture */ + if (p->format.pix_format == CAMERASRC_PIX_RGGB8) { + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_control control; + + control.id = V4L2_CID_CAMERA_CAPTURE; + control.value = 0; + camsrc_info("[VIDIOC_S_CTRL] V4L2_CID_CAMERA_CAPTURE -> 0"); + __ta__( " Stillshot V4L2_CID_CAMERA_CAPTURE", + err = _camerasrc_ioctl(p, VIDIOC_S_CTRL, &control); + ); + if (err != CAMERASRC_SUCCESS) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("V4L2_CID_CAMERA_CAPTURE failed : %s", err_msg); + return err; + } + } + + + LOCK(p); + __ta__( " Stillshot skip_frame_func", + if (_CAMERASRC_NEED_MISC_FUNCTION(p->cur_dev_id, CAMERASRC_OP_CAPTURE, p->format.colorspace, CAMERASRC_MISC_SKIP_FRAME)) { + p->skip_frame_func = (camerasrc_skip_frame_func_t)_camerasrc_skip_frame; + } else { + p->skip_frame_func = NULL; + } + ); + UNLOCK(p); + +#if defined(USE_SKIP_FRAME_AT_RAW_FRAME) + /*< If YUV capture, */ + if (!p->format.is_highquality_mode) { + if (p->skip_frame_func != NULL) { + p->skip_frame_func(p, 5000, 3); + } + } +#endif + + CAMERASRC_SET_STATE(p, CAMERASRC_STATE_STILL); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_start_preview_stream(camsrc_handle_t handle) +{ + camerasrc_handle_t* p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + struct v4l2_streamparm vstreamparm; + struct v4l2_format vformat; + struct v4l2_requestbuffers vreq_bufs; + struct v4l2_control control; + enum v4l2_buf_type vtype; + int cnt = 0; + int preview_width = 0; + int preview_height = 0; +/* + int capture_width = 0; + int capture_height = 0; +*/ + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + /* STATE OR PHASE CHECKING */ + if(CAMERASRC_STATE(p) == CAMERASRC_STATE_PREVIEW) { + return CAMERASRC_SUCCESS; + } + if(CAMERASRC_STATE(p) != CAMERASRC_STATE_READY) { + camsrc_warning("Invalid state transition"); + } + + /* P R E V I E W M O D E S E T T I N G */ + CLEAR(vstreamparm); + vstreamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + __ta__( " VIDIOC_G_PARM", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_G_PARM, &vstreamparm)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_G_PARAM failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + vstreamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + + if ((vstreamparm.parm.capture.timeperframe.numerator != p->timeperframe.numerator) || + (vstreamparm.parm.capture.timeperframe.denominator != p->timeperframe.denominator) || + ((p->format.is_highquality_mode == 1) &&(vstreamparm.parm.capture.capturemode != V4L2_MODE_HIGHQUALITY)) || + ((p->format.is_highquality_mode == 0) &&(vstreamparm.parm.capture.capturemode != 0))) { + if (p->format.is_highquality_mode == 1) { + camsrc_info("High quality frame will be captured"); + vstreamparm.parm.capture.capturemode = V4L2_MODE_HIGHQUALITY; + } else if (p->format.is_highquality_mode == 0) { + camsrc_info("Normal quality frame will be captured"); + vstreamparm.parm.capture.capturemode = 0; + } else { + camsrc_info("Quality not initialized, or incorrect value. set normal quality..."); + vstreamparm.parm.capture.capturemode = 0; + } + + vstreamparm.parm.capture.timeperframe.numerator = p->timeperframe.numerator; + vstreamparm.parm.capture.timeperframe.denominator = p->timeperframe.denominator; + + camsrc_info("[FPS] timeperframe.numerator = %d", p->timeperframe.numerator); + camsrc_info("[FPS] timeperframe.denominator = %d", p->timeperframe.denominator); + camsrc_info("[QUA] vstreamparm.parm.capture.capturemode = %d", vstreamparm.parm.capture.capturemode); + + __ta__( " VIDIOC_S_PARM", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_S_PARM, &vstreamparm)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_S_PARAM failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + } + + /* P R E V I E W F O R M A T S E T T I N G */ + if (!(p->format.pix_format == CAMERASRC_PIX_YUV422P || p->format.pix_format == CAMERASRC_PIX_YUV420P || + p->format.pix_format == CAMERASRC_PIX_SN12 || p->format.pix_format == CAMERASRC_PIX_ST12 || + p->format.pix_format == CAMERASRC_PIX_NV12 || p->format.pix_format == CAMERASRC_PIX_YUY2 || + p->format.pix_format == CAMERASRC_PIX_UYVY || p->format.pix_format == CAMERASRC_PIX_INTERLEAVED) ) { + camsrc_error("Invalid output format."); + return CAMERASRC_ERR_INVALID_FORMAT; + } + + if (p->format.colorspace != CAMERASRC_COL_RAW) { + camsrc_error("Invalid store method."); + return CAMERASRC_ERR_INVALID_PARAMETER; + } + + err = _camerasrc_dump_format(handle); + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Format dump error"); + return err; + } + + preview_width = p->format.img_size.dim.width; + preview_height = p->format.img_size.dim.height; + + CLEAR(vformat); + vformat.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + vformat.fmt.pix.width = preview_width; + vformat.fmt.pix.height = preview_height; + vformat.fmt.pix.field = V4L2_FIELD_NONE; + + switch (p->format.pix_format) { + case CAMERASRC_PIX_YUV422P: + vformat.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P; + vformat.fmt.pix.bytesperline = preview_width << 1; + vformat.fmt.pix.sizeimage = (preview_width * preview_height) << 1; + break; + case CAMERASRC_PIX_YUY2: + vformat.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; + vformat.fmt.pix.bytesperline = preview_width << 1; + vformat.fmt.pix.sizeimage = (preview_width * preview_height) << 1; + break; + case CAMERASRC_PIX_UYVY: + vformat.fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY; + vformat.fmt.pix.bytesperline = preview_width << 1; + vformat.fmt.pix.sizeimage = (preview_width * preview_height) << 1; + break; + case CAMERASRC_PIX_YUV420P: + vformat.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420; + vformat.fmt.pix.bytesperline = (preview_width * 3) >> 1; + vformat.fmt.pix.sizeimage = (preview_width * preview_height * 3) >> 1; + break; + case CAMERASRC_PIX_NV12: + case CAMERASRC_PIX_SN12: + vformat.fmt.pix.pixelformat = V4L2_PIX_FMT_NV12; + vformat.fmt.pix.bytesperline = (preview_width * 3) >> 1; + vformat.fmt.pix.sizeimage = (preview_width * preview_height * 3) >> 1; + break; + case CAMERASRC_PIX_ST12: + vformat.fmt.pix.pixelformat = V4L2_PIX_FMT_NV12T; + vformat.fmt.pix.bytesperline = (preview_width * 3) >> 1; + vformat.fmt.pix.sizeimage = (preview_width * preview_height * 3) >> 1; + break; + case CAMERASRC_PIX_INTERLEAVED: /* JPEG + YUYV */ + vformat.fmt.pix.pixelformat = V4L2_PIX_FMT_INTERLEAVED; + vformat.fmt.pix.priv = V4L2_PIX_FMT_MODE_PREVIEW; + vformat.fmt.pix.field = IS_MODE_PREVIEW_STILL; + /* TODO: Set JPEG size */ + /* ... */ + break; + default: + camsrc_error("Invalid output format."); + break; + } + + camsrc_info("==========================================="); + camsrc_info("| Request output width = %d", vformat.fmt.pix.width); + camsrc_info("| Request output height = %d", vformat.fmt.pix.height); + camsrc_info("| Request output field = %d", vformat.fmt.pix.field); + camsrc_info("| Request output pixel format = %d", vformat.fmt.pix.pixelformat); + camsrc_info("| Request output bytesperline = %d", vformat.fmt.pix.bytesperline); + camsrc_info("| Request output image size = %d", vformat.fmt.pix.sizeimage); + camsrc_info("==========================================="); + + __ta__( " VIDIOC_S_FMT", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_S_FMT, &vformat)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_S_FMT failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + control.id = V4L2_CID_CACHEABLE; + if (p->format.pix_format == CAMERASRC_PIX_INTERLEAVED) { + control.value = 1; + } else { + control.value = 0; + } + camsrc_info("[VIDIOC_S_CTRL] V4L2_CID_CACHEABLE -> %d", control.value); + __ta__( " VIDIOC_S_CTRL: V4L2_CID_CACHEABLE", + err = _camerasrc_ioctl(p, VIDIOC_S_CTRL, &control); + ); + if (err != CAMERASRC_SUCCESS) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("V4L2_CID_CACHEABLE failed : %s", err_msg); + return err; + } + + control.id = V4L2_CID_EMBEDDEDDATA_ENABLE; + if (p->format.pix_format == CAMERASRC_PIX_INTERLEAVED) { + control.value = 1; + } else { + control.value = 0; + } + camsrc_info("[VIDIOC_S_CTRL] V4L2_CID_EMBEDDEDDATA_ENABLE -> %d", control.value); + __ta__( " VIDIOC_S_CTRL: V4L2_CID_EMBEDDEDDATA_ENABLE", + err = _camerasrc_ioctl(p, VIDIOC_S_CTRL, &control); + ); + if (err != CAMERASRC_SUCCESS) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("V4L2_CID_EMBEDDEDDATA_ENABLE failed : %s", err_msg); + return err; + } + + /* Prepare alter format frame */ + __ta__( " camerasrc_query_img_buf_size", + camerasrc_query_img_buf_size(p, &(p->alter_frame.length), NULL); + ); + p->alter_frame.start = (unsigned char*) malloc(p->alter_frame.length); + if (p->alter_frame.start == NULL) { + camsrc_error("alter_frame alloc failed."); + return CAMERASRC_ERR_ALLOCATION; + } + + /* M E M O R Y M A P P I N G */ + CLEAR(vreq_bufs); + switch(p->io_method) { + case CAMERASRC_IO_METHOD_MMAP: + camsrc_info("MMAP mode"); + vreq_bufs.count = CAMERASRC_PREVIEW_BUFFER_NUM; + vreq_bufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + vreq_bufs.memory = V4L2_MEMORY_MMAP; + camsrc_info("MMAP mode (bufs.count:%d)", vreq_bufs.count); + __ta__( " VIDIOC_REQBUFS", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_REQBUFS, &vreq_bufs)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_REQBUFS failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + camsrc_assert(vreq_bufs.count >= 1); + + p->num_buffers = vreq_bufs.count; + p->buffer = calloc (vreq_bufs.count, sizeof (camerasrc_buffer_t)); + if (!p->buffer) { + camsrc_error("calloc() failed"); + return CAMERASRC_ERR_ALLOCATION; + } + + for (p->buffer_idx = 0; p->buffer_idx < vreq_bufs.count; ++(p->buffer_idx)) { + struct v4l2_buffer vbuf; + CLEAR(vbuf); + vbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + vbuf.memory = V4L2_MEMORY_MMAP; + vbuf.index = p->buffer_idx; + + __ta__( " VIDIOC_QUERYBUF", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_QUERYBUF, &vbuf)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_QUERYBUF failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + __ta__( " mmap", + p->buffer[p->buffer_idx].length = vbuf.length; + p->buffer[p->buffer_idx].start = mmap(NULL /* start anywhere */, + vbuf.length, + PROT_READ | PROT_WRITE, + MAP_SHARED, + p->dev_fd, vbuf.m.offset); + ); + if (MAP_FAILED == p->buffer[p->buffer_idx].start) { + camsrc_error("mmap failed."); + return CAMERASRC_ERR_ALLOCATION; + } + + GST_INFO("buffer index %d, addr %x, length %d", + p->buffer_idx, p->buffer[p->buffer_idx].start, p->buffer[p->buffer_idx].length); + + switch (p->format.pix_format) { + case CAMERASRC_PIX_YUV422P: + case CAMERASRC_PIX_YUY2: + case CAMERASRC_PIX_UYVY: + if (p->buffer[p->buffer_idx].length < p->format.img_size.dim.width * p->format.img_size.dim.height * 2) { + camsrc_error("format[%d] device insufficient memory.", p->format.pix_format); + } + break; + case CAMERASRC_PIX_YUV420P: + case CAMERASRC_PIX_NV12: + if (p->buffer[p->buffer_idx].length < (p->format.img_size.dim.width * p->format.img_size.dim.height * 3 / 2)) { + camsrc_error("format[%d] device insufficient memory.", p->format.pix_format); + } + break; + case CAMERASRC_PIX_SN12: + case CAMERASRC_PIX_ST12: + if (p->buffer[p->buffer_idx].length < (p->format.img_size.dim.width * p->format.img_size.dim.height)) { + camsrc_error("SN12,ST12: device insufficient memory."); + } + break; + case CAMERASRC_PIX_INTERLEAVED: + if (p->buffer[p->buffer_idx].length < (p->format.img_size.dim.width * p->format.img_size.dim.height)) { + camsrc_error("INTERLEAVED: device insufficient memory."); + } + break; + default: + camsrc_error("Not supported format[%d]", p->format.pix_format); + break; + } + + camsrc_info("MMAP BUF: addr[%p] size[%d]", + p->buffer[p->buffer_idx].start, p->buffer[p->buffer_idx].length); + } + break; + case CAMERASRC_IO_METHOD_USRPTR: + camsrc_info("USRPTR mode"); + + vreq_bufs.count = p->present_buf->num_buffer; + vreq_bufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + vreq_bufs.memory = V4L2_MEMORY_USERPTR; + __ta__( " VIDIOC_REQBUFS", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_REQBUFS, &vreq_bufs)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_REQBUFS failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + camsrc_assert(vreq_bufs.count >= 1); + + p->num_buffers = vreq_bufs.count; + p->buffer = calloc (vreq_bufs.count, sizeof (camerasrc_buffer_t)); + + if(!p->buffer) { + camsrc_error("calloc() failed"); + return CAMERASRC_ERR_ALLOCATION; + } + + if(!p->present_buf->present_buffer) { + camsrc_error("No Usrptr buffer presented!"); + return CAMERASRC_ERR_ALLOCATION; + } + + for (p->buffer_idx = 0; p->buffer_idx < vreq_bufs.count; ++(p->buffer_idx)) { + p->buffer[p->buffer_idx].length = p->present_buf->present_buffer[p->buffer_idx].length; + p->buffer[p->buffer_idx].start = p->present_buf->present_buffer[p->buffer_idx].start; + + if (!p->buffer[p->buffer_idx].start) { + camsrc_error("presented usrptr buffer is NULL"); + return CAMERASRC_ERR_ALLOCATION; + } + + camsrc_info("USERPTR BUF: addr[%p] size[%d]", + p->buffer[p->buffer_idx].start, p->buffer[p->buffer_idx].length); + } + break; + case CAMERASRC_IO_METHOD_READ: + default: + camsrc_error("Device not support that io-method"); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + break; + } + + /* A C T U A L S T A R T S T R E A M */ + switch(p->io_method) { + case CAMERASRC_IO_METHOD_MMAP: + camsrc_info("MMAP mode"); + + p->queued_buffer_count = 0; + for (cnt = 0; cnt < p->num_buffers; cnt++) { + struct v4l2_buffer lvbuf; + CLEAR(lvbuf); + + lvbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + lvbuf.memory = V4L2_MEMORY_MMAP; + lvbuf.index = cnt; + __ta__( " VIDIOC_QBUF", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_QBUF, &lvbuf)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_QBUF failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + LOCK(p); + p->buffer[lvbuf.index].queued_status = CAMERASRC_BUFFER_QUEUED; + p->queued_buffer_count++; + UNLOCK(p); + } + break; + case CAMERASRC_IO_METHOD_USRPTR: + camsrc_info("USRPTR mode"); + + for (cnt = 0; cnt < p->present_buf->num_buffer; cnt++) { + struct v4l2_buffer lvbuf; + CLEAR(lvbuf); + + lvbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + lvbuf.memory = V4L2_MEMORY_USERPTR; + lvbuf.index = cnt; + lvbuf.m.userptr = (unsigned long)p->buffer[cnt].start; + lvbuf.length = p->buffer[cnt].length; + __ta__( " VIDIOC_QBUF", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_QBUF, &lvbuf)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_QBUF failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + camsrc_info("QBUF Address [%d] %p", lvbuf.index, (void*)lvbuf.m.userptr); + + memcpy(&(p->queued_buf_list[cnt]), &lvbuf, sizeof(struct v4l2_buffer)); + } + break; + case CAMERASRC_IO_METHOD_READ: + default: + camsrc_error("Device not support that io-method"); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + break; + } + + /* calibrate rotation - kernel of c210 could not support V4L2_CID_PHYSICAL_ROTATION */ + if (p->cur_dev_id == CAMERASRC_DEV_ID_SECONDARY) { + p->format.rotation += 180; + p->format.rotation = p->format.rotation % 360; + camsrc_info("Calibrate rotate : secondary camera +180 degree"); + } + + /* Rotation set */ + __ta__( " _CAMERASRC_CMD_ROTATION", + CAMERASRC_SET_CMD(_CAMERASRC_CMD_ROTATION, &(p->format.rotation)); + ); +#if USE_SENSOR_MODE + /* Sensor mode Set */ + __ta__( " _CAMERASRC_CMD_SENSOR_MODE", + CAMERASRC_SET_CMD(_CAMERASRC_CMD_SENSOR_MODE, &(p->sensor_mode)); + ); +#endif + /* Sensor flip */ + __ta__( " _CAMERASRC_CMD_VFLIP", + CAMERASRC_SET_CMD(_CAMERASRC_CMD_VFLIP, &(p->vflip)); + ); + __ta__( " _CAMERASRC_CMD_HFLIP", + CAMERASRC_SET_CMD(_CAMERASRC_CMD_HFLIP, &(p->hflip)); + ); + + vtype = V4L2_BUF_TYPE_VIDEO_CAPTURE; + __ta__( " VIDIOC_STREAMON", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_STREAMON, &vtype)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_STREAMON failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + /* For noti that it is first frame DQ */ + p->first_frame = 1; + + LOCK(p); + if (_CAMERASRC_NEED_MISC_FUNCTION(p->cur_dev_id, CAMERASRC_OP_PREVIEW, p->format.colorspace, CAMERASRC_MISC_SKIP_FRAME)) { + p->skip_frame_func = (camerasrc_skip_frame_func_t)_camerasrc_skip_frame; + } else { + p->skip_frame_func = NULL; + } + UNLOCK(p); + +#if defined(USE_SKIP_FRAME_AT_RAW_FRAME) + if(p->skip_frame_func != NULL) + p->skip_frame_func(p, 5000, 3); +#endif + + for (cnt = 0; cnt < p->num_buffers; cnt++) { + camsrc_info("MMAP/USRPTR BUF: addr[%p] size[%d]", p->buffer[cnt].start, p->buffer[cnt].length); + } + + CAMERASRC_SET_STATE(p, CAMERASRC_STATE_PREVIEW); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_query_img_buf_size(camsrc_handle_t handle, unsigned int* main_img_size, unsigned int* thm_img_size) +{ + camerasrc_handle_t* p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_STILL && + CAMERASRC_STATE(p) != CAMERASRC_STATE_PREVIEW && + CAMERASRC_STATE(p) != CAMERASRC_STATE_VIDEO) { + camsrc_warning("Invalid state transition"); + } + + camsrc_info("Start to set format"); + + if (p->format.colorspace == CAMERASRC_COL_RAW && + (p->format.pix_format == CAMERASRC_PIX_YUV422P || + p->format.pix_format == CAMERASRC_PIX_YUY2 || + p->format.pix_format == CAMERASRC_PIX_UYVY || + p->format.pix_format == CAMERASRC_PIX_INTERLEAVED)) { + *main_img_size = p->format.img_size.dim.width * p->format.img_size.dim.height * 2; + if (thm_img_size != NULL) { + *thm_img_size = 0; + } + camsrc_info("422 RAW!! WIDTH = %d, HEIGHT=%d, SIZE = %d", + p->format.img_size.dim.width, p->format.img_size.dim.height, *main_img_size); + } else if (p->format.colorspace == CAMERASRC_COL_RAW && + (p->format.pix_format == CAMERASRC_PIX_YUV420P || + p->format.pix_format == CAMERASRC_PIX_SN12 || + p->format.pix_format == CAMERASRC_PIX_ST12 || + p->format.pix_format == CAMERASRC_PIX_NV12)) { + /*Kessler preview path*/ + *main_img_size = p->format.img_size.dim.width * p->format.img_size.dim.height * 3 / 2; + if (thm_img_size != NULL) { + *thm_img_size = 0; + } + camsrc_info("420 RAW!! WIDTH = %d, HEIGHT=%d, SIZE = %d", + p->format.img_size.dim.width, p->format.img_size.dim.height, *main_img_size); + } else if (p->format.colorspace == CAMERASRC_COL_JPEG) { + if (p->format.pix_format == CAMERASRC_PIX_RGGB10) { + camsrc_info("JPG + YUV Mode enabled."); + *main_img_size = CAMERASRC_JPG_STILL_H_SYNC * CAMERASRC_JPG_STILL_V_SYNC; + if (thm_img_size != NULL) { + *thm_img_size = 320 * 240 * 2; + } + } else if (p->format.pix_format == CAMERASRC_PIX_RGGB8) { + /*Kessler capture path*/ + camsrc_info("JPG + JPG Mode enabled."); + *main_img_size = CAMERASRC_JPG_STILL_H_SYNC * CAMERASRC_JPG_STILL_V_SYNC; + if (thm_img_size != NULL) { + *thm_img_size = CAMERASRC_JPG_STILL_H_SYNC * CAMERASRC_JPG_STILL_V_SYNC - CAMERASRC_JPG_STILL_THUMBNAIL_OFFSET; + } + } else { + camsrc_error("Unknown image format..."); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + } + + camsrc_info("JPEG!! WIDTH = %d, HEIGHT=%d, SIZE = %d", + p->format.img_size.dim.width, p->format.img_size.dim.height, *main_img_size); + } else { + camsrc_error("Unknown format set. can't go any more"); + camsrc_assert(0); + } + + camsrc_info("leave.."); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_stop_stream(camsrc_handle_t handle) +{ + camerasrc_handle_t *p = NULL; + enum v4l2_buf_type type; + int cnt = 0; + int try_count = 0; + camerasrc_auto_focus_status_t af_status = 0; + struct v4l2_requestbuffers vreq_bufs; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) == CAMERASRC_STATE_READY) { + return CAMERASRC_SUCCESS; + } + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_STILL && + CAMERASRC_STATE(p) != CAMERASRC_STATE_PREVIEW && + CAMERASRC_STATE(p) != CAMERASRC_STATE_VIDEO && + CAMERASRC_STATE(p) != CAMERASRC_STATE_AF_IN_PROGRESS) { + camsrc_warning("Stop stream called [STREAM-NOT-STARTED STATE]"); + } + + camsrc_info("Change to READY state first for preventing to check Q/DQ after stop"); + CAMERASRC_SET_STATE(p, CAMERASRC_STATE_READY); + + LOCK(p); + p->timeperframe.denominator = 0; + p->timeperframe.numerator = 0; + p->first_frame = 1; + UNLOCK(p); + +#ifdef STOP_AF_BEFORE_STREAMOFF + _camerasrc_stop_autofocusing(p); + + /* Wait for AF stop done */ + try_count = 0; + __ta__( " camerasrc_stop_stream:Wait for AF release", + while (try_count++ < CAMERASRC_AF_TOTALTIME/CAMERASRC_AF_STOP_INTERVAL) { + usleep( CAMERASRC_AF_STOP_INTERVAL ); + camerasrc_get_autofocusing_status(p, &af_status); + if (af_status == CAMERASRC_AUTO_FOCUS_STATUS_RELEASED) { + camsrc_info("AF Stop done"); + break; + } + } + ); +#endif /* STOP_AF_BEFORE_STREAMOFF */ + + /* S T O P S T R E A M */ + type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + __ta__( " camerasrc_stop_stream:VIDIOC_STREAMOFF", + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_STREAMOFF, &type)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_STREAMOFF failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + ); + + /* M E M O R Y U N M A P P I N G & F R E E */ + switch(p->io_method) { + case CAMERASRC_IO_METHOD_MMAP: + camsrc_info("Init io method to MMAP mode"); + for (cnt = 0; cnt < p->num_buffers; ++cnt) { + if (-1 == munmap (p->buffer[cnt].start,p->buffer[cnt].length)) { + camsrc_error("MUNMAP failed."); + return CAMERASRC_ERR_INTERNAL; + } + + p->buffer[cnt].start = NULL; + p->buffer[cnt].length = 0; + } + break; + case CAMERASRC_IO_METHOD_USRPTR: + camsrc_info("User ptr mode don't need munmap"); + break; + case CAMERASRC_IO_METHOD_READ: + default: + camsrc_error("Device not support that io-method"); +#if USE_NOT_SUPPORT_ERR + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; +#else + return CAMERASRC_SUCCESS; +#endif + } + + /* release buffer */ + CLEAR(vreq_bufs); + vreq_bufs.count = 0; + vreq_bufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + vreq_bufs.memory = V4L2_MEMORY_MMAP; + if (CAMERASRC_SUCCESS != _camerasrc_ioctl(p, VIDIOC_REQBUFS, &vreq_bufs)) { + strerror_r(p->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_REQBUFS failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + + /* RETURN BUFFER MANAGE MODE TO DEFAULT */ + p->io_method = CAMERASRC_IO_METHOD_MMAP; + p->present_buf = NULL; + + if (p->buffer != NULL) { + free(p->buffer); + } + + if (p->alter_frame.start != NULL) { + free(p->alter_frame.start); + } + + p->buffer = NULL; + p->alter_frame.start = NULL; + + p->buffer_idx = 0; + p->num_buffers = 0; + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_wait_frame_available(camsrc_handle_t handle, long int timeout) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_STILL && + CAMERASRC_STATE(p) != CAMERASRC_STATE_PREVIEW && + CAMERASRC_STATE(p) != CAMERASRC_STATE_VIDEO && + CAMERASRC_STATE(p) != CAMERASRC_STATE_AF_IN_PROGRESS) { + camsrc_warning("Invalid state transition" ); + } + + if ( p->queued_buffer_count > 0 ) { + /*camsrc_info( CAMERASRC, "Current queue count : %d", p->queued_buffer_count );*/ + err = _camerasrc_wait_frame_available(p, timeout); + } else { + camsrc_warning("queued_buffer_count[%d] is unavailable. sleep 100 ms and try again...", p->queued_buffer_count ); + usleep( 100000 ); + err = CAMERASRC_ERR_INVALID_STATE; + } + + return err; +} + + +int camerasrc_check_esd_shock(camsrc_handle_t *handle, int *check_val) +{ + camerasrc_handle_t* p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + printf("start _camerasrc_check_emp_shock"); + *check_val = 0; + err = _camerasrc_check_emp_shock(p, check_val); + printf("check_val = %d", *check_val); + printf("end _camerasrc_check_emp_shock"); + + return err; +} + + +int camerasrc_queue_buffer(camsrc_handle_t handle, int buf_index, camerasrc_buffer_t *buffer) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_STILL && + CAMERASRC_STATE(p) != CAMERASRC_STATE_PREVIEW && + CAMERASRC_STATE(p) != CAMERASRC_STATE_VIDEO && + CAMERASRC_STATE(p) != CAMERASRC_STATE_AF_IN_PROGRESS ) { + camsrc_warning("Invalid state transition"); + } + + err = _camerasrc_queue_buffer(p, buf_index, buffer); + + return err; +} + + +int camerasrc_dequeue_buffer(camsrc_handle_t handle, int *buf_index, camerasrc_buffer_t *buffer, camerasrc_buffer_t *thm_buffer) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_STILL && + CAMERASRC_STATE(p) != CAMERASRC_STATE_PREVIEW && + CAMERASRC_STATE(p) != CAMERASRC_STATE_VIDEO && + CAMERASRC_STATE(p) != CAMERASRC_STATE_AF_IN_PROGRESS) { + camsrc_warning("Invalid state transition"); + } + + err = _camerasrc_dequeue_buffer(p, buf_index, buffer, thm_buffer); + + return err; +} + + +int camerasrc_read_frame(camsrc_handle_t handle, camerasrc_buffer_t *main_img_buffer, camerasrc_buffer_t *thm_img_buffer, int *buffer_index) +{ + camerasrc_handle_t* p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("ENTER"); + + if (!handle || !main_img_buffer || !thm_img_buffer || !buffer_index) { + camsrc_error("handle(%p)main(%p)thm(%p)index(%p) is null", + handle, main_img_buffer, thm_img_buffer, buffer_index); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_STILL && + CAMERASRC_STATE(p) != CAMERASRC_STATE_PREVIEW && + CAMERASRC_STATE(p) != CAMERASRC_STATE_VIDEO && + CAMERASRC_STATE(p) != CAMERASRC_STATE_AF_IN_PROGRESS) { + camsrc_warning("Invalid state transition"); + } + +#ifndef DUMMY_OUTPUT + __ta__( " Stillshot select()", + err = _camerasrc_wait_frame_available(p, CAMERASRC_TIMEOUT_CRITICAL_VALUE); + ); + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Frame waiting error, [%x]", err); + return err; + } + + /* Buffer DQ */ + __ta__( " Stillshot VIDIOC_DQBUF", + err = _camerasrc_dequeue_buffer(p, buffer_index, main_img_buffer, thm_img_buffer); + ); + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Dequeue frame error, [%x]", err); + return err; + } +#endif + + camsrc_info("DEQUEUED Index : %d", *buffer_index); + +#if defined(USE_CAMERASRC_FRAME_DUMP) + printf("******************in buf len = %d", inner_main_buffer.length); + write_buffer_into_path(&inner_main_buffer, CAMERASRC_FRAME_DUMP_PATH, "main.jpg"); +#endif + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_set_focused_callback(camsrc_handle_t handle, camerasrc_callback_t cb, void *usr_data) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_REALIZED && + CAMERASRC_STATE(p) != CAMERASRC_STATE_READY) { + camsrc_warning("Invalid state transition"); + } + + LOCK(p); + p->af_cb = cb; + if (usr_data != NULL) { + p->af_usr_data = usr_data; + } + UNLOCK(p); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_set_autofocusing_area(camsrc_handle_t handle, camerasrc_rect_t *rect) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_PHASE(p) != CAMERASRC_PHASE_RUNNING) { + camsrc_warning("Invalid state transition"); + } + + err = _camerasrc_set_autofocusing_area(p, rect); + + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Set autofocusing area error"); + return err; + } + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_get_autofocusing_area(camsrc_handle_t handle, camerasrc_rect_t *rect) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_PHASE(p) != CAMERASRC_PHASE_RUNNING) { + camsrc_warning("Invalid state transition"); + } + + err = _camerasrc_get_autofocusing_area(p, rect); + + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Get autofocusing area error"); + return err; + } + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_start_autofocusing(camsrc_handle_t handle) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (!(CAMERASRC_STATE(p) == CAMERASRC_STATE_PREVIEW || + CAMERASRC_STATE(p) == CAMERASRC_STATE_VIDEO || + CAMERASRC_STATE(p) == CAMERASRC_STATE_AF_IN_PROGRESS)) { + camsrc_error("Invalid state [%d]", CAMERASRC_STATE(p)); + return CAMERASRC_ERR_INVALID_STATE; + } + + /* START AF */ + err = _camerasrc_start_autofocusing(p); + + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Create autofocusing thread error"); + return err; + } + + camsrc_info("Set state to [AF-IN-PROGRESS]!!"); + CAMERASRC_SET_STATE(p, CAMERASRC_STATE_AF_IN_PROGRESS); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_stop_autofocusing(camsrc_handle_t handle) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (!(CAMERASRC_STATE(p) == CAMERASRC_STATE_PREVIEW || + CAMERASRC_STATE(p) == CAMERASRC_STATE_VIDEO || + CAMERASRC_STATE(p) == CAMERASRC_STATE_AF_IN_PROGRESS)) { + camsrc_error("Invalid state [%d]", CAMERASRC_STATE(p)); + return CAMERASRC_ERR_INVALID_STATE; + } + + /* STOP AF */ + camsrc_info("AF_STOP called!!"); + err = _camerasrc_stop_autofocusing(p); + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Stop autofocusing error"); + return err; + } + + if (CAMERASRC_STATE(p) > CAMERASRC_STATE_READY) { + camsrc_info("Set state to [PREVIEW] again!!"); + CAMERASRC_SET_STATE(p, CAMERASRC_STATE_PREVIEW); + } else { + camsrc_info("Do not change state"); + } + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_release_autofocusing(camsrc_handle_t handle) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_PREVIEW && + CAMERASRC_STATE(p) != CAMERASRC_STATE_VIDEO && + CAMERASRC_STATE(p) != CAMERASRC_STATE_AF_IN_PROGRESS) { + return CAMERASRC_SUCCESS; + } + + /* STOP AF */ + camsrc_info("AF_RELEASE called!!"); + err = _camerasrc_release_autofocusing(p); + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Stop autofocusing error"); + return err; + } + + camsrc_info("Set state to [PREVIEW] again!!"); + CAMERASRC_SET_STATE(p, CAMERASRC_STATE_PREVIEW); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_init_autofocusing_mode(camsrc_handle_t handle, camerasrc_af_mode_t af_mode, camerasrc_af_scan_range_t af_range) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_PREVIEW) { + camsrc_warning("Invalid state transition"); + } + + p->cur_af_mode = af_mode; + p->cur_af_range = af_range; + + err = _camerasrc_init_autofocusing_mode(p); + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Init autofocusing mode error"); + return err; + } + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_get_autofocusing_mode(camsrc_handle_t handle, camerasrc_af_mode_t *af_mode, camerasrc_af_scan_range_t *af_range) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (af_mode) { + *af_mode = p->cur_af_mode; + } + + if (af_range) { + *af_range = p->cur_af_range; + } + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_get_autofocusing_status(camsrc_handle_t handle, camerasrc_auto_focus_status_t *af_status) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + if (af_status == NULL) { + camsrc_error("*af_status is NULL"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + *af_status = p->af_status; + + camsrc_info("[camerasrc_get_autofocusing_status] %d", *af_status); + + return CAMERASRC_SUCCESS; +} + +/**** M I S C E L L A N E O U S O P E R A T I O N ****/ + +/**** I N P U T ( C A M D E V I C E ) O P E R A T I O N ****/ + +int camerasrc_get_input(camsrc_handle_t handle, camerasrc_dev_id_t *id) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_PHASE(p) != CAMERASRC_PHASE_RUNNING) { + camsrc_warning("Invalid phase"); + } + + *id = p->cur_dev_id; + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_set_input(camsrc_handle_t handle, camerasrc_dev_id_t camera_id) +{ + camerasrc_handle_t *p = NULL; + struct v4l2_control control; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_READY) { + camsrc_error("Invalid state"); + return CAMERASRC_ERR_INVALID_STATE; + } + + if (p->cur_dev_id == camera_id) { + return CAMERASRC_SUCCESS; + } + + camsrc_info("Set Index to %d", _CAMERASRC_GET_DEV_INDEX(camera_id)); + + err = _camerasrc_ioctl(p, VIDIOC_S_INPUT, &(_CAMERASRC_GET_DEV_INDEX(camera_id))); + if (CAMERASRC_SUCCESS != err) { + camsrc_error("[***DEBUG] ERROR SET INPUT to %dth DEVICE", _CAMERASRC_GET_DEV_INDEX(camera_id)); + return CAMERASRC_ERR_IO_CONTROL; + } else { + camsrc_info("[***DEBUG] SET INPUT OK!!! to %dth DEVICE", _CAMERASRC_GET_DEV_INDEX(camera_id)); + camsrc_info("return value of ioctl VIDIOC_S_INPUT = %d", err); + } + + p->cur_dev_id = camera_id; + + /* check physical rotation of camera lens */ + #ifndef V4L2_CID_PHYSICAL_ROTATION + #define V4L2_CID_PHYSICAL_ROTATION (V4L2_CID_PRIVATE_BASE + 300) + #endif /* V4L2_CID_PHYSICAL_ROTATION */ + + control.id = V4L2_CID_PHYSICAL_ROTATION; + _camerasrc_ioctl(handle, VIDIOC_G_CTRL, &control); + if(CAMERASRC_SUCCESS != _camerasrc_ioctl(handle, VIDIOC_G_CTRL, &control)) { + camsrc_warning("failed to get rotation of lens"); + p->lens_rotation = -1; + } else { + p->lens_rotation = control.value; + camsrc_info("lens rotation %d", p->lens_rotation); + } + + return err; +} + +/**** O U T P U T C O N T R O L O P E R A T I O N ****/ + +int camerasrc_set_timeperframe(camsrc_handle_t handle, camerasrc_frac_t *frac) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_PHASE(p) != CAMERASRC_PHASE_RUNNING) { + camsrc_warning("Invalid phase, but can go"); + } + + camsrc_info("Numerator = %d, Denominator = %d", frac->numerator, frac->denominator); + + LOCK(p); + p->timeperframe.numerator = frac->numerator; + p->timeperframe.denominator = frac->denominator; + UNLOCK(p); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_set_format(camsrc_handle_t handle, camerasrc_format_t *fmt) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_READY) { + camsrc_error("Invalid state"); + } + + err = _camerasrc_dump_format(handle); + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Format dump error"); + return err; + } + + CLEAR(p->format); + + p->format.colorspace = fmt->colorspace; + p->format.bytesperline = fmt->bytesperline; + p->format.img_size.dim.width = fmt->img_size.dim.width; + p->format.img_size.dim.height = fmt->img_size.dim.height; + p->format.thumb_size.dim.width = fmt->thumb_size.dim.width; + p->format.thumb_size.dim.height= fmt->thumb_size.dim.height; + p->format.is_highquality_mode = fmt->is_highquality_mode; + p->format.pix_format = fmt->pix_format; + p->format.quality = fmt->quality; + p->format.sizeimage = fmt->sizeimage; + p->format.rotation = fmt->rotation; + p->is_preset = 0; + + switch (fmt->pix_format) { + case CAMERASRC_PIX_YUV422P: + p->format.num_planes = 3; + break; + case CAMERASRC_PIX_YUV420P: + p->format.num_planes = 3; + break; + case CAMERASRC_PIX_YUV420: + p->format.num_planes = 2; + break; + case CAMERASRC_PIX_SN12: + p->format.num_planes = 2; + break; + case CAMERASRC_PIX_ST12: + p->format.num_planes = 2; + break; + case CAMERASRC_PIX_NV12: + p->format.num_planes = 2; + break; + case CAMERASRC_PIX_YUY2: + p->format.num_planes = 1; + break; + case CAMERASRC_PIX_UYVY: + p->format.num_planes = 1; + break; + case CAMERASRC_PIX_RGGB8: + p->format.num_planes = 1; + break; + case CAMERASRC_PIX_RGGB10: + p->format.num_planes = 1; + break; + case CAMERASRC_PIX_RGB565: + p->format.num_planes = 1; + break; + default: + p->format.num_planes = 3; + camsrc_error("Invalid output format."); + break; + } + + err = _camerasrc_dump_format(handle); + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Format dump error"); + return err; + } + + camsrc_info("leave"); + + return err; +} + + +int camerasrc_get_format(camsrc_handle_t handle, camerasrc_format_t *fmt) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_PHASE(p) != CAMERASRC_PHASE_RUNNING) { + camsrc_warning("Invalid phase"); + } + + memcpy(fmt, &(p->format), sizeof(camerasrc_format_t)); + + camsrc_info("leave"); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_try_format(camsrc_handle_t handle, camerasrc_format_t *fmt) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_STATE(p) != CAMERASRC_STATE_READY) { + camsrc_error("Invalid state"); + return CAMERASRC_ERR_INVALID_STATE; + } + + /* size limitaion check */ + if (fmt->img_size.dim.width < 0 || fmt->img_size.dim.width > CAMERASRC_MAX_WIDTH) { + camsrc_info("Invalid width"); + return CAMERASRC_ERR_INVALID_PARAMETER; + } + if (fmt->img_size.dim.height < 0 || fmt->img_size.dim.height > CAMERASRC_MAX_HEIGHT) { + camsrc_info("Invalid height"); + return CAMERASRC_ERR_INVALID_PARAMETER; + } + + /* check pixel number & colorspace are in bound */ + if (fmt->pix_format < 0 || fmt->pix_format >= CAMERASRC_PIX_NUM) { + camsrc_info("Invalid pixel format"); + return CAMERASRC_ERR_INVALID_PARAMETER; + } + if (fmt->colorspace < 0 || fmt->colorspace >= CAMERASRC_COL_NUM) { + camsrc_info("Invalid colorspace"); + return CAMERASRC_ERR_INVALID_PARAMETER; + } + + /* colorspace & pixel format combinability check */ + if (_CAMERASRC_MATCH_COL_TO_PIX(p->cur_dev_id, fmt->colorspace, fmt->pix_format, CAMERASRC_QUALITY_HIGH) || + _CAMERASRC_MATCH_COL_TO_PIX(p->cur_dev_id, fmt->colorspace, fmt->pix_format, CAMERASRC_QUALITY_NORMAL)) { + err = CAMERASRC_SUCCESS; + } else { + camsrc_info("UNAVAILABLE SETTING"); + err = CAMERASRC_ERR_INVALID_PARAMETER; + } + + camsrc_info("leave"); + + return err; +} + + +int camerasrc_get_frame_data(camsrc_handle_t handle, camerasrc_frame_data_t *data) +{ + camerasrc_handle_t *p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_PHASE(p) != CAMERASRC_PHASE_RUNNING) { + camsrc_warning("Invalid state transition"); + } + + err = _camerasrc_get_frame_data(p, data); + if (err != CAMERASRC_SUCCESS) { + camsrc_error("toggle auto exposure failed"); + return err; + } + + return err; +} + + +static int _camerasrc_dump_format(camsrc_handle_t handle) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + camsrc_info("---------FORMAT SETTING DUMP--------"); + camsrc_info("- Image size : %d x %d", p->format.img_size.dim.width, p->format.img_size.dim.height); + camsrc_info("- Thumbnail size : %d x %d", p->format.thumb_size.dim.width, p->format.thumb_size.dim.height); + camsrc_info("- Pixel format : %d", p->format.pix_format); + camsrc_info("- Bytes per line : %d", p->format.bytesperline); + camsrc_info("- Image size in bytes : %d", p->format.sizeimage); + camsrc_info("- Colorspace : %d", p->format.colorspace); + camsrc_info("- Rotation : %d", p->format.rotation); + camsrc_info("------------------------------------"); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_get_exif_info(camsrc_handle_t handle, camerasrc_exif_t *exif_struct) +{ + camerasrc_handle_t* p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + if (CAMERASRC_PHASE(p) != CAMERASRC_PHASE_RUNNING) { + camsrc_warning("Invalid state transition"); + } + + err = _camerasrc_get_exif_info(p, (camerasrc_buffer_t*)exif_struct); + if (err != CAMERASRC_SUCCESS) { + camsrc_error("Get exif information string failed"); + } + + return err; +} + + +int camerasrc_device_is_open(camsrc_handle_t handle) +{ + camerasrc_handle_t *p = handle; + + if (p) { + if (p->dev_fd > 0) { + return TRUE; + } + } + + return FALSE; +} + + +int camerasrc_set_videofd(camsrc_handle_t handle, int videofd) +{ + camerasrc_handle_t *p = handle; + + if (p) { + p->dev_fd = videofd; + return CAMERASRC_SUCCESS; + } else { + return CAMERASRC_ERR_INVALID_HANDLE; + } +} + + +int camerasrc_set_af_hold_after_capture(camsrc_handle_t handle, int hold_af) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + p->hold_af_after_capturing = hold_af; + + camsrc_info("leave"); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_set_sensor_mode(camsrc_handle_t handle, int mode) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + p->sensor_mode = mode; + + camsrc_info("leave"); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_set_vflip(camsrc_handle_t handle, int vflip) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + p->vflip = vflip; + + if (CAMERASRC_STATE(p) > CAMERASRC_STATE_READY) { + CAMERASRC_SET_CMD(_CAMERASRC_CMD_VFLIP, &(p->vflip)); + } + + camsrc_info("leave"); + + return CAMERASRC_SUCCESS; +} + + +int camerasrc_set_hflip(camsrc_handle_t handle, int hflip) +{ + camerasrc_handle_t *p = NULL; + + camsrc_info("enter"); + + if (handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + p->hflip = hflip; + if (CAMERASRC_STATE(p) > CAMERASRC_STATE_READY) { + CAMERASRC_SET_CMD(_CAMERASRC_CMD_HFLIP, &(p->hflip)); + } + + camsrc_info("leave"); + + return CAMERASRC_SUCCESS; +} + + +/* For Query functionalities */ +int camerasrc_read_basic_dev_info(camerasrc_dev_id_t dev_id, camerasrc_caps_info_t* caps_info) +{ + int err = CAMERASRC_ERR_UNKNOWN; + int nread = 0; + char* store_path = NULL; + FILE *fp = NULL; + + camsrc_info("enter"); + + if(dev_id == CAMERASRC_DEV_ID_PRIMARY) + store_path = CAMERASRC_PRIMARY_BASIC_INFO_PATH; + else if (dev_id == CAMERASRC_DEV_ID_SECONDARY) + store_path = CAMERASRC_SECONDARY_BASIC_INFO_PATH; + else + { + camsrc_error("Unsupported device ID"); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + + fp = fopen(store_path, "rb"); + if(fp) + { + fseek(fp, 0, SEEK_SET); + nread = fread(caps_info, 1, sizeof(camerasrc_caps_info_t), fp); + camsrc_info("Need to be read : %d / Actual read : %d", sizeof(camerasrc_caps_info_t), nread); + fclose(fp); + } + else + return CAMERASRC_ERR_ALLOCATION; + + err = CAMERASRC_SUCCESS; + camsrc_info("leave"); + return err; +} + + +int camerasrc_read_misc_dev_info(camerasrc_dev_id_t dev_id, camerasrc_ctrl_list_info_t* ctrl_info) +{ + int err = CAMERASRC_ERR_UNKNOWN; + camsrc_info("enter"); + + int nread = 0; + FILE *fp = NULL; + char* store_path = NULL; + + if(dev_id == CAMERASRC_DEV_ID_PRIMARY) + store_path = CAMERASRC_PRIMARY_MISC_INFO_PATH; + else if (dev_id == CAMERASRC_DEV_ID_SECONDARY) + store_path = CAMERASRC_SECONDARY_MISC_INFO_PATH; + else + { + camsrc_error("Unsupported device ID"); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + + fp = fopen(store_path, "rb"); + if(fp) + { + fseek(fp, 0, SEEK_SET); + nread = fread(ctrl_info, 1, sizeof(camerasrc_ctrl_list_info_t), fp); + camsrc_info("Need to be read : %d / Actual read : %d", sizeof(camerasrc_ctrl_list_info_t), nread); + fclose(fp); + } + else + return CAMERASRC_ERR_ALLOCATION; + + err = CAMERASRC_SUCCESS; + camsrc_info("leave"); + return err; +} + + +int camerasrc_read_extra_dev_info(camerasrc_dev_id_t dev_id, camerasrc_extra_info_t* extra_info) +{ + int err = CAMERASRC_ERR_UNKNOWN; + camsrc_info("enter"); + + int nread = 0; + FILE *fp = NULL; + char* store_path = NULL; + + if(dev_id == CAMERASRC_DEV_ID_PRIMARY) + store_path = CAMERASRC_PRIMARY_EXTRA_INFO_PATH; + else if (dev_id == CAMERASRC_DEV_ID_SECONDARY) + store_path = CAMERASRC_SECONDARY_EXTRA_INFO_PATH; + else + { + camsrc_error("Unsupported device ID"); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + + fp = fopen(store_path, "rb"); + if(fp) + { + fseek(fp, 0, SEEK_SET); + nread = fread(extra_info, 1, sizeof(camerasrc_extra_info_t), fp); + camsrc_info("Need to be read : %d / Actual read : %d", sizeof(camerasrc_extra_info_t), nread); + fclose(fp); + } + else + return CAMERASRC_ERR_ALLOCATION; + + err = CAMERASRC_SUCCESS; + camsrc_info("leave"); + return err; +} + + +int camerasrc_write_basic_dev_info(camsrc_handle_t handle, camerasrc_caps_info_t* caps_info) +{ + camerasrc_handle_t* p = NULL; + char* store_path = NULL; + FILE *fp = NULL; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + int nwrite = 0; + + if(p->cur_dev_id == CAMERASRC_DEV_ID_PRIMARY) + { + camsrc_info("Primary(Mega) camera capabilities info will be written.."); + store_path = CAMERASRC_PRIMARY_BASIC_INFO_PATH; + } + else if (p->cur_dev_id == CAMERASRC_DEV_ID_SECONDARY) + { + camsrc_info("Secondary(VGA) camera capabilities info will be written.."); + store_path = CAMERASRC_SECONDARY_BASIC_INFO_PATH; + } + else + { + camsrc_error("Unsupported device ID"); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + camsrc_info("PATH = %s", store_path); + + fp = fopen(store_path, "wb"); + if(fp) + { + fseek(fp, 0, SEEK_SET); + nwrite = fwrite(caps_info, 1, sizeof(camerasrc_caps_info_t), fp); + camsrc_info("Need to be written : %d / Actual written : %d", sizeof(camerasrc_caps_info_t), nwrite); + fclose(fp); + } + else + return CAMERASRC_ERR_ALLOCATION; + + camsrc_info("leave"); + return CAMERASRC_SUCCESS; +} + + +int camerasrc_write_misc_dev_info(camsrc_handle_t handle, camerasrc_ctrl_list_info_t* ctrl_info) +{ + camerasrc_handle_t* p = NULL; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + int nwrite = 0; + FILE *fp = NULL; + + char* store_path = NULL; + + if(p->cur_dev_id == CAMERASRC_DEV_ID_PRIMARY) + { + camsrc_info("Primary(Mega) camera controls info will be written.."); + store_path = CAMERASRC_PRIMARY_MISC_INFO_PATH; + } + else if (p->cur_dev_id == CAMERASRC_DEV_ID_SECONDARY) + { + camsrc_info("Secondary(VGA) camera controls info will be written.."); + store_path = CAMERASRC_SECONDARY_MISC_INFO_PATH; + } + else + { + camsrc_error("Unsupported device ID"); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + + fp = fopen(store_path, "wb"); + if(fp) + { + fseek(fp, 0, SEEK_SET); + nwrite = fwrite(ctrl_info, 1, sizeof(camerasrc_ctrl_list_info_t), fp); + camsrc_info("Need to be written : %d / Actual written : %d", sizeof(camerasrc_ctrl_list_info_t), nwrite); + fclose(fp); + } + else + return CAMERASRC_ERR_ALLOCATION; + + camsrc_info("leave"); + return CAMERASRC_SUCCESS; +} + + +int camerasrc_write_extra_dev_info(camsrc_handle_t handle, camerasrc_extra_info_t* extra_info) +{ + camerasrc_handle_t* p = NULL; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + int nwrite = 0; + FILE *fp = NULL; + + char* store_path = NULL; + + if(p->cur_dev_id == CAMERASRC_DEV_ID_PRIMARY) + { + camsrc_info("Primary(Mega) extra controls info will be written.."); + store_path = CAMERASRC_PRIMARY_EXTRA_INFO_PATH; + } + else if (p->cur_dev_id == CAMERASRC_DEV_ID_SECONDARY) + { + camsrc_info("Secondary(VGA) extra controls info will be written.."); + store_path = CAMERASRC_SECONDARY_EXTRA_INFO_PATH; + } + else + { + camsrc_error("Unsupported device ID"); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + + fp = fopen(store_path, "wb"); + if(fp) + { + fseek(fp, 0, SEEK_SET); + nwrite = fwrite(extra_info, 1, sizeof(camerasrc_extra_info_t), fp); + camsrc_info("Need to be written : %d / Actual written : %d", sizeof(camerasrc_extra_info_t), nwrite); + fclose(fp); + } + else + return CAMERASRC_ERR_ALLOCATION; + + camsrc_info("leave"); + return CAMERASRC_SUCCESS; +} + +void _camerasrc_add_total_resolution_info( camerasrc_caps_info_t* caps_info, int fcc_use, int width, int height ) +{ + int i = 0; + + if( 1 || fcc_use == CAMERASRC_FCC_USE_REC_PREVIEW + || fcc_use == CAMERASRC_FCC_USE_CAP_PREVIEW ) + { + for( i = 0 ; i < caps_info->num_preview_resolution ; i++ ) + { + if( caps_info->preview_resolution_width[i] == width + && caps_info->preview_resolution_height[i] == height ) + { + camsrc_info("preview resolution[%dx%d] is already existed.", width, height); + return; + } + } + + caps_info->preview_resolution_width[i] = width; + caps_info->preview_resolution_height[i] = height; + caps_info->num_preview_resolution++; + + camsrc_info("preview resolution[%dx%d] is added.", width, height); + } + else if( fcc_use == CAMERASRC_FCC_USE_NORMAL_CAPTURE + || fcc_use == CAMERASRC_FCC_USE_CONT_CAPTURE ) + { + for( i = 0 ; i < caps_info->num_capture_resolution ; i++ ) + { + if( caps_info->capture_resolution_width[i] == width + && caps_info->capture_resolution_height[i] == height ) + { + camsrc_info("capture resolution[%dx%d] is already existed.", width, height); + return; + } + } + + caps_info->capture_resolution_width[i] = width; + caps_info->capture_resolution_height[i] = height; + caps_info->num_capture_resolution++; + + camsrc_info("capture resolution[%dx%d] is added.", width, height); + } + + return; +} + +void _camerasrc_add_total_fps( camerasrc_caps_info_t* caps_info, int numerator, int denominator ) +{ + int i = 0; + + for( i = 0 ; i < caps_info->num_fps ; i++ ) + { + if( caps_info->fps[i].numerator == numerator + && caps_info->fps[i].denominator == denominator ) + { + camsrc_info("fps[%d/%d] is already existed.", numerator, denominator); + return; + } + } + + caps_info->fps[caps_info->num_fps].numerator = numerator; + caps_info->fps[caps_info->num_fps].denominator = denominator; + caps_info->num_fps++; + + camsrc_info("fps[%d/%d] is added.", numerator, denominator); + + return; +} + +int _camerasrc_query_pixfmt_timeperframe(camerasrc_handle_t* handle, camerasrc_caps_info_t* caps_info, int fmt_index, int resolution_index ) +{ + int tpf_cnt = 0; + struct v4l2_frmivalenum vfrmivalenum; + unsigned int fcc = 0; + camerasrc_resolution_t* resolutions = NULL; + + if( !handle || !caps_info ) + { + camsrc_info("handle[%p] or caps_info[%p] is NULL.", handle, caps_info); + return CAMERASRC_ERR_NULL_POINTER; + } + + if( 0 > fmt_index || 0 > resolution_index ) + { + camsrc_info("some index is too small. fmt_index[%d], resolution_index[%d]", + fmt_index, resolution_index); + return CAMERASRC_ERR_RANGE_UNDER; + } + + fcc = caps_info->fmt_desc[fmt_index].fcc; + resolutions = &(caps_info->fmt_desc[fmt_index].resolutions[resolution_index]); + + /* + *How many resolutions are supported in this format + */ + CLEAR(vfrmivalenum); + vfrmivalenum.index = tpf_cnt; + vfrmivalenum.pixel_format = fcc; + vfrmivalenum.width = resolutions->w; + vfrmivalenum.height = resolutions->h; + + while(0 == _camerasrc_ioctl(handle, VIDIOC_ENUM_FRAMEINTERVALS, &vfrmivalenum)) + { + resolutions->tpf[tpf_cnt].num = vfrmivalenum.discrete.numerator; + resolutions->tpf[tpf_cnt].den = vfrmivalenum.discrete.denominator; + tpf_cnt++; + + _camerasrc_add_total_fps( caps_info, vfrmivalenum.discrete.denominator, vfrmivalenum.discrete.numerator ); + } + + if(tpf_cnt == 0) + { + camsrc_info("No timeperframe supported. driver may not support to query"); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + + resolutions->num_avail_tpf = tpf_cnt; + + return CAMERASRC_SUCCESS; +} + +int _camerasrc_query_pixfmt_resolution(camerasrc_handle_t* handle, camerasrc_caps_info_t* caps_info, int fmt_index ) +{ + int res_cnt = 0; + int err = CAMERASRC_ERR_UNKNOWN; + struct v4l2_frmsizeenum vfrmszenum; + camerasrc_fmt_desc_t* fmt_desc = NULL; + + if( !handle || !caps_info ) + { + camsrc_info("handle[%p] or caps_info[%p] is NULL.", handle, caps_info); + return CAMERASRC_ERR_NULL_POINTER; + } + + if( 0 > fmt_index ) + { + camsrc_info("fmt_index[%d] is too small.", fmt_index); + return CAMERASRC_ERR_RANGE_UNDER; + } + + fmt_desc = &caps_info->fmt_desc[fmt_index]; + + /* + *How many resolutions are supported in this format + */ + CLEAR(vfrmszenum); + vfrmszenum.index = res_cnt; + vfrmszenum.pixel_format = fmt_desc->fcc; + while(0 == _camerasrc_ioctl(handle, VIDIOC_ENUM_FRAMESIZES, &vfrmszenum)) + { + fmt_desc->resolutions[res_cnt].w = vfrmszenum.discrete.width; + fmt_desc->resolutions[res_cnt].h = vfrmszenum.discrete.height; + + _camerasrc_add_total_resolution_info( caps_info, caps_info->fmt_desc[fmt_index].fcc_use, + vfrmszenum.discrete.width, vfrmszenum.discrete.height ); + + err = _camerasrc_query_pixfmt_timeperframe(handle, caps_info, fmt_index, res_cnt); + if(err != CAMERASRC_SUCCESS) + { + camsrc_info("timeperframe querying error, err = %d", err); + return err; + } + + res_cnt++; + vfrmszenum.index = res_cnt; + } + + if(res_cnt == 0) + { + camsrc_info("No resolution supported in fcc[0x%8x]. driver may not support to query", fmt_desc->fcc); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + + fmt_desc->num_resolution = res_cnt; + + return CAMERASRC_SUCCESS; +} + +int _camerasrc_query_basic_dev_info(camerasrc_handle_t* handle, camerasrc_caps_info_t* caps_info) { + struct v4l2_fmtdesc vfmtdesc; + struct v4l2_input vinput; + int err = CAMERASRC_ERR_UNKNOWN; + int fmt_cnt = 0; + char err_msg[CAMERASRC_ERRMSG_MAX_LEN] = {'\0',}; + + caps_info->num_preview_resolution = 0; + caps_info->num_capture_resolution = 0; + caps_info->num_preview_fmt = 0; + caps_info->num_capture_fmt = 0; + caps_info->num_fps = 0; + + /* + * Count how many formats are supported in this dev + */ + CLEAR(vfmtdesc); + CLEAR(vinput); + + /* + * What name of module is selected? + */ + if(CAMERASRC_SUCCESS != _camerasrc_ioctl(handle, VIDIOC_ENUMINPUT, &vinput)) { + strerror_r(handle->errnum, err_msg, CAMERASRC_ERRMSG_MAX_LEN); + camsrc_error("VIDIOC_ENUMINPUT failed : %s", err_msg); + return CAMERASRC_ERR_IO_CONTROL; + } + memcpy(caps_info->dev_name, vinput.name, 32); + + vfmtdesc.index = 0; + vfmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + while(0 == _camerasrc_ioctl(handle, VIDIOC_ENUM_FMT, &vfmtdesc)) + { + camsrc_info("pixelformat \"%c%c%c%c\"(%8x) detected", + (char)(vfmtdesc.pixelformat >> 0), + (char)(vfmtdesc.pixelformat >> 8), + (char)(vfmtdesc.pixelformat >> 16), + (char)(vfmtdesc.pixelformat >> 24), + vfmtdesc.pixelformat); + caps_info->fmt_desc[fmt_cnt].fcc = vfmtdesc.pixelformat; + /* + * Along the description of the format, we seperate them into each usecase + */ + camsrc_info("description \"%s\"", vfmtdesc.description ); + if(!strcmp("rec_prev", (const char*)(vfmtdesc.description))) + { + caps_info->fmt_desc[fmt_cnt].fcc_use = CAMERASRC_FCC_USE_REC_PREVIEW; + caps_info->preview_fmt[caps_info->num_preview_fmt] = vfmtdesc.pixelformat; + caps_info->num_preview_fmt++; + } + else if(!strcmp("cap_prev", (const char*)(vfmtdesc.description))) + { + caps_info->fmt_desc[fmt_cnt].fcc_use = CAMERASRC_FCC_USE_CAP_PREVIEW; + caps_info->preview_fmt[caps_info->num_preview_fmt] = vfmtdesc.pixelformat; + caps_info->num_preview_fmt++; + } + else if(!strcmp("recording", (const char*)(vfmtdesc.description))) + { + caps_info->fmt_desc[fmt_cnt].fcc_use = CAMERASRC_FCC_USE_RECORDING; + } + else if(!strcmp("capture", (const char*)(vfmtdesc.description))) + { + caps_info->fmt_desc[fmt_cnt].fcc_use = CAMERASRC_FCC_USE_NORMAL_CAPTURE; + caps_info->capture_fmt[caps_info->num_capture_fmt] = vfmtdesc.pixelformat; + caps_info->num_capture_fmt++; + } + else if(!strcmp("cont_cap", (const char*)(vfmtdesc.description))) + { + caps_info->fmt_desc[fmt_cnt].fcc_use = CAMERASRC_FCC_USE_CONT_CAPTURE; + caps_info->capture_fmt[caps_info->num_capture_fmt] = vfmtdesc.pixelformat; + caps_info->num_capture_fmt++; + } + else + { + caps_info->fmt_desc[fmt_cnt].fcc_use = CAMERASRC_FCC_USE_NONE; + } + err = _camerasrc_query_pixfmt_resolution(handle, caps_info, fmt_cnt); + if(err != CAMERASRC_SUCCESS) + { + camsrc_info("pixel format querying error, err = %x", err); + } + fmt_cnt++; + vfmtdesc.index = fmt_cnt; + vfmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + } + + + /* + * memory allocation as counted num + */ + camsrc_info("Total supported format = %d", fmt_cnt); + caps_info->num_fmt_desc = fmt_cnt; + + /* + * If no formats supported + */ + if(fmt_cnt == 0) + { + camsrc_info("no format supported"); + return CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + } + return CAMERASRC_SUCCESS; +} + + +int camerasrc_query_basic_dev_info(camsrc_handle_t handle, camerasrc_caps_info_t* caps_info) { + camerasrc_handle_t* p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + err = _camerasrc_query_basic_dev_info(p, caps_info); + + err = CAMERASRC_SUCCESS; + camsrc_info("leave"); + + return err; +} + +int _camerasrc_query_ctrl_menu (camerasrc_handle_t* handle, struct v4l2_queryctrl queryctrl, camerasrc_ctrl_info_t* ctrl_info) +{ + struct v4l2_querymenu querymenu; + + camsrc_info(" Menu items:"); + + memset (&querymenu, 0, sizeof (querymenu)); + querymenu.id = queryctrl.id; + + for (querymenu.index = queryctrl.minimum; + querymenu.index <= queryctrl.maximum; + querymenu.index++) { + if (0 == _camerasrc_ioctl (handle, VIDIOC_QUERYMENU, &querymenu)) { + camsrc_info(" menu name : %s", querymenu.name); + ctrl_info->ctrl_menu[querymenu.index].menu_index = querymenu.index; + memcpy(&(ctrl_info->ctrl_menu[querymenu.index].menu_name), querymenu.name, 32); + } else { + camsrc_error("VIDIOC_QUERYMENU error"); + return CAMERASRC_ERR_IO_CONTROL; + } + } + return CAMERASRC_SUCCESS; +} + +static int _find_camsrc_ctrl_id(camerasrc_handle_t* handle, int v4l2_cid) +{ + int i = 0; + int tmp_id = 0; + for(i=0; i<CAMERASRC_CTRL_NUM; i++) + { + tmp_id = _CAMERASRC_GET_CID(i, handle->cur_dev_id); + if(tmp_id == v4l2_cid) + return i; + } + + return -1; +} + +int _camerasrc_query_misc_dev_info(camerasrc_handle_t* handle, camerasrc_ctrl_list_info_t* ctrl_list_info) +{ + struct v4l2_queryctrl queryctrl; + + int i = 0; + int ctrl_cnt = 0; + + + camsrc_info("QUERY_BASE "); + for (i = CAMERASRC_CTRL_BRIGHTNESS; i < CAMERASRC_CTRL_NUM; i++) { + queryctrl.id = _CAMERASRC_GET_CID(i, handle->cur_dev_id); + if(queryctrl.id == -1) + { + continue; + } + if (0 == _camerasrc_ioctl (handle, VIDIOC_QUERYCTRL, &queryctrl)) { + if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) + { + camsrc_info("ID [%8x] disabled", queryctrl.id); + /*continue;*/ + } + camsrc_info("Control %s", queryctrl.name); + ctrl_list_info->ctrl_info[ctrl_cnt].v4l2_ctrl_id = queryctrl.id; + ctrl_list_info->ctrl_info[ctrl_cnt].camsrc_ctrl_id = i; + if(ctrl_list_info->ctrl_info[ctrl_cnt].camsrc_ctrl_id == -1) + { + camsrc_warning("This control is not included in camsrc ctrl ID error"); + } + switch(queryctrl.type) + { + case V4L2_CTRL_TYPE_INTEGER: + ctrl_list_info->ctrl_info[ctrl_cnt].ctrl_type = CTRL_TYPE_RANGE; + break; + case V4L2_CTRL_TYPE_BOOLEAN: + ctrl_list_info->ctrl_info[ctrl_cnt].ctrl_type = CTRL_TYPE_BOOL; + break; + case V4L2_CTRL_TYPE_MENU: + ctrl_list_info->ctrl_info[ctrl_cnt].ctrl_type = CTRL_TYPE_ARRAY; + _camerasrc_query_ctrl_menu (handle, queryctrl, &(ctrl_list_info->ctrl_info[ctrl_cnt])); + break; + default: + ctrl_list_info->ctrl_info[ctrl_cnt].ctrl_type = CTRL_TYPE_UNKNOWN; + break; + } + memcpy(ctrl_list_info->ctrl_info[ctrl_cnt].ctrl_name,queryctrl.name, 32); + ctrl_list_info->ctrl_info[ctrl_cnt].min = queryctrl.minimum; + ctrl_list_info->ctrl_info[ctrl_cnt].max = queryctrl.maximum; + ctrl_list_info->ctrl_info[ctrl_cnt].step = queryctrl.step; + ctrl_list_info->ctrl_info[ctrl_cnt].default_val = queryctrl.default_value; + camsrc_info("Control %s", queryctrl.name); + } else { + if (errno == EINVAL) + continue; + camsrc_error("VIDIOC_QUERYCTRL error"); + return CAMERASRC_ERR_IO_CONTROL; + } + ctrl_cnt++; + } + ctrl_list_info->num_ctrl_list_info = ctrl_cnt; + return CAMERASRC_SUCCESS; +} + + +int camerasrc_query_misc_dev_info(camsrc_handle_t handle, camerasrc_ctrl_list_info_t* ctrl_list_info) +{ + camerasrc_handle_t* p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + err = _camerasrc_query_misc_dev_info(p, ctrl_list_info); + + err = CAMERASRC_SUCCESS; + + camsrc_info("leave"); + + return err; +} + + +int camerasrc_query_misc_dev_ctrl_info(camsrc_handle_t handle, camerasrc_ctrl_t ctrl_id, camerasrc_ctrl_info_t* ctrl_info) +{ + struct v4l2_queryctrl queryctrl; + + CLEAR(queryctrl); + + queryctrl.id = _CAMERASRC_GET_CID(ctrl_id, CAMERASRC_HANDLE(handle)->cur_dev_id); + if (0 == _camerasrc_ioctl (handle, VIDIOC_QUERYCTRL, &queryctrl)) { + if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) + { + camsrc_info("ID [%8x] disabled", queryctrl.id); + } + camsrc_info("Control %s", queryctrl.name); + ctrl_info->v4l2_ctrl_id = queryctrl.id; + ctrl_info->camsrc_ctrl_id = ctrl_id; + if(ctrl_info->camsrc_ctrl_id == -1) + { + camsrc_warning("This control is not included in camsrc ctrl ID error"); + } + switch(queryctrl.type) + { + case V4L2_CTRL_TYPE_INTEGER: + ctrl_info->ctrl_type = CTRL_TYPE_RANGE; + break; + case V4L2_CTRL_TYPE_BOOLEAN: + ctrl_info->ctrl_type = CTRL_TYPE_BOOL; + break; + case V4L2_CTRL_TYPE_MENU: + ctrl_info->ctrl_type = CTRL_TYPE_ARRAY; + _camerasrc_query_ctrl_menu (handle, queryctrl, ctrl_info); + break; + default: + ctrl_info->ctrl_type = CTRL_TYPE_UNKNOWN; + break; + } + memcpy(ctrl_info->ctrl_name,queryctrl.name, MAX_SZ_CTRL_NAME_STRING); + ctrl_info->min = queryctrl.minimum; + ctrl_info->max = queryctrl.maximum; + ctrl_info->step = queryctrl.step; + ctrl_info->default_val = queryctrl.default_value; + camsrc_info("Control %s", queryctrl.name); + } else { + camsrc_error("VIDIOC_QUERYCTRL error"); + return CAMERASRC_ERR_IO_CONTROL; + } + return CAMERASRC_SUCCESS; +} + + +int camerasrc_dump_misc_dev_info(camsrc_handle_t handle, camerasrc_ctrl_list_info_t* ctrl_list_info) +{ + camsrc_info("enter"); + int i=0; + int j=0; + + camsrc_info("============================================================"); + for(i = 0; i < ctrl_list_info->num_ctrl_list_info; i++) + { + camsrc_info("[camsrc ID : %d] [v4l2 ID : 0x%08x] Control name = %s", ctrl_list_info->ctrl_info[i].camsrc_ctrl_id, ctrl_list_info->ctrl_info[i].v4l2_ctrl_id, ctrl_list_info->ctrl_info[i].ctrl_name); + camsrc_info(" Control type = %d", ctrl_list_info->ctrl_info[i].ctrl_type); + camsrc_info(" Min = %d / Max = %d / Step = %d / default = %d", ctrl_list_info->ctrl_info[i].min, ctrl_list_info->ctrl_info[i].max, ctrl_list_info->ctrl_info[i].step, ctrl_list_info->ctrl_info[i].default_val); + if(ctrl_list_info->ctrl_info[i].ctrl_type == CTRL_TYPE_ARRAY) + { + camsrc_info("\t\t - Menu list"); + for(j = ctrl_list_info->ctrl_info[i].min; j <= ctrl_list_info->ctrl_info[i].max; j++) + { + camsrc_info("\t\t * %s", ctrl_list_info->ctrl_info[i].ctrl_menu[j].menu_name); + } + } + } + camsrc_info("============================================================"); + camsrc_info("leave"); + return CAMERASRC_SUCCESS; +} + + +int camerasrc_dump_basic_dev_info(camsrc_handle_t handle, camerasrc_caps_info_t* caps_info) { + camsrc_info("enter"); + int i,j,k; + + camsrc_info("==============================================================="); + for(i = 0; i< caps_info->num_fmt_desc; i++) + { + camsrc_info("Supported fourcc = \"%c%c%c%c\" (0x%08x) num of resolution = %d", + (char)(caps_info->fmt_desc[i].fcc >> 0), + (char)(caps_info->fmt_desc[i].fcc >> 8), + (char)(caps_info->fmt_desc[i].fcc >> 16), + (char)(caps_info->fmt_desc[i].fcc >> 24), + caps_info->fmt_desc[i].fcc, + caps_info->fmt_desc[i].num_resolution); + for(j=0; j<caps_info->fmt_desc[i].num_resolution; j++) + { + camsrc_info("\tresolution = %d x %d, num of tpf = %d", + caps_info->fmt_desc[i].resolutions[j].w, caps_info->fmt_desc[i].resolutions[j].h, + caps_info->fmt_desc[i].resolutions[j].num_avail_tpf); + for(k=0; k<caps_info->fmt_desc[i].resolutions[j].num_avail_tpf; k++) + { + camsrc_info("\t\ttimeperframe = %d / %d", caps_info->fmt_desc[i].resolutions[j].tpf[k].num, caps_info->fmt_desc[i].resolutions[j].tpf[k].den); + } + } + } + camsrc_info("==============================================================="); + camsrc_info("total supported preview resolution"); + for( i = 0 ; i < caps_info->num_preview_resolution ; i++ ) + { + camsrc_info("%5dx%5d", caps_info->preview_resolution_width[i], caps_info->preview_resolution_height[i]); + + } + camsrc_info("==============================================================="); + camsrc_info("total supported capture resolution"); + for( i = 0 ; i < caps_info->num_capture_resolution ; i++ ) + { + camsrc_info("%5dx%5d", caps_info->capture_resolution_width[i], caps_info->capture_resolution_height[i]); + + } + camsrc_info("==============================================================="); + camsrc_info("total supported preview fourcc"); + for( i = 0 ; i < caps_info->num_preview_fmt ; i++ ) + { + camsrc_info("\"%c%c%c%c\" (0x%08x)", + (char)(caps_info->preview_fmt[i]), + (char)(caps_info->preview_fmt[i]>>8), + (char)(caps_info->preview_fmt[i]>>16), + (char)(caps_info->preview_fmt[i]>>24), + caps_info->preview_fmt[i]); + } + camsrc_info("==============================================================="); + camsrc_info("total supported capture fourcc"); + for( i = 0 ; i < caps_info->num_capture_fmt ; i++ ) + { + camsrc_info("\"%c%c%c%c\" (0x%08x)", + (char)(caps_info->capture_fmt[i]), + (char)(caps_info->capture_fmt[i]>>8), + (char)(caps_info->capture_fmt[i]>>16), + (char)(caps_info->capture_fmt[i]>>24), + caps_info->capture_fmt[i]); + } + camsrc_info("==============================================================="); + camsrc_info("total supported fps"); + for( i = 0 ; i < caps_info->num_fps ; i++ ) + { + camsrc_info("%3d/%3d", caps_info->fps[i].numerator, caps_info->fps[i].denominator); + } + camsrc_info("==============================================================="); + camsrc_info("leave"); + return CAMERASRC_SUCCESS; +} + + +int camerasrc_dump_extra_dev_info(camsrc_handle_t handle, camerasrc_extra_info_t* extra_info) +{ + camsrc_info("enter"); + camsrc_info("==============================================================="); + camsrc_info(" Strobe can support below"); + if (extra_info->strobe_caps == CAMERASRC_STROBE_CAP_NONE) { + camsrc_info(" - Strobe Support Nothing"); + } else { + if (extra_info->strobe_caps & CAMERASRC_STROBE_CAP_OFF) + camsrc_info(" - Always off mode"); + if (extra_info->strobe_caps & CAMERASRC_STROBE_CAP_ON) + camsrc_info(" - Always on mode"); + if (extra_info->strobe_caps & CAMERASRC_STROBE_CAP_AUTO) + camsrc_info(" - Auto mode"); + if (extra_info->strobe_caps & CAMERASRC_STROBE_CAP_REDEYE) + camsrc_info(" - Redeye reduction mode"); + if (extra_info->strobe_caps & CAMERASRC_STROBE_CAP_SLOWSYNC) + camsrc_info(" - Slow sync mode"); + if (extra_info->strobe_caps & CAMERASRC_STROBE_CAP_FRONT_CURTAIN) + camsrc_info(" - Front curtain mode"); + if (extra_info->strobe_caps & CAMERASRC_STROBE_CAP_REAR_CURTAIN) + camsrc_info(" - Rear curtain mode"); + if (extra_info->strobe_caps & CAMERASRC_STROBE_CAP_PERMANENT) + camsrc_info(" - Permanent mode"); + if (extra_info->strobe_caps & CAMERASRC_STROBE_CAP_EXTERNAL) + camsrc_info(" - External strobe mode"); + } + + if (extra_info->detection_caps) { + camsrc_info(" Facedetection can be supported"); + } else { + camsrc_info(" Facedetection can't be supported"); + } + + camsrc_info("==============================================================="); + camsrc_info("leave"); + + return CAMERASRC_SUCCESS; +} + +static int _camerasrc_query_extra_dev_info(camerasrc_handle_t* handle, camerasrc_extra_info_t* extra_info) +{ + struct v4l2_capability capability; + + if (0 == _camerasrc_ioctl (handle, VIDIOC_QUERYCAP, &capability)) { + camsrc_info("VIDIOC_QUERYCAP result [0x%08x]", capability.capabilities); + if((capability.capabilities & V4L2_CAP_STROBE) != 0) + { + /* Strobe supported */ + struct v4l2_strobe strobecaps; + if (0 == _camerasrc_ioctl (handle, VIDIOC_G_STROBE, &strobecaps)) + { + extra_info->strobe_caps = strobecaps.capabilities; + } + else + { + camsrc_error("VIDIOC_G_STROBE error"); + return CAMERASRC_ERR_IO_CONTROL; + } + } + else + { + /* Strobe NOT supported */ + camsrc_error("STROBE not supported"); + extra_info->strobe_caps = 0; + } + + if((capability.capabilities & V4L2_CAP_OBJ_RECOGNITION) != 0) + { + /* Recognition supported */ + extra_info->detection_caps = 1; + } + else + { + camsrc_error("FACEDETECTION not supported"); + extra_info->detection_caps = 0; + /* Recognition NOT supported */ + } + + return CAMERASRC_SUCCESS; + } else { + camsrc_error("VIDIOC_QUERYCAP error"); + + return CAMERASRC_ERR_IO_CONTROL; + } +} + + +int camerasrc_query_extra_dev_info(camsrc_handle_t handle, camerasrc_extra_info_t* extra_info) +{ + camerasrc_handle_t* p = NULL; + int err = CAMERASRC_ERR_UNKNOWN; + + camsrc_info("enter"); + + if(handle == NULL) { + camsrc_error("handle is null"); + return CAMERASRC_ERR_NULL_POINTER; + } + + p = CAMERASRC_HANDLE(handle); + + err = _camerasrc_query_extra_dev_info(p, extra_info); + + err = CAMERASRC_SUCCESS; + camsrc_info("leave"); + + return err; +} + +/* END For Query functionalities */ diff --git a/camerasrc/src/gstcamerasrc.c b/camerasrc/src/gstcamerasrc.c new file mode 100644 index 0000000..9871741 --- /dev/null +++ b/camerasrc/src/gstcamerasrc.c @@ -0,0 +1,3894 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gst/gst.h> +#include <gst/gstutils.h> +#include <glib-object.h> +#include <unistd.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <sched.h> /* sched_yield() */ + +#include "gstcamerasrc.h" +#include "gstcamerasrccontrol.h" +#include "gstcamerasrccolorbalance.h" + + +/****************************************************************************** + * Definitions + *******************************************************************************/ +GST_DEBUG_CATEGORY (camerasrc_debug); +#define GST_CAT_DEFAULT camerasrc_debug + +#define USE_FRAME_SAFETY_MARGIN + +#if defined (USE_FRAME_SAFETY_MARGIN) +#define FRAME_SAFETY_MARGIN 4096 +#else +#define FRAME_SAFETY_MARGIN 0 +#endif + +#ifndef YUV422_SIZE +#define YUV422_SIZE(width,height) ( ((width)*(height)) << 1 ) +#endif + +#ifndef YUV420_SIZE +#define YUV420_SIZE(width,height) ( ((width)*(height)*3) >> 1 ) +#endif + +#define SCMN_CS_YUV420 1 /* Y:U:V 4:2:0 */ +#define SCMN_CS_I420 SCMN_CS_YUV420 /* Y:U:V */ +#define SCMN_CS_NV12 6 +#define SCMN_CS_NV12_T64X32 11 /* 64x32 Tiled NV12 type */ +#define SCMN_CS_UYVY 100 +#define SCMN_CS_YUYV 101 +#define SCMN_CS_YUY2 SCMN_CS_YUYV + +/* max channel count *********************************************************/ +#define SCMN_IMGB_MAX_PLANE (4) + +/* image buffer definition *************************************************** + + +------------------------------------------+ --- + | | ^ + | a[], p[] | | + | +---------------------------+ --- | | + | | | ^ | | + | |<---------- w[] ---------->| | | | + | | | | | | + | | | | + | | | h[] | e[] + | | | | + | | | | | | + | | | | | | + | | | v | | + | +---------------------------+ --- | | + | | v + +------------------------------------------+ --- + + |<----------------- s[] ------------------>| +*/ + +typedef struct +{ + /* width of each image plane */ + int w[SCMN_IMGB_MAX_PLANE]; + /* height of each image plane */ + int h[SCMN_IMGB_MAX_PLANE]; + /* stride of each image plane */ + int s[SCMN_IMGB_MAX_PLANE]; + /* elevation of each image plane */ + int e[SCMN_IMGB_MAX_PLANE]; + /* user space address of each image plane */ + void *a[SCMN_IMGB_MAX_PLANE]; + /* physical address of each image plane, if needs */ + void *p[SCMN_IMGB_MAX_PLANE]; + /* color space type of image */ + int cs; + /* left postion, if needs */ + int x; + /* top position, if needs */ + int y; + /* to align memory */ + int __dummy2; + /* arbitrary data */ + int data[16]; +} SCMN_IMGB; + +#if !defined (PAGE_SHIFT) + #define PAGE_SHIFT sysconf(_SC_PAGESIZE) +#endif +#if !defined (PAGE_SIZE) + #define PAGE_SIZE (1UL << PAGE_SHIFT) +#endif +#if !defined (PAGE_MASK) + #define PAGE_MASK (~(PAGE_SIZE-1)) +#endif +#if !defined (PAGE_ALIGN) + #define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK) +#endif + +#define ALIGN_SIZE_I420 (1024<<2) +#define ALIGN_SIZE_NV12 (1024<<6) +#define CAMERASRC_ALIGN(addr,size) (((addr)+((size)-1))&(~((size)-1))) + +#if !defined (CLEAR) + #define CLEAR(x) memset(&(x), 0, sizeof(x)) +#endif + + +/* Enables */ +#define _ENABLE_CAMERASRC_DEBUG 0 + +/* Local definitions */ +#define _DEFAULT_WIDTH 320 +#define _DEFAULT_HEIGHT 240 +#define _DEFAULT_FPS 30 +#define _DEFAULT_HIGH_SPEED_FPS 0 +#define _DEFAULT_FPS_AUTO FALSE +#define _DEFAULT_PIX_FORMAT CAMERASRC_PIX_UYVY +#define _DEFAULT_FOURCC GST_MAKE_FOURCC ('J', 'P', 'E', 'G') +#define _DEFAULT_COLORSPACE CAMERASRC_COL_RAW +#define _DEFAULT_CAMERA_ID CAMERASRC_DEV_ID_PRIMARY + +/* mmap/pad-alloc related definition */ +#define _DEFAULT_NUM_LIVE_BUFFER 0 +#define _DEFAULT_BUFFER_COUNT 0 +#define _ALLOWED_GAP_BTWN_BUF 5 /* Allowed gap between circulation count of each buffer and that of average buffer */ +#define _DEFAULT_BUFFER_RUNNING FALSE +#define _DEFAULT_DEQUE_WAITINGTIME 200 /* msec */ +#define _MINIMUM_REMAINING_V4L2_QBUF 1 /* minimum number of buffer that should remain in the v4l2 driver */ + +#define _FD_DEFAULT (-1) +#define _FD_MIN (-1) +#define _FD_MAX (1<<15) /* 2^15 == 32768 */ + +#define _DEFAULT_CAP_QUALITY GST_CAMERASRC_QUALITY_HIGH +#define _DEFAULT_CAP_JPG_QUALITY 95 +#define _DEFAULT_CAP_WIDTH 1600 +#define _DEFAULT_CAP_HEIGHT 1200 +#define _DEFAULT_CAP_COUNT 1 +#define _DEFAULT_CAP_INTERVAL 0 +#define _DEFAULT_CAP_PROVIDE_EXIF FALSE +#define _DEFAULT_ENABLE_ZSL_MODE FALSE +#define _DEFAULT_DO_FACE_DETECT FALSE +#define _DEFAULT_DO_AF FALSE +#define _DEFAULT_SIGNAL_AF FALSE +#define _DEFAULT_SIGNAL_STILL_CAPTURE FALSE +#define _DEFAULT_PREVIEW_WIDTH _DEFAULT_WIDTH +#define _DEFAULT_PREVIEW_HEIGHT _DEFAULT_HEIGHT +#define _DEFAULT_KEEPING_BUFFER 0 +#define _DEFAULT_USE_PAD_ALLOC FALSE +#define _DEFAULT_NUM_ALLOC_BUF 6 +#define _DEFAULT_SCRNL_FOURCC GST_MAKE_FOURCC('N','V','1','2') +#define _MAX_NUM_ALLOC_BUF 100 +#define _MAX_TRIAL_WAIT_FRAME 25 +#define _PAD_ALLOC_RETRY_PERIOD 25 +#define _MAX_PAD_ALLOC_RETRY_COUNT 300 +#define _CONTINUOUS_SHOT_MARGIN 50 /* msec */ +#define _PREVIEW_BUFFER_WAIT_TIMEOUT 2000000 /* usec */ + +/*FIXME*/ +#define _THUMBNAIL_WIDTH 320 +#define _THUMBNAIL_HEIGHT 240 + +#define GST_TYPE_CAMERASRC_BUFFER (gst_camerasrc_buffer_get_type()) +#define GST_IS_CAMERASRC_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_CAMERASRC_BUFFER)) +#define GST_CAMERASRC_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_CAMERASRC_BUFFER, GstCameraBuffer)) +#define GST_TYPE_CAMERASRC_QUALITY (gst_camerasrc_quality_get_type()) + + +/* Enumerations */ +enum { + /*signal*/ + SIGNAL_STILL_CAPTURE, + SIGNAL_NEGO_COMPLETE, + /*SIGNAL_REGISTER_TROUBLE,*/ + LAST_SIGNAL +}; + +enum { + ARG_0, + /* camera */ + ARG_CAMERA_HIGH_SPEED_FPS, + ARG_CAMERA_AUTO_FPS, + ARG_CAMERA_ID, + ARG_CAMERA_EXT_VIDEO_FD, + + /* capture */ + ARG_CAMERA_CAPTURE_FOURCC, + ARG_CAMERA_CAPTURE_QUALITY, + ARG_CAMERA_CAPTURE_WIDTH, + ARG_CAMERA_CAPTURE_HEIGHT, + ARG_CAMERA_CAPTURE_INTERVAL, + ARG_CAMERA_CAPTURE_COUNT, + ARG_CAMERA_CAPTURE_JPG_QUALITY, + ARG_CAMERA_CAPTURE_PROVIDE_EXIF, + + /* signal */ + ARG_SIGNAL_STILLCAPTURE, + ARG_REQ_NEGOTIATION, + + ARG_USE_PAD_ALLOC, + ARG_NUM_ALLOC_BUF, + ARG_OPERATION_STATUS, + ARG_HOLD_AF_AFTER_CAPTURE, + ARG_FAKE_ESD, + ARG_SENSOR_MODE, + ARG_VFLIP, + ARG_HFLIP, + ARG_NUM, +}; + +enum { + VIDEO_IN_MODE_UNKNOWN, + VIDEO_IN_MODE_PREVIEW, + VIDEO_IN_MODE_VIDEO, + VIDEO_IN_MODE_CAPTURE, +}; + +/* Structures */ +typedef struct { + gint index; + gint buffer_data_index; + GstCameraSrc *camerasrc; +} GST_CAMERASRC_BUFFER_DATA; + +static void gst_camerasrc_uri_handler_init (gpointer g_iface, gpointer iface_data); + +/* Local variables */ +static GstBufferClass *camera_buffer_parent_class = NULL; + +static guint gst_camerasrc_signals[LAST_SIGNAL] = { 0 }; + +/* For pad_alloc architecture */ +static camerasrc_usr_buf_t g_present_buf; + +#if _ENABLE_CAMERASRC_DEBUG +static unsigned int g_preview_frame_cnt; +#endif + +/* Element template variables */ +static GstStaticPadTemplate src_factory = + GST_STATIC_PAD_TEMPLATE("src", + GST_PAD_SRC, + GST_PAD_ALWAYS, + GST_STATIC_CAPS("video/x-raw-yuv," + "format = (fourcc) { UYVY }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-yuv," + "format = (fourcc) { YU12 }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-yuv," + "format = (fourcc) { I420 }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-yuv," + "format = (fourcc) { NV12 }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-yuv," + "format = (fourcc) { SN12 }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-yuv," + "format = (fourcc) { ST12 }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-yuv," + "format = (fourcc) { YUY2 }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-yuv," + "format = (fourcc) { YUYV }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-yuv," + "format = (fourcc) { SUYV }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-yuv," + "format = (fourcc) { SUY2 }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-yuv," + "format = (fourcc) { SYVY }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-yuv," + "format = (fourcc) { S420 }, " + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]; " + "video/x-raw-rgb," + "width = (int) [ 1, 4096 ], " + "height = (int) [ 1, 4096 ]")); + +static GstElementDetails camerasrc_details = { + "Camera Source GStreamer Plug-in", + "Src/Video", + "camera src for videosrc based GStreamer Plug-in", + "" +}; + + + +/* Local static functions */ +static void gst_camerasrc_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); +static void gst_camerasrc_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); + +static gboolean gst_camerasrc_src_start(GstBaseSrc *src); +static gboolean gst_camerasrc_src_stop(GstBaseSrc *src); +static gboolean gst_camerasrc_start(GstCameraSrc *camerasrc); + +static GstFlowReturn gst_camerasrc_src_create(GstPushSrc *src, GstBuffer **buffer); +static GstFlowReturn gst_camerasrc_read_capture(GstCameraSrc *camerasrc, GstBuffer **buffer, int command); +static GstFlowReturn gst_camerasrc_read_preview_mmap(GstCameraSrc *camerasrc, GstBuffer **buffer); +static GstFlowReturn gst_camerasrc_read_preview_pad_alloc(GstCameraSrc *camerasrc, GstBuffer **buffer); +static unsigned char *gst_camerasrc_make_metadata_for_nonlinear_buffer(GstCameraSrc *avsysvideosrc, guint32 index); + +static GstStateChangeReturn gst_camerasrc_change_state(GstElement *element, GstStateChange transition); +static GstCaps *gst_camerasrc_get_caps(GstBaseSrc *src); +static gboolean gst_camerasrc_set_caps(GstBaseSrc *src, GstCaps *caps); +static gboolean gst_camerasrc_get_caps_info(GstCameraSrc *camerasrc, GstCaps *caps, guint *size); +static gboolean gst_camerasrc_fill_ctrl_list(GstCameraSrc *camerasrc); +static gboolean gst_camerasrc_empty_ctrl_list(GstCameraSrc *camerasrc); +static void gst_camerasrc_finalize(GObject *object); + +static gboolean gst_camerasrc_device_is_open(GstCameraSrc *camerasrc); +static gboolean gst_camerasrc_get_timeinfo(GstCameraSrc *camerasrc, GstBuffer *buffer); +static int get_proper_index(GstCameraSrc *camerasrc, GstBuffer *pad_alloc_buffer); + +static gboolean gst_camerasrc_capture_start(GstCameraSrc *camerasrc); +static gboolean gst_camerasrc_capture_stop(GstCameraSrc *camerasrc); + +static GstCameraBuffer *gst_camerasrc_buffer_new(GstCameraSrc *camerasrc); +static GType gst_camerasrc_buffer_get_type(void); +static void gst_camerasrc_buffer_class_init(gpointer g_class, gpointer class_data); +static void gst_camerasrc_buffer_finalize(GstCameraBuffer *buffer); +static void gst_camerasrc_buffer_free(gpointer data); +static void gst_camerasrc_buffer_trace(GstCameraSrc *camerasrc); +static void gst_camerasrc_error_handler(GstCameraSrc *camerasrc, int ret); + +/* Util functions */ +static void GenerateYUV420BlackFrame(unsigned char *buf, int buf_size, int width, int height); +static void GenerateYUV422BlackFrame(unsigned char *buf, int buf_size, int width, int height); +static gboolean GenerateST12BlackFrame(GstCameraSrc *camerasrc, unsigned char *buf, int buf_size, int width, int height); +static unsigned long gst_get_current_time(void); +static gboolean _gst_camerasrc_get_frame_size(int fourcc, int width, int height, unsigned int *outsize); +static gboolean _gst_camerasrc_get_raw_pixel_info(int fourcc, int *pix_format, int *colorspace); +static gboolean _gst_camerasrc_get_normal_buffer(GstCameraSrc *camerasrc, int fourcc, + unsigned char *base_buf, int width, int height, + unsigned char **new_buf, unsigned int *length); +#if _ENABLE_CAMERASRC_DEBUG +static int __util_write_file(char *filename, void *data, int size); +#endif /* _ENABLE_CAMERASRC_DEBUG */ +static gboolean gst_camerasrc_negotiate (GstBaseSrc * basesrc); + +GST_IMPLEMENT_CAMERASRC_COLOR_BALANCE_METHODS(GstCameraSrc, gst_camera_src); +GST_IMPLEMENT_CAMERASRC_CONTROL_METHODS(GstCameraSrc, gst_camera_src); + + +/****************************************************************************** + * Implementations + *******************************************************************************/ +static void gst_camerasrc_error_handler(GstCameraSrc *camerasrc, int ret) +{ + switch (ret) { + case CAMERASRC_SUCCESS: + break; + case CAMERASRC_ERR_IO_CONTROL: + GST_ELEMENT_ERROR(camerasrc, RESOURCE, FAILED, ("IO control error"), GST_ERROR_SYSTEM); + break; + case CAMERASRC_ERR_DEVICE_OPEN: + GST_ELEMENT_ERROR(camerasrc, RESOURCE, OPEN_READ_WRITE, ("camera open failed"), GST_ERROR_SYSTEM); + break; + case CAMERASRC_ERR_DEVICE_BUSY: + GST_ELEMENT_ERROR(camerasrc, RESOURCE, BUSY, ("camera device busy"), GST_ERROR_SYSTEM); + break; + case CAMERASRC_ERR_DEVICE_NOT_FOUND: + GST_ELEMENT_ERROR(camerasrc, RESOURCE, NOT_FOUND, ("camera device not found"), GST_ERROR_SYSTEM); + break; + case CAMERASRC_ERR_DEVICE_UNAVAILABLE: + GST_ELEMENT_ERROR(camerasrc, RESOURCE, OPEN_READ, ("camera device unavailable"), GST_ERROR_SYSTEM); + break; + case CAMERASRC_ERR_DEVICE_WAIT_TIMEOUT: + gst_camerasrc_buffer_trace(camerasrc); + GST_ELEMENT_ERROR(camerasrc, RESOURCE, TOO_LAZY, (("Timeout[live_buffers=%d]"), camerasrc->num_live_buffers), GST_ERROR_SYSTEM); + break; + case CAMERASRC_ERR_DEVICE_NOT_SUPPORT: + GST_ELEMENT_ERROR(camerasrc, RESOURCE, SETTINGS, ("Not supported"), GST_ERROR_SYSTEM); + break; + case CAMERASRC_ERR_ALLOCATION: + GST_ELEMENT_ERROR(camerasrc, RESOURCE, SETTINGS, ("memory allocation failed"), GST_ERROR_SYSTEM); + break; + case CAMERASRC_ERR_SECURITY_SERVICE: + GST_ELEMENT_ERROR(camerasrc, RESOURCE, FAILED, ("Security service failed"), GST_ERROR_SYSTEM); + break; + default: + GST_ELEMENT_ERROR(camerasrc, RESOURCE, SEEK, (("General video device error[ret=%x]"), ret), GST_ERROR_SYSTEM); + break; + } + + return; +} + +static gboolean gst_camerasrc_iface_supported(GstImplementsInterface *iface, GType iface_type) +{ + g_assert(iface_type == GST_TYPE_CAMERA_CONTROL || + iface_type == GST_TYPE_COLOR_BALANCE); + + return TRUE; +} + + +static void gst_camerasrc_interface_init(GstImplementsInterfaceClass *klass) +{ + /* + * default virtual functions + */ + klass->supported = gst_camerasrc_iface_supported; +} + + +void gst_camerasrc_init_interfaces(GType type) +{ + static const GInterfaceInfo urihandler_info = { + gst_camerasrc_uri_handler_init, + NULL, + NULL + }; + + static const GInterfaceInfo cameraiface_info = { + (GInterfaceInitFunc)gst_camerasrc_interface_init, + NULL, + NULL, + }; + + static const GInterfaceInfo camerasrc_control_info = { + (GInterfaceInitFunc)gst_camera_src_control_interface_init, + NULL, + NULL, + }; + + static const GInterfaceInfo camerasrc_color_balance_info = { + (GInterfaceInitFunc)gst_camera_src_color_balance_interface_init, + NULL, + NULL, + }; + + g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info); + g_type_add_interface_static(type, GST_TYPE_IMPLEMENTS_INTERFACE, &cameraiface_info); + g_type_add_interface_static(type, GST_TYPE_CAMERA_CONTROL, &camerasrc_control_info); + g_type_add_interface_static(type, GST_TYPE_COLOR_BALANCE, &camerasrc_color_balance_info); +} + + +static GType gst_camerasrc_quality_get_type(void) +{ + static GType camerasrc_quality_type = 0; + static const GEnumValue quality_types[] = { + {GST_CAMERASRC_QUALITY_LOW, "Low quality", "low"}, + {GST_CAMERASRC_QUALITY_HIGH, "High quality", "high"}, + {0, NULL, NULL} + }; + + if (!camerasrc_quality_type) { + camerasrc_quality_type = g_enum_register_static ("GstCameraSrcQuality", quality_types); + } + return camerasrc_quality_type; +} + +/* VOID:OBJECT,OBJECT (generated by 'glib-genmarshal') */ +#define g_marshal_value_peek_object(v) (v)->data[0].v_pointer +void gst_camerasrc_VOID__OBJECT_OBJECT(GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data) +{ + typedef void (*GMarshalFunc_VOID__OBJECT_OBJECT)(gpointer data1, + gpointer arg_1, + gpointer arg_2, + gpointer arg_3, + gpointer data2); + register GMarshalFunc_VOID__OBJECT_OBJECT callback; + register GCClosure *cc = (GCClosure*) closure; + register gpointer data1, data2; + + g_return_if_fail (n_param_values == 4); + + if (G_CCLOSURE_SWAP_DATA(closure)) { + data1 = closure->data; + data2 = g_value_peek_pointer(param_values + 0); + } else { + data1 = g_value_peek_pointer(param_values + 0); + data2 = closure->data; + } + + callback = (GMarshalFunc_VOID__OBJECT_OBJECT)(marshal_data ? marshal_data : cc->callback); + + callback(data1, + g_marshal_value_peek_object(param_values + 1), + g_marshal_value_peek_object(param_values + 2), + g_marshal_value_peek_object(param_values + 3), + data2); +} + + +/* use following BOILERPLATE MACRO as _get_type entry */ +GST_BOILERPLATE_FULL(GstCameraSrc, gst_camerasrc, GstPushSrc, GST_TYPE_PUSH_SRC, gst_camerasrc_init_interfaces); + + +static int gst_camerasrc_af_cb(camsrc_handle_t handle, int state, void *usr_param) +{ + GstCameraSrc *camerasrc = (GstCameraSrc *)usr_param; + GstMessage *m = NULL; + GstStructure *s = NULL; + + MMTA_ACUM_ITEM_END("AutoFocus operating time", FALSE); + + GST_INFO_OBJECT(camerasrc, "autofocus callback: state [%d]", state); + + s = gst_structure_new("camerasrc-AF", + "focus-state", G_TYPE_INT, state, + NULL); + m = gst_message_new_element(GST_OBJECT(camerasrc), s); + gst_element_post_message(GST_ELEMENT(camerasrc), m); + + return CAMERASRC_SUCCESS; +} + + +static gboolean gst_camerasrc_create(GstCameraSrc *camerasrc) +{ + int ret = 0; + + /*create handle*/ + __ta__(" camerasrc_create", + ret = camerasrc_create(&(camerasrc->v4l2_handle)); + ); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "camerasrc_create() failed. errcode = 0x%08X", ret); + goto _ERROR; + } + + GST_INFO_OBJECT (camerasrc, "camerasrc_create() done"); + + if (camerasrc->external_videofd != _FD_DEFAULT) { + __ta__(" camerasrc_set_videofd", + camerasrc_set_videofd(camerasrc->v4l2_handle,camerasrc->external_videofd); + ); + } + + /*CAMERASRC CAM: realize*/ + __ta__(" camerasrc_realize", + ret = camerasrc_realize(camerasrc->v4l2_handle); + ); + if (ret != CAMERASRC_SUCCESS) { + goto _ERROR; + } + + GST_INFO_OBJECT (camerasrc, "camerasrc_realize() done"); + + /*CAMERASRC CAM: start*/ + __ta__(" camerasrc_start", + ret = camerasrc_start(camerasrc->v4l2_handle); + ); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "camerasrc_start() failed. errcode = 0x%x", ret); + goto _ERROR; + } + + /*CAMERASRC CAM: set camera device id*/ + /**@note if realized, iput device can't change!*/ + __ta__(" camerasrc_set_input", + ret = camerasrc_set_input(camerasrc->v4l2_handle, camerasrc->camera_id); + ); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "camerasrc_set_input() failed. errcode = 0x%x", ret); + goto _ERROR; + } + + if (!gst_camerasrc_fill_ctrl_list(camerasrc)) { + GST_WARNING_OBJECT(camerasrc, "Can't fill v4l2 control list."); + } + + return TRUE; + + _ERROR: + gst_camerasrc_error_handler(camerasrc, ret); + + return FALSE; +} + + +static gboolean gst_camerasrc_destroy(GstCameraSrc *camerasrc) +{ + GST_INFO_OBJECT (camerasrc, "ENTERED"); + + if (camerasrc->v4l2_handle) { + /*Empty control list */ + gst_camerasrc_empty_ctrl_list(camerasrc); + + /*CAMERASRC CAM: stop stream*/ + /*CAMERASRC CAM: unrealize*/ + GST_INFO_OBJECT(camerasrc, "camerasrc_unrealize() calling..."); + camerasrc_unrealize(camerasrc->v4l2_handle); + + /*CAMERASRC CAM: destroy*/ + GST_INFO_OBJECT(camerasrc, "camerasrc_destroy() calling..."); + camerasrc_destroy(camerasrc->v4l2_handle); + camerasrc->v4l2_handle = NULL; + GST_INFO_OBJECT(camerasrc, "AV cam destroyed."); + camerasrc->mode = VIDEO_IN_MODE_UNKNOWN; + } + + GST_INFO_OBJECT(camerasrc, "LEAVED"); + + return TRUE; +} + + +static gboolean gst_camerasrc_fill_ctrl_list(GstCameraSrc *camerasrc) +{ + int n = 0; + camerasrc_ctrl_info_t ctrl_info; + + g_return_val_if_fail(camerasrc, FALSE); + g_return_val_if_fail(camerasrc->v4l2_handle, FALSE); + + GST_DEBUG_OBJECT(camerasrc, "ENTERED"); + + for (n = CAMERASRC_CTRL_BRIGHTNESS ; n < CAMERASRC_CTRL_NUM ; n++) { + GstCameraSrcColorBalanceChannel *camerasrc_color_channel = NULL; + GstColorBalanceChannel *color_channel = NULL; + + GstCamerasrcControlChannel *camerasrc_control_channel = NULL; + GstCameraControlChannel *control_channel = NULL; + + gint channel_type; + + memset(&ctrl_info, 0x0, sizeof(camerasrc_ctrl_info_t)); + + if (camerasrc_query_control(camerasrc->v4l2_handle, n, &ctrl_info) != CAMERASRC_SUCCESS) { + /* TODO: */ + } + + switch (n) { + case CAMERASRC_CTRL_BRIGHTNESS: + case CAMERASRC_CTRL_CONTRAST: + case CAMERASRC_CTRL_WHITE_BALANCE: + case CAMERASRC_CTRL_COLOR_TONE: + case CAMERASRC_CTRL_PARTCOLOR_SRC: + case CAMERASRC_CTRL_PARTCOLOR_DST: + case CAMERASRC_CTRL_PARTCOLOR_MODE: + case CAMERASRC_CTRL_SATURATION: + case CAMERASRC_CTRL_SHARPNESS: + channel_type = INTERFACE_COLOR_BALANCE; + break; + case CAMERASRC_CTRL_DIGITAL_ZOOM: + case CAMERASRC_CTRL_OPTICAL_ZOOM: + case CAMERASRC_CTRL_PROGRAM_MODE: + case CAMERASRC_CTRL_FLIP: + case CAMERASRC_CTRL_ANTI_HANDSHAKE: + case CAMERASRC_CTRL_WIDE_DYNAMIC_RANGE: + channel_type = INTERFACE_CAMERA_CONTROL; + break; + default: + channel_type = INTERFACE_NONE; + continue; + } + + if (channel_type == INTERFACE_COLOR_BALANCE) { + camerasrc_color_channel = g_object_new(GST_TYPE_CAMERASRC_COLOR_BALANCE_CHANNEL, NULL); + color_channel = GST_COLOR_BALANCE_CHANNEL(camerasrc_color_channel); + + color_channel->label = g_strdup((const gchar *)camerasrc_ctrl_label[n]); + camerasrc_color_channel->id = n; + color_channel->min_value = ctrl_info.min; + color_channel->max_value = ctrl_info.max; + + camerasrc->colors = g_list_append(camerasrc->colors, (gpointer)color_channel); + GST_INFO_OBJECT(camerasrc, "Adding Color Balance Channel %s (%x)", + color_channel->label, camerasrc_color_channel->id); + } else { /* if( channel_type == INTERFACE_CAMERA_CONTROL ) */ + camerasrc_control_channel = g_object_new(GST_TYPE_CAMERASRC_CONTROL_CHANNEL, NULL); + control_channel = GST_CAMERA_CONTROL_CHANNEL(camerasrc_control_channel); + + control_channel->label = g_strdup((const gchar *)camerasrc_ctrl_label[n]); + camerasrc_control_channel->id = n; + control_channel->min_value = ctrl_info.min; + control_channel->max_value = ctrl_info.max; + + camerasrc->camera_controls = g_list_append(camerasrc->camera_controls, (gpointer)control_channel); + GST_INFO_OBJECT(camerasrc, "Adding Camera Control Channel %s (%x)", + control_channel->label, camerasrc_control_channel->id); + } + } + + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + + return TRUE; +} + + +static gboolean gst_camerasrc_empty_ctrl_list(GstCameraSrc *camerasrc) +{ + g_return_val_if_fail(camerasrc, FALSE); + + GST_DEBUG_OBJECT (camerasrc, "ENTERED"); + + g_list_foreach(camerasrc->colors, (GFunc)g_object_unref, NULL); + g_list_free(camerasrc->colors); + camerasrc->colors = NULL; + + g_list_foreach(camerasrc->camera_controls, (GFunc)g_object_unref, NULL); + g_list_free(camerasrc->camera_controls); + camerasrc->camera_controls = NULL; + + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + + return TRUE; +} + + +static GstFlowReturn gst_camerasrc_prepare_preview(GstCameraSrc *camerasrc, int buf_num) +{ + int page_size = getpagesize(); + int buffer_size = 0; + int i = 0; + int ret = 0; + unsigned int main_buf_sz = 0; + unsigned int thumb_buf_sz = 0; + GstCaps *negotiated_caps = gst_pad_get_negotiated_caps(GST_BASE_SRC_PAD(camerasrc)); + + g_return_val_if_fail(buf_num <= MAX_USR_BUFFER_NUM, GST_FLOW_ERROR); + + camerasrc_query_img_buf_size(camerasrc->v4l2_handle, &main_buf_sz, &thumb_buf_sz); + + buffer_size = (main_buf_sz + page_size - 1) & ~(page_size - 1); + + g_present_buf.present_buffer = calloc(buf_num, sizeof(camerasrc_buffer_t)); + + for (i = 0 ; i < buf_num ; i++) { + GST_INFO_OBJECT (camerasrc,"pad_alloc called"); + ret = gst_pad_alloc_buffer(GST_BASE_SRC_PAD (camerasrc), + 0, + main_buf_sz, + negotiated_caps, + &(camerasrc->usr_buffer[i])); + + if (!GST_IS_BUFFER(camerasrc->usr_buffer[i])) { + GST_INFO_OBJECT (camerasrc, "[%d] NOT BUFFER!!?", i); + } + if (ret != GST_FLOW_OK) { + GST_ERROR_OBJECT (camerasrc, "gst_pad_alloc_buffer failed. [%d]", ret); + return ret; + } + + GST_INFO_OBJECT(camerasrc, "Alloced Size %d, Alloced address %p", + GST_BUFFER_SIZE(camerasrc->usr_buffer[i]), + GST_BUFFER_DATA(camerasrc->usr_buffer[i])); + + g_present_buf.present_buffer[i].start = GST_BUFFER_DATA(camerasrc->usr_buffer[i]); + g_present_buf.present_buffer[i].length = buffer_size; + } + + g_present_buf.num_buffer = buf_num; + + gst_caps_unref(negotiated_caps); + negotiated_caps = NULL; + + ret = camerasrc_present_usr_buffer(camerasrc->v4l2_handle, &g_present_buf, CAMERASRC_IO_METHOD_USRPTR); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "camerasrc_present_usr_buffer failed ret = %x", ret); + } else { + GST_INFO_OBJECT(camerasrc, "present_buffer success"); + } + + /*CAMERASRC CAM: start video preview*/ + ret = camerasrc_start_preview_stream(camerasrc->v4l2_handle); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT (camerasrc, "camerasrc_start_preview_stream() failed. errcode = 0x%08X", ret); + gst_camerasrc_error_handler(camerasrc, ret); + + return GST_FLOW_ERROR; + } + + GST_INFO_OBJECT(camerasrc, "camerasrc_start_preview_stream() done"); + + camerasrc->buffer_running = TRUE; + camerasrc->buffer_count = buf_num; + camerasrc->first_invokation = TRUE; + + return GST_FLOW_OK; +} + + +static gboolean gst_camerasrc_start(GstCameraSrc *camerasrc) +{ + int ret = 0; + + camerasrc_format_t fmt; + camerasrc_frac_t frac; + + GST_DEBUG_OBJECT(camerasrc, "ENTERED"); + +#ifdef _SPEED_UP_RAW_CAPTURE + /* check if from no stream change capture */ + if (camerasrc->mode == VIDEO_IN_MODE_CAPTURE && + camerasrc->cap_stream_diff == FALSE) { + camerasrc->buffer_running = TRUE; + goto _READY_DONE; + } + + camerasrc->cap_stream_diff = FALSE; +#endif + + /**@note do not setting at camer starting time*/ + /*CAMERASRC CAM: set FILTER (by default value)*/ + + /*CAMERASRC CAM: callback*/ + camerasrc_set_focused_callback(camerasrc->v4l2_handle, gst_camerasrc_af_cb, camerasrc); + + CLEAR(fmt); + CLEAR(frac); + + /*CAMERASRC CAM: format*/ + fmt.pix_format = camerasrc->pix_format; + fmt.colorspace = camerasrc->colorspace; + fmt.is_highquality_mode = 0; /*Fixed. if preview mode, set 0*/ + fmt.rotation = camerasrc->rotate; + + /*CAMERASRC CAM: set resolution - Do not care about rotation */ + CAMERASRC_SET_SIZE_BY_DIMENSION(fmt, camerasrc->width, camerasrc->height); + + /*CAMERASRC CAM: set format*/ + GST_INFO_OBJECT(camerasrc, "pix [%dx%d] fps %d, format %d, colorspace %d, rotation %d", + camerasrc->width, camerasrc->height, camerasrc->fps, + fmt.pix_format, fmt.colorspace, fmt.rotation); + ret = camerasrc_set_format(camerasrc->v4l2_handle, &fmt); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT (camerasrc, "camerasrc_set_format() failed. errcode = 0x%08X", ret); + goto _ERROR; + } + + /*CAMERASRC CAM: set fps*/ + if (camerasrc->fps_auto) { + /*if fps is zero, auto fps mode*/ + frac.numerator = 0; + frac.denominator = 1; + GST_INFO_OBJECT (camerasrc, "FPS auto(%d)", camerasrc->fps_auto); + } else if (camerasrc->high_speed_fps <= 0) { + if (camerasrc->fps <= 0) { + /*if fps is zero, auto fps mode*/ + frac.numerator = 0; + frac.denominator = 1; + } else { + frac.numerator = 1; + frac.denominator = camerasrc->fps; + } + } else { + GST_INFO_OBJECT(camerasrc, "high speed recording(%d)", camerasrc->high_speed_fps); + frac.numerator = 1; + frac.denominator = camerasrc->high_speed_fps; + } + + ret = camerasrc_set_timeperframe(camerasrc->v4l2_handle, &frac); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "camerasrc_set_timeperframe() failed. errcode = 0x%x", ret); + goto _ERROR; + } + GST_INFO_OBJECT (camerasrc, "camerasrc_set_timeperframe() done"); + + camerasrc->buffer_running = TRUE; + + if ((camerasrc->use_pad_alloc == FALSE)) { + /*CAMERASRC CAM: Set Sensor mode*/ + camerasrc_set_sensor_mode(camerasrc->v4l2_handle, camerasrc->sensor_mode); + + /*CAMERASRC CAM: Set flip*/ + camerasrc_set_vflip(camerasrc->v4l2_handle, camerasrc->vflip); + camerasrc_set_hflip(camerasrc->v4l2_handle, camerasrc->hflip); + + /*CAMERASRC CAM: start video preview*/ + __ta__(" camerasrc_start_preview_stream", + ret = camerasrc_start_preview_stream(camerasrc->v4l2_handle); + ); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "camerasrc_start_preview_stream() failed. errcode = 0x%x", ret); + camerasrc->buffer_running = FALSE; + goto _ERROR; + } + + GST_INFO_OBJECT(camerasrc, "camerasrc_start_preview_stream() done"); + + ret = camerasrc_get_num_buffer(camerasrc->v4l2_handle, &(camerasrc->buffer_count)); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "camerasrc_get_num_buffer() failed. errcode = 0x%x", ret); + goto _ERROR; + } + + GST_INFO_OBJECT(camerasrc, "buffer number %d", camerasrc->buffer_count); +#ifdef _SPEED_UP_RAW_CAPTURE + camerasrc->cap_stream_diff = FALSE; +#endif + } + +#ifdef _SPEED_UP_RAW_CAPTURE +_READY_DONE: +#endif + camerasrc->mode = VIDEO_IN_MODE_PREVIEW; + camerasrc->current_buffer_data_index = (camerasrc->current_buffer_data_index + 1)%10; + + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + + return TRUE; + +_ERROR: + gst_camerasrc_error_handler(camerasrc, ret); + + /* Stop stream */ + camerasrc_stop_stream(camerasrc->v4l2_handle); + + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + + return FALSE; +} + + +static gboolean gst_camerasrc_stop(GstCameraSrc *camerasrc) +{ + camerasrc_io_method_t io_method; + + GST_DEBUG_OBJECT (camerasrc, "ENTERED"); + + if (camerasrc->v4l2_handle) { + /* CAMERASRC CAM: stop stream */ + /* To guarantee buffers are valid before finishing */ + GMutex *lock_mutex = NULL; + + if (camerasrc->use_pad_alloc == FALSE) { + lock_mutex = camerasrc->buffer_lock; + } else { + lock_mutex = camerasrc->pad_alloc_mutex; + } + + g_mutex_lock(lock_mutex); + while (camerasrc->num_live_buffers > _DEFAULT_KEEPING_BUFFER) { + GTimeVal abstimeout; + GST_INFO_OBJECT(camerasrc, "Wait until all live buffers are relased. (Tot=%d, Live=%d)", + camerasrc->buffer_count, camerasrc->num_live_buffers); + + g_get_current_time(&abstimeout); + g_time_val_add(&abstimeout, _PREVIEW_BUFFER_WAIT_TIMEOUT); + + if (!g_cond_timed_wait(camerasrc->buffer_cond, lock_mutex, &abstimeout)) { + GST_ERROR_OBJECT(camerasrc, "Buffer wait timeout[%d usec].(Live=%d) Skip waiting...", + _PREVIEW_BUFFER_WAIT_TIMEOUT, camerasrc->num_live_buffers); + gst_camerasrc_buffer_trace(camerasrc); + break; + } else { + GST_INFO_OBJECT(camerasrc, "Signal received."); + } + } + g_mutex_unlock(lock_mutex); + GST_INFO_OBJECT(camerasrc, "Waiting free buffer finished. (Live=%d)", camerasrc->num_live_buffers); + + camerasrc_stop_stream(camerasrc->v4l2_handle); + + camerasrc->buffer_running = FALSE; + camerasrc->mode = VIDEO_IN_MODE_UNKNOWN; + + /* If I/O method is Usrptr, it uses usr_buffer that is member of camerasrc structure */ + /* It must be unreffed when it stops */ + + camerasrc_get_io_method(camerasrc->v4l2_handle, &io_method); + switch (io_method) { + case CAMERASRC_IO_METHOD_MMAP: + break; + case CAMERASRC_IO_METHOD_USRPTR: + if (g_present_buf.present_buffer) { + free(g_present_buf.present_buffer); + g_present_buf.present_buffer = NULL; + } + break; + case CAMERASRC_IO_METHOD_READ: + default: + break; + } + } + + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + + return TRUE; +} + + +static gboolean gst_camerasrc_capture_start(GstCameraSrc *camerasrc) +{ + /*CAMERASRC CAM*/ + int ret = 0; + char *pfourcc = NULL; + camerasrc_format_t fmt; + camerasrc_frac_t frac; + + GST_INFO_OBJECT(camerasrc, "ENTERED"); + + if (camerasrc->mode == VIDEO_IN_MODE_PREVIEW) { + /* To guarantee buffers are valid before finishing. */ + __ta__( " wait for buffer in gst_camerasrc_capture_start", + if (camerasrc->use_pad_alloc == FALSE) { + g_mutex_lock(camerasrc->buffer_lock); + while (camerasrc->num_live_buffers > _DEFAULT_KEEPING_BUFFER) { + GTimeVal abstimeout; + GST_INFO_OBJECT(camerasrc, "Wait until all live buffers are relased. (Tot=%d, Live=%d)", + camerasrc->buffer_count, camerasrc->num_live_buffers); + + g_get_current_time(&abstimeout); + g_time_val_add(&abstimeout, _PREVIEW_BUFFER_WAIT_TIMEOUT); + + if (!g_cond_timed_wait(camerasrc->buffer_cond, camerasrc->buffer_lock, &abstimeout)) { + GST_ERROR_OBJECT(camerasrc, "Buffer wait timeout[%d usec].(Live=%d) Skip waiting...", + _PREVIEW_BUFFER_WAIT_TIMEOUT, camerasrc->num_live_buffers); + gst_camerasrc_buffer_trace(camerasrc); + break; + } else { + GST_INFO_OBJECT(camerasrc, "Signal received."); + } + } + g_mutex_unlock(camerasrc->buffer_lock); + GST_INFO_OBJECT(camerasrc, "Waiting free buffer is finished. (Live=%d)", camerasrc->num_live_buffers); + } + ); + +#ifdef _SPEED_UP_RAW_CAPTURE + /* Skip restart stream if format/width/height are all same */ + if (camerasrc->fourcc == camerasrc->cap_fourcc && + camerasrc->width == camerasrc->cap_width && + camerasrc->height == camerasrc->cap_height) { + GST_INFO_OBJECT(camerasrc, "fourcc, width and height is same. Skip restarting stream."); + goto _CAPTURE_READY_DONE; + } + + camerasrc->cap_stream_diff = TRUE; +#endif + + /*CAMERASRC CAM: stop stream*/ + __ta__( " camerasrc_stop_stream in gst_camerasrc_capture_start", + camerasrc_stop_stream(camerasrc->v4l2_handle); + ); + + GST_INFO_OBJECT (camerasrc, "camerasrc_stop_stream() done"); + camerasrc->buffer_running = FALSE; + + pfourcc = (char*)&camerasrc->cap_fourcc; + GST_INFO_OBJECT(camerasrc, "CAPTURE: Size[%dx%d], fourcc(%c%c%c%c) quality[%d] interval[%d] count[%d]", + camerasrc->cap_width, camerasrc->cap_height, + pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3], + camerasrc->cap_quality, camerasrc->cap_interval, camerasrc->cap_count); + + /*START STILL CAPTURE*/ + + /*set current video info*/ + + memset(&fmt, 0x00, sizeof (camerasrc_format_t)); + + /*CAMERASRC CAM: set format*/ + CAMERASRC_SET_SIZE_BY_DIMENSION(fmt, camerasrc->cap_width, camerasrc->cap_height); + + _gst_camerasrc_get_raw_pixel_info(camerasrc->cap_fourcc, &(fmt.pix_format), &(fmt.colorspace)); + fmt.rotation = 0; + + if (camerasrc->cap_fourcc == GST_MAKE_FOURCC('J', 'P', 'E', 'G') || + camerasrc->cap_fourcc == GST_MAKE_FOURCC('j', 'p', 'e', 'g')) { + fmt.quality = camerasrc->cap_jpg_quality; + fmt.is_highquality_mode = camerasrc->cap_quality; /*must be 1*/ + } else { + fmt.is_highquality_mode = camerasrc->cap_quality; /*0 or 1 (default: 0)*/ + } + + /*CAMERASRC CAM: format*/ + ret = camerasrc_set_format(camerasrc->v4l2_handle, &fmt); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "camerasrc_set_format() failed. errcode = 0x%x", ret); + goto _ERROR; + } + GST_INFO_OBJECT(camerasrc, "camerasrc_set_format() done"); + + if (camerasrc->fps_auto || camerasrc->fps <= 0) { + /*if fps is zero, auto fps mode*/ + frac.numerator = 0; + frac.denominator = 1; + GST_INFO_OBJECT (camerasrc, "FPS auto"); + } else { + frac.numerator = 1; + frac.denominator = camerasrc->fps; + GST_INFO_OBJECT (camerasrc, "FPS (%d)", camerasrc->fps); + } + + ret = camerasrc_set_timeperframe(camerasrc->v4l2_handle, &frac); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "camerasrc_set_timeperframe() failed. errcode = 0x%x", ret); + goto _ERROR; + } + GST_INFO_OBJECT (camerasrc, "camerasrc_set_timeperframe() done"); + + /*CAMERASRC CAM: start stream*/ + __ta__( " camerasrc_start_still_stream", + ret = camerasrc_start_still_stream(camerasrc->v4l2_handle); + ); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "camerasrc_start_still_stream() failed. errcode = 0x%x", ret); + goto _ERROR; + } + GST_INFO_OBJECT(camerasrc, "camerasrc_start_still_stream() done"); + + camerasrc->buffer_running = TRUE; + + /*CAMERASRC CAM: set fps*/ + /*TODO: maybe do not works!*/ + +#ifdef _SPEED_UP_RAW_CAPTURE +_CAPTURE_READY_DONE: +#endif + + g_mutex_lock(camerasrc->jpg_mutex); + camerasrc->cap_next_time = 0UL; + g_mutex_unlock(camerasrc->jpg_mutex); + camerasrc->cap_count_current = 0; + + /* end change to capture mode*/ + camerasrc->mode = VIDEO_IN_MODE_CAPTURE; + + GST_INFO_OBJECT(camerasrc, "CAPTURE STARTED!"); + } else { + GST_WARNING_OBJECT(camerasrc, "Wrong state[%d]!", camerasrc->mode); + } + + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + + return TRUE; + +_ERROR: + gst_camerasrc_error_handler(camerasrc, ret); + + return FALSE; +} + + +static gboolean gst_camerasrc_capture_stop(GstCameraSrc *camerasrc) +{ + GST_DEBUG_OBJECT(camerasrc, "ENTERED"); + + if (camerasrc->mode == VIDEO_IN_MODE_CAPTURE) { +#ifdef _SPEED_UP_RAW_CAPTURE + if (camerasrc->cap_stream_diff) { + /*CAMERASRC CAM: stop stream*/ + camerasrc_stop_stream(camerasrc->v4l2_handle); + camerasrc->buffer_running = FALSE; + + GST_INFO_OBJECT(camerasrc, "camerasrc_stop_stream() done"); + } else { + GST_INFO_OBJECT(camerasrc, "no need to stop stream(capture format==preview format)"); + } +#else + /*CAMERASRC CAM: stop stream*/ + camerasrc_stop_stream(camerasrc->v4l2_handle); + camerasrc->buffer_running = FALSE; + + GST_INFO_OBJECT(camerasrc, "camerasrc_stop_stream() done"); +#endif + GST_INFO_OBJECT(camerasrc, "CAPTURE STOPPED!"); + } + + GST_DEBUG_OBJECT (camerasrc, "LEAVED"); + + return TRUE; +} + + +static GstFlowReturn gst_camerasrc_read_preview_mmap(GstCameraSrc *camerasrc, GstBuffer **buffer) +{ + int ret = 0; + int v4l2_buffer_index = 0; + guint i = 0; + unsigned int isize = 0; + unsigned char *pData = NULL; + void *buf = NULL; + camerasrc_buffer_t main_buf; + GstCameraBuffer *vid_buf = NULL; + + /*alloc main buffer*/ + vid_buf = gst_camerasrc_buffer_new(camerasrc); + buf = (GstCameraBuffer *)vid_buf; + + g_mutex_lock(camerasrc->buffer_lock); + GST_LOG_OBJECT(camerasrc, "After lock(buffer live %d, total %d)", + camerasrc->num_live_buffers, camerasrc->buffer_count); + + for (i = 0 ; i < _MAX_TRIAL_WAIT_FRAME ; i++) { + /* Wait frame */ + __ta__( " camerasrc_wait_frame_available", + ret = camerasrc_wait_frame_available(camerasrc->v4l2_handle, _DEFAULT_DEQUE_WAITINGTIME); + ); + if (ret != CAMERASRC_SUCCESS) { + if (ret == CAMERASRC_ERR_DEVICE_WAIT_TIMEOUT && i < (_MAX_TRIAL_WAIT_FRAME - 1)) { + GST_ERROR_OBJECT(camerasrc, "SELECT TIMEOUT!!! Retry..(pad_alloc %d, live %d)", + camerasrc->use_pad_alloc, camerasrc->num_live_buffers); + continue; + } + + if (ret == CAMERASRC_ERR_DEVICE_UNAVAILABLE) { + GST_ERROR_OBJECT(camerasrc, "register trouble error!! [%x]", ret); + /*g_signal_emit (G_OBJECT (camerasrc), gst_camerasrc_signals[SIGNAL_REGISTER_TROUBLE], (GQuark)NULL);*/ + g_mutex_unlock(camerasrc->buffer_lock); + gst_camerasrc_error_handler(camerasrc, ret); + + return GST_FLOW_ERROR; + } else if (ret == CAMERASRC_ERR_INVALID_STATE && (i < _MAX_TRIAL_WAIT_FRAME - 1)) { + GST_WARNING_OBJECT(camerasrc, "try again..."); + } else { + GST_ERROR_OBJECT(camerasrc, "Frame waiting error[%x]", ret); + g_mutex_unlock(camerasrc->buffer_lock); + gst_camerasrc_error_handler(camerasrc, ret); + + return GST_FLOW_ERROR; + } + } else { + GST_LOG_OBJECT(camerasrc, "select success, do DQBUF"); + break; + } + } + + /* Buffer DQ */ + __ta__( " camerasrc_dequeue_buffer", + ret = camerasrc_dequeue_buffer(camerasrc->v4l2_handle, &v4l2_buffer_index, &main_buf, NULL); + ); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "Dequeue frame error[%x]", ret); + g_mutex_unlock(camerasrc->buffer_lock); + gst_camerasrc_error_handler(camerasrc, ret); + + return GST_FLOW_ERROR; + } + + camerasrc->num_live_buffers++; + GST_LOG_OBJECT (camerasrc, "after : DQBUF (index %d, live bufs %d)", + v4l2_buffer_index, camerasrc->num_live_buffers); + + g_mutex_unlock (camerasrc->buffer_lock); + + if (camerasrc->fourcc == GST_MAKE_FOURCC('S','N','1','2') || + camerasrc->fourcc == GST_MAKE_FOURCC('S','T','1','2') || + camerasrc->fourcc == GST_MAKE_FOURCC('S','U','Y','V') || + camerasrc->fourcc == GST_MAKE_FOURCC('S','U','Y','2') || + camerasrc->fourcc == GST_MAKE_FOURCC('S','Y','V','Y') || + camerasrc->fourcc == GST_MAKE_FOURCC('S','4','2','0')) { + /* for physical address */ + GST_BUFFER_MALLOCDATA(vid_buf) = gst_camerasrc_make_metadata_for_nonlinear_buffer(camerasrc, v4l2_buffer_index); + + /* for virtual address - GST_BUFFER_DATA and SIZE */ + pData = main_buf.start; + isize = main_buf.length; + } else if (camerasrc->fourcc == GST_MAKE_FOURCC('I','4','2','0') || + camerasrc->fourcc == GST_MAKE_FOURCC('N','V','1','2')) { + ret = _gst_camerasrc_get_normal_buffer(camerasrc, camerasrc->fourcc, + main_buf.start, camerasrc->width, camerasrc->height, + &pData, &isize); + if (ret == FALSE) { + g_mutex_unlock( camerasrc->buffer_lock ); + return GST_FLOW_ERROR; + } + + vid_buf->is_alloc_data = TRUE; + } else { + pData = main_buf.start; + isize = main_buf.length; + } + + GST_BUFFER_DATA(vid_buf) = pData; + GST_BUFFER_SIZE(vid_buf) = isize; + vid_buf->v4l2_buffer_index = v4l2_buffer_index; + + if (camerasrc->firsttime) { + /** Because of basesrc negotiation , "framerate" field is needed. */ + int fps_nu = 0; + int fps_de = 0; + int af_mode = CAMERASRC_AF_MODE_AUTO; + int af_range = CAMERASRC_AF_RANGE_NORMAL; + gchar *caps_string = NULL; + GstCaps *caps = NULL; + + if (camerasrc->fps <= 0) { + /*if fps is zero, auto fps mode*/ + fps_nu = 0; + fps_de = 1; + } else { + fps_nu = 1; + fps_de = camerasrc->fps; + } + + GST_INFO_OBJECT(camerasrc, "FPS auto[%d], FPS[%d], High speed FPS[%d]", + camerasrc->fps_auto, camerasrc->fps, camerasrc->high_speed_fps); + + caps = gst_caps_new_simple("video/x-raw-yuv", + "format", GST_TYPE_FOURCC, camerasrc->fourcc, + "width", G_TYPE_INT, camerasrc->width, + "height", G_TYPE_INT, camerasrc->height, + "framerate", GST_TYPE_FRACTION, fps_de, fps_nu, + NULL); + if (caps == NULL) { + GST_ERROR_OBJECT(camerasrc, "failed to alloc caps"); + gst_buffer_unref((GstBuffer *)vid_buf); + vid_buf = NULL; + buf = NULL; + return GST_FLOW_ERROR; + } + + if (camerasrc->use_rotate_caps) { + gst_caps_set_simple(caps, "rotate", G_TYPE_INT, camerasrc->rotate, NULL); + } + + GST_BUFFER_CAPS(buf) = caps; + + caps_string = gst_caps_to_string(caps); + GST_INFO_OBJECT(camerasrc, "PREVIEW MODE first time [%dx%d], rotate[%d], caps[%s]", + camerasrc->width, camerasrc->height, camerasrc->rotate, caps_string); + if (caps_string) { + g_free(caps_string); + caps_string = NULL; + } + + if (camerasrc->fourcc == GST_MAKE_FOURCC('N','V','1','2') || + camerasrc->fourcc == GST_MAKE_FOURCC('S','N','1','2')) { + /* Start Focusing when Continuous AF */ + camerasrc_get_autofocusing_mode(camerasrc->v4l2_handle, &af_mode, &af_range); + if (af_mode == CAMERASRC_AF_MODE_CONTINUOUS) { + GST_INFO_OBJECT(camerasrc, "This is (NV12 or SN12) and Continuous AF mode. Start AF"); + camerasrc_start_autofocusing(camerasrc->v4l2_handle); + } + } + + camerasrc->firsttime = FALSE; + } + + gst_camerasrc_get_timeinfo(camerasrc, (GstBuffer*)vid_buf); + + *buffer = (GstBuffer*)vid_buf; + + /*GST_DEBUG_OBJECT(camerasrc, "refcount: %d", GST_OBJECT_REFCOUNT(*buffer));*/ + +#if _ENABLE_CAMERASRC_DEBUG + g_preview_frame_cnt++; +#endif + + return GST_FLOW_OK; +} + + +static GstFlowReturn gst_camerasrc_read_preview_pad_alloc(GstCameraSrc *camerasrc, GstBuffer **buffer) +{ + int ret = 0; + int v4l2_buffer_index = 0; + int is_esd = 0; + guint cnt_again = _MAX_PAD_ALLOC_RETRY_COUNT; + void *buf = NULL; + camerasrc_buffer_t main_buf; + GstBuffer *gst_buf = NULL; + GST_CAMERASRC_BUFFER_DATA *buffer_data = NULL; + + /*alloc main buffer*/ + gst_buf = *buffer; + buf = (GstBuffer *)gst_buf; + + g_mutex_lock(camerasrc->buffer_lock); + GST_LOG_OBJECT(camerasrc, "After lock(lvn %d, buf cnt %d)", camerasrc->num_live_buffers, camerasrc->buffer_count); + +AGAIN: + g_mutex_lock (camerasrc->pad_alloc_mutex); + while (!g_queue_is_empty(camerasrc->pad_alloc_list) && !camerasrc->first_invokation) { + int pad_alloc_index = -1; + int proper_index = -1; + camerasrc_buffer_t camerasrc_buffer; + GstBuffer *pad_alloc_buffer; + GstCaps *negotiated_caps = NULL; + + g_mutex_unlock (camerasrc->pad_alloc_mutex); + + camerasrc_buffer.start = NULL; + camerasrc_buffer.length = 0; + + g_mutex_lock (camerasrc->pad_alloc_mutex); + pad_alloc_index = (int)g_queue_pop_head(camerasrc->pad_alloc_list); + g_mutex_unlock (camerasrc->pad_alloc_mutex); + sched_yield(); + GST_LOG_OBJECT (camerasrc, "Index queue have item. pad_alloc_index = %d", pad_alloc_index); + + negotiated_caps = gst_pad_get_negotiated_caps (GST_BASE_SRC_PAD (camerasrc)); + + /* pad allocation from sink(or any where) */ + ret = gst_pad_alloc_buffer(GST_BASE_SRC_PAD (camerasrc), + 0, + camerasrc->main_buf_sz, + negotiated_caps, + &pad_alloc_buffer); + if (ret != GST_FLOW_OK) { + GST_ERROR_OBJECT(camerasrc, "Pad alloc fail, ret = [%d]", ret); + g_cond_signal(camerasrc->buffer_cond); + g_mutex_unlock(camerasrc->buffer_lock); + gst_caps_unref(negotiated_caps); + + return ret; + } + + proper_index = get_proper_index(camerasrc, pad_alloc_buffer); + if (proper_index == -1) { + GST_INFO_OBJECT(camerasrc, "Proper index doesn't exist"); + g_mutex_unlock(camerasrc->buffer_lock); + + return GST_FLOW_ERROR; + } + + if (proper_index != pad_alloc_index) { + GST_LOG_OBJECT(camerasrc, "Proper index different from pad_alloc_index, proper_index = %d, pad_alloc_index = %d", + proper_index, pad_alloc_index); + } + + GST_LOG_OBJECT(camerasrc, "gst_pad_alloc_buffer called. index = %d, GstBuffer address = %p", + proper_index, &(camerasrc->usr_buffer[pad_alloc_index])); + + camerasrc->usr_buffer[proper_index] = pad_alloc_buffer; + + camerasrc_buffer.start = GST_BUFFER_DATA(GST_BUFFER(camerasrc->usr_buffer[proper_index])); + camerasrc_buffer.length = GST_BUFFER_SIZE(GST_BUFFER(camerasrc->usr_buffer[proper_index])); + + camerasrc_buffer.length = PAGE_ALIGN(camerasrc_buffer.length); + + if (!camerasrc_buffer.start) { + GST_ERROR_OBJECT(camerasrc, "Data for queueing is not available one, data = %p", camerasrc_buffer.start); + g_cond_signal(camerasrc->buffer_cond); + g_mutex_unlock(camerasrc->buffer_lock); + + return GST_FLOW_ERROR; + } + + if (camerasrc_buffer.length <= 0) { + GST_ERROR_OBJECT(camerasrc, "Length for queueing is not available one, data = %d", camerasrc_buffer.length); + g_cond_signal(camerasrc->buffer_cond); + g_mutex_unlock(camerasrc->buffer_lock); + + return GST_FLOW_ERROR; + } + + ret = camerasrc_queue_buffer(camerasrc->v4l2_handle, proper_index, &camerasrc_buffer); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "Queue frame error, [%x]", ret); + g_cond_signal(camerasrc->buffer_cond); + g_mutex_unlock(camerasrc->buffer_lock); + gst_caps_unref(negotiated_caps); + + return GST_FLOW_ERROR; + } + + gst_caps_unref(negotiated_caps); + + g_cond_signal(camerasrc->buffer_cond); + GST_LOG_OBJECT(camerasrc, "QBUF : [idx=%d, lvn=%d]", proper_index, camerasrc->num_live_buffers); + g_mutex_lock(camerasrc->pad_alloc_mutex); + } + + g_mutex_unlock(camerasrc->pad_alloc_mutex); + + ret = camerasrc_wait_frame_available(camerasrc->v4l2_handle, _PAD_ALLOC_RETRY_PERIOD); + if (ret == CAMERASRC_ERR_DEVICE_WAIT_TIMEOUT) { + if (cnt_again--) { + if ((cnt_again % 100) == 0) { + GST_LOG_OBJECT (camerasrc, "Check ESD......"); + + is_esd = 0; + ret = camerasrc_check_esd_shock(camerasrc->v4l2_handle, &is_esd); + if (is_esd) { + /* ESD situation */ + GST_LOG_OBJECT(camerasrc, "ESD situation. device reset preview"); + GST_LOG_OBJECT(camerasrc, "register trouble error!! [%x]", CAMERASRC_ERR_DEVICE_UNAVAILABLE); + g_mutex_unlock(camerasrc->buffer_lock); + + gst_camerasrc_error_handler(camerasrc, CAMERASRC_ERR_DEVICE_UNAVAILABLE); + + return GST_FLOW_ERROR; + } + } + goto AGAIN; + } else { + GST_LOG_OBJECT (camerasrc, "Check ESD......"); + + is_esd = 0; + ret = camerasrc_check_esd_shock(camerasrc->v4l2_handle, &is_esd); + if (!is_esd) { + /* Not ESD situation */ + GST_LOG_OBJECT(camerasrc, "Not ESD situation. normal timeout"); + g_mutex_unlock(camerasrc->buffer_lock); + + gst_camerasrc_error_handler(camerasrc, CAMERASRC_ERR_DEVICE_WAIT_TIMEOUT); + + return GST_FLOW_WRONG_STATE; + } else { + /* ESD situation */ + GST_LOG_OBJECT(camerasrc, "ESD situation. device reset preview"); + GST_LOG_OBJECT(camerasrc, "register trouble error!! [%x]", CAMERASRC_ERR_DEVICE_UNAVAILABLE); + g_mutex_unlock(camerasrc->buffer_lock); + + gst_camerasrc_error_handler(camerasrc, CAMERASRC_ERR_DEVICE_UNAVAILABLE); + + return GST_FLOW_ERROR; + } + + GST_ERROR_OBJECT(camerasrc, "Time out retried [%d] times %d", _MAX_PAD_ALLOC_RETRY_COUNT - cnt_again + 1); + } + } else if (ret != CAMERASRC_SUCCESS) { + g_mutex_unlock(camerasrc->buffer_lock); + GST_ERROR_OBJECT(camerasrc, "camerasrc_wait_frame_available error [%x]", ret); + + return GST_FLOW_ERROR; + } + + /* Buffer DQ */ + __ta__( " camerasrc_dequeue_buffer", + ret = camerasrc_dequeue_buffer(camerasrc->v4l2_handle, &v4l2_buffer_index, &main_buf, NULL); + ); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "Dequeue frame error, [%x]", ret); + g_mutex_unlock(camerasrc->buffer_lock); + gst_camerasrc_error_handler(camerasrc, ret); + + return GST_FLOW_ERROR; + } + + camerasrc->num_live_buffers++; + GST_LOG_OBJECT(camerasrc, "after : DQBUF (index %d, live number %d)", + v4l2_buffer_index, camerasrc->num_live_buffers); + g_mutex_unlock(camerasrc->buffer_lock); + + /* For camera state stopped */ + gst_buf = camerasrc->usr_buffer[v4l2_buffer_index]; + if (gst_buf == NULL) { + GST_ERROR_OBJECT(camerasrc, "camerasrc->usr_buffer[v4l2_buffer_index] NULL"); + return GST_FLOW_WRONG_STATE; + } + + GST_LOG_OBJECT(camerasrc, "Start to set up buffer free structure"); + + buffer_data = g_malloc(sizeof(GST_CAMERASRC_BUFFER_DATA)); + if (buffer_data == NULL) { + GST_ERROR_OBJECT(camerasrc, "buffer_data NULL"); + return GST_FLOW_WRONG_STATE; + } + + buffer_data->camerasrc = camerasrc; + buffer_data->index = v4l2_buffer_index; + buffer_data->buffer_data_index = camerasrc->current_buffer_data_index; + + GST_BUFFER_DATA(gst_buf) = main_buf.start; + GST_BUFFER_SIZE(gst_buf) = main_buf.length; + GST_BUFFER_MALLOCDATA(gst_buf) = (guint8 *)buffer_data; + GST_BUFFER_FREE_FUNC(gst_buf) = gst_camerasrc_buffer_free; + + GST_LOG_OBJECT(camerasrc, "End to set up buffer free structure"); + + + if (camerasrc->firsttime) { + /* Because of basesrc negotiation , "framerate" field is needed. */ + int fps_nu = 0; + int fps_de = 0; + gchar *caps_string = NULL; + GstCaps *caps = NULL; + + if (camerasrc->fps <= 0) { + /*if fps is zero, auto fps mode*/ + fps_nu = 0; + fps_de = 1; + } else { + fps_nu = 1; + fps_de = camerasrc->fps; + } + + GST_INFO_OBJECT(camerasrc, "FPS auto[%d], FPS[%d], High speed FPS[%d]", + camerasrc->fps_auto, camerasrc->fps, camerasrc->high_speed_fps); + + caps = gst_caps_new_simple("video/x-raw-yuv", + "format", GST_TYPE_FOURCC, camerasrc->fourcc, + "width", G_TYPE_INT, camerasrc->width, + "height", G_TYPE_INT, camerasrc->height, + "framerate", GST_TYPE_FRACTION, fps_de, fps_nu, + NULL); + if (caps == NULL) { + GST_ERROR_OBJECT(camerasrc, "failed to alloc caps"); + return GST_FLOW_ERROR; + } + + if (camerasrc->use_rotate_caps) { + gst_caps_set_simple(caps, "rotate", G_TYPE_INT, camerasrc->rotate, NULL); + } + + GST_BUFFER_CAPS(gst_buf) = caps; + + caps_string = gst_caps_to_string(caps); + GST_INFO_OBJECT (camerasrc, "PREVIEW MODE first time [%dx%d], rotate[%d], caps[%s]", + camerasrc->width, camerasrc->height, camerasrc->rotate, caps_string); + if (caps_string) { + g_free(caps_string); + caps_string = NULL; + } + + camerasrc->firsttime = FALSE; + } + + gst_camerasrc_get_timeinfo(camerasrc, gst_buf); + + *buffer = gst_buf; + + g_mutex_lock (camerasrc->pad_alloc_mutex); + if (camerasrc->first_invokation) { + GST_INFO_OBJECT(camerasrc, "[DEBUG] Check something in pad_alloc_list"); + g_mutex_unlock (camerasrc->pad_alloc_mutex); + g_mutex_lock (camerasrc->pad_alloc_mutex); + + while (!g_queue_is_empty(camerasrc->pad_alloc_list)) { + g_mutex_unlock(camerasrc->pad_alloc_mutex); + g_mutex_lock(camerasrc->pad_alloc_mutex); + + /* Remove all item */ + + g_queue_pop_head(camerasrc->pad_alloc_list); + GST_INFO_OBJECT(camerasrc, "[DEBUG] Something is in pad_alloc_list before first frame. remove it!"); + + g_mutex_unlock(camerasrc->pad_alloc_mutex); + g_mutex_lock(camerasrc->pad_alloc_mutex); + } + + g_mutex_unlock(camerasrc->pad_alloc_mutex); + g_mutex_lock(camerasrc->pad_alloc_mutex); + camerasrc->first_invokation = FALSE; + } + g_mutex_unlock (camerasrc->pad_alloc_mutex); + +#if _ENABLE_CAMERASRC_DEBUG + g_preview_frame_cnt++; +#endif + + return GST_FLOW_OK; +} + + +static GstFlowReturn gst_camerasrc_draw_black_preview(GstCameraSrc *camerasrc, GstBuffer **buffer) +{ + int fps_nu = 0; + int fps_de = 0; + guint fakebuflen = 0; + GstBuffer *gst_buf = NULL; + + /*alloc main buffer*/ + if (camerasrc->fourcc == GST_MAKE_FOURCC('Y','U','Y','2')) { + fakebuflen = camerasrc->width * camerasrc->height * 2; + gst_buf = gst_buffer_new_and_alloc(fakebuflen); + GenerateYUV422BlackFrame(GST_BUFFER_MALLOCDATA(gst_buf), fakebuflen, camerasrc->width , camerasrc->height); + } else if (camerasrc->fourcc == GST_MAKE_FOURCC ('I','4','2','0') || + camerasrc->fourcc == GST_MAKE_FOURCC ('Y', 'U', '1', '2')) { + fakebuflen = camerasrc->width * camerasrc->height * 3 / 2; + gst_buf = gst_buffer_new_and_alloc(fakebuflen); + GenerateYUV420BlackFrame(GST_BUFFER_MALLOCDATA(gst_buf), fakebuflen, camerasrc->width , camerasrc->height); + } else { + GST_DEBUG_OBJECT(camerasrc, "Wrong fourcc(%x)", camerasrc->fourcc); + return GST_FLOW_OK; + } + + GST_DEBUG_OBJECT(camerasrc, "gst_camerasrc_draw_black_preview (data=%p, len=%d)", + GST_BUFFER_MALLOCDATA(gst_buf), fakebuflen); + /** + Because of basesrc negotiation , "framerate" field is needed. + */ + if (camerasrc->fps <= 0) { + /*if fps is zero, auto fps mod e*/ + fps_nu = 0; + fps_de = 1; + } else { + fps_nu = 1; + fps_de = camerasrc->fps; + } + + GST_INFO_OBJECT(camerasrc, "FPS auto[%d], FPS[%d], High speed FPS[%d]", + camerasrc->fps_auto, camerasrc->fps, camerasrc->high_speed_fps); + + GST_BUFFER_CAPS(gst_buf) = gst_caps_new_simple("video/x-raw-yuv", + "format", GST_TYPE_FOURCC, camerasrc->fourcc, + "width", G_TYPE_INT, camerasrc->width, + "height", G_TYPE_INT, camerasrc->height, + "framerate", GST_TYPE_FRACTION, fps_de, fps_nu, + NULL); + + GST_DEBUG_OBJECT(camerasrc, "PREVIEW MODE first time [%d] [%d]", camerasrc->width, camerasrc->height); + gst_camerasrc_get_timeinfo(camerasrc, gst_buf); + + *buffer = gst_buf; + return GST_FLOW_OK; +} + + +static GstFlowReturn gst_camerasrc_read_capture(GstCameraSrc *camerasrc, GstBuffer **buffer, int command) +{ + int ret; + int buffer_index = 0; + unsigned long cur_time; + gboolean is_jpeg = FALSE; + + static gboolean get_stop_command = FALSE; + static gboolean get_stop_multi_command = FALSE; + + GstCameraBuffer *buf = NULL; /*output buffer for preview*/ + GstBuffer *buf_cap_signal1 = NULL; /*output main buffer for capture signal*/ + GstBuffer *buf_cap_signal2 = NULL; /*output thumbnail buffer for capture signal*/ + GstBuffer *buf_cap_signal3 = NULL; /*output screennail buffer for capture signal*/ + + camerasrc_buffer_t main_buf = {0, NULL, 0}; /*original size buffer*/ + camerasrc_buffer_t thumb_buf = {0, NULL, 0}; /*thumbnail size buffer*/ + camerasrc_buffer_t scrnl_buf = {0, NULL, 0}; /*screennail size buffer*/ + + GST_DEBUG_OBJECT(camerasrc, "ENTERED. Command[%d]", command); + + GST_INFO_OBJECT(camerasrc, "src size[%dx%d], capture size[%dx%d]", + camerasrc->width, camerasrc->height, + camerasrc->cap_width, camerasrc->cap_height ); + + if (command == GST_CAMERA_CONTROL_CAPTURE_COMMAND_STOP) { + get_stop_command = TRUE; + } else if (command == GST_CAMERA_CONTROL_CAPTURE_COMMAND_STOP_MULTISHOT) { + get_stop_multi_command = TRUE; + } + + GST_INFO_OBJECT(camerasrc, "cnt current:%d, reverse:%d, stop cmd:%d, multi stop cmd:%d", + camerasrc->cap_count_reverse, camerasrc->cap_count_current, + get_stop_command, get_stop_multi_command); + + while (TRUE) { + if (camerasrc->cap_count_reverse == 0 || + ((get_stop_command || get_stop_multi_command) && + camerasrc->cap_count_current != 0 )) { + g_mutex_lock(camerasrc->mutex); + + GST_INFO_OBJECT(camerasrc, "Capture finished."); + + __ta__( " capture: gst_camerasrc_capture_stop", + gst_camerasrc_capture_stop(camerasrc); + ); + if (get_stop_command == FALSE) { + if (!g_queue_is_empty(camerasrc->command_list)) { + command = (int)g_queue_pop_head(camerasrc->command_list); + GST_INFO_OBJECT(camerasrc, "Pop command [%d]", command); + if (command == GST_CAMERA_CONTROL_CAPTURE_COMMAND_STOP) { + get_stop_command = TRUE; + } + } + + if (get_stop_command == FALSE) { + GST_INFO_OBJECT(camerasrc, "Start : Wait for Capture stop signal"); + __ta__( " capture: wait for cond after image capture", + g_cond_wait(camerasrc->cond, camerasrc->mutex); + ); + GST_INFO_OBJECT(camerasrc, "End : Wait for Capture stop signal"); + } + } + + __ta__(" capture: gst_camerasrc_start", + gst_camerasrc_start(camerasrc); + ); + + __ta__(" capture: one gst_camerasrc_read_preview_mmap", + ret = gst_camerasrc_read_preview_mmap(camerasrc, buffer); + ); + + get_stop_command = FALSE; + get_stop_multi_command = FALSE; + + g_mutex_unlock(camerasrc->mutex); + + MMTA_ACUM_ITEM_END( " Shot to Shot in gstcamerasrc", FALSE); + + return ret; + } + + if (camerasrc->cap_fourcc == GST_MAKE_FOURCC('J','P','E','G') || + camerasrc->cap_fourcc == GST_MAKE_FOURCC('j','p','e','g')) { + is_jpeg = TRUE; + } else { + is_jpeg = FALSE; + } + + /** + *@important JPEG still: always same image generated by camerasrc_read_frame. + * if you want to multi shot, set YUV format ! + */ + __ta__(" camerasrc_read_frame:select,DQ,Copy,Q", + ret = camerasrc_read_frame(camerasrc->v4l2_handle, &main_buf, &thumb_buf, &buffer_index); + ); + if (ret != CAMERASRC_SUCCESS) { + if (ret == CAMERASRC_ERR_DEVICE_UNAVAILABLE) { + GST_ERROR_OBJECT (camerasrc, "Video src device return register trouble error!! [%x]", ret); + /*g_signal_emit (G_OBJECT (camerasrc), gst_camerasrc_signals[SIGNAL_REGISTER_TROUBLE], (GQuark)NULL);*/ + gst_camerasrc_error_handler(camerasrc, ret); + return GST_FLOW_ERROR; + } else { + GST_ERROR_OBJECT (camerasrc, "camerasrc_read_frame() failed. [ret = 0x%08X]", ret); + GST_ERROR_OBJECT (camerasrc, "return GST_FLOW_ERROR"); + /* should stop capture; */ + /*retrun EOS*/ + *buffer = NULL; + gst_camerasrc_error_handler(camerasrc, ret); + + return GST_FLOW_ERROR; + } + } + + /* Get screennail buffer */ + scrnl_buf.start = NULL; + scrnl_buf.length = 0; + camerasrc_get_screennail_buffer(camerasrc->v4l2_handle, &scrnl_buf); + + GST_INFO_OBJECT(camerasrc, "main(%p,%d), thumb(%p,%d), scrnl(%p,%d)", + main_buf.start, main_buf.length, + thumb_buf.start, thumb_buf.length, + scrnl_buf.start, scrnl_buf.length); + +CHECK_CAPTURE_INTERVAL: + /* get shot time */ + cur_time = gst_get_current_time(); + + if (camerasrc->cap_next_time == 0UL) { + camerasrc->cap_next_time = cur_time; + } + + if (camerasrc->cap_count_reverse > 0 && camerasrc->cap_next_time <= cur_time) { + GST_INFO_OBJECT(camerasrc, "CHECK: reverse capture count: %d, next time:%lu current time:%lu", + camerasrc->cap_count_reverse, camerasrc->cap_next_time, cur_time); + + camerasrc->cap_next_time = cur_time + camerasrc->cap_interval; + camerasrc->cap_count_reverse--; + camerasrc->cap_count_current++; + + /* alloc buffer for capture callback */ + buf_cap_signal1 = gst_buffer_new (); + + /* make buffers for capture callback and display(raw format) */ + if (is_jpeg) { + GST_INFO_OBJECT (camerasrc, "JPEG CAPTURE MODE"); + + GST_BUFFER_DATA(buf_cap_signal1) = main_buf.start; + GST_BUFFER_SIZE(buf_cap_signal1) = main_buf.length; + GST_BUFFER_CAPS(buf_cap_signal1) = gst_caps_new_simple("image/jpeg", + "width", G_TYPE_INT, camerasrc->cap_width, + "height", G_TYPE_INT, camerasrc->cap_height, + NULL); + + if (thumb_buf.start) { + buf_cap_signal2 = gst_buffer_new(); + GST_BUFFER_DATA(buf_cap_signal2) = thumb_buf.start; + GST_BUFFER_SIZE(buf_cap_signal2) = thumb_buf.length; + GST_BUFFER_CAPS(buf_cap_signal2) = gst_caps_new_simple("image/jpeg", + "width", G_TYPE_INT, _THUMBNAIL_WIDTH, + "height", G_TYPE_INT, _THUMBNAIL_HEIGHT, + NULL); + } else { + buf_cap_signal2 = NULL; + } + + if (scrnl_buf.start && scrnl_buf.length > 0) { + buf_cap_signal3 = gst_buffer_new(); + GST_BUFFER_DATA(buf_cap_signal3) = scrnl_buf.start; + GST_BUFFER_SIZE(buf_cap_signal3) = scrnl_buf.length; + GST_BUFFER_CAPS(buf_cap_signal3) = gst_caps_new_simple("video/x-raw-yuv", + "format", GST_TYPE_FOURCC, _DEFAULT_SCRNL_FOURCC, + "width", G_TYPE_INT, camerasrc->width, + "height", G_TYPE_INT, camerasrc->height, + NULL); + } else { + buf_cap_signal3 = NULL; + } + + *buffer = NULL; + } else { + unsigned char *pMetaData = NULL; + + GST_INFO_OBJECT (camerasrc, "RAW CAPTURE MODE"); + + /*alloc main buffer*/ + buf = gst_camerasrc_buffer_new(camerasrc);; + if (buf == NULL) { + GST_ERROR_OBJECT(camerasrc, "Buffer alloc failed."); + *buffer = NULL; + gst_camerasrc_error_handler(camerasrc, CAMERASRC_ERR_ALLOCATION); + return GST_FLOW_ERROR; + } + + pMetaData = gst_camerasrc_make_metadata_for_nonlinear_buffer(camerasrc, buffer_index); + if (!pMetaData) { + GST_ERROR_OBJECT(camerasrc, "Error on making metadata"); + } + + GST_BUFFER_MALLOCDATA(buf) = pMetaData; + buf->v4l2_buffer_index = buffer_index; + + if (camerasrc->cap_fourcc == GST_MAKE_FOURCC('I','4','2','0') || + camerasrc->cap_fourcc == GST_MAKE_FOURCC('S','4','2','0') || + camerasrc->cap_fourcc == GST_MAKE_FOURCC('N','V','1','2') || + camerasrc->cap_fourcc == GST_MAKE_FOURCC('S','N','1','2')) { + ret = _gst_camerasrc_get_normal_buffer(camerasrc, camerasrc->cap_fourcc, + main_buf.start, camerasrc->cap_width, camerasrc->cap_height, + &GST_BUFFER_DATA(buf), &GST_BUFFER_SIZE(buf)); + if (ret == FALSE) { + g_mutex_unlock( camerasrc->buffer_lock ); + return GST_FLOW_ERROR; + } + + buf->is_alloc_data = TRUE; + } else { + GST_BUFFER_DATA(buf) = main_buf.start; + GST_BUFFER_SIZE(buf) = main_buf.length; + } + + *buffer = (GstBuffer *)buf; + GST_INFO_OBJECT(camerasrc, "BUF for PREVIEW: addr %p size %d", + GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf)); + + GST_BUFFER_DATA(buf_cap_signal1) = GST_BUFFER_DATA(buf); + GST_BUFFER_SIZE(buf_cap_signal1) = GST_BUFFER_SIZE(buf); + GST_BUFFER_CAPS(buf_cap_signal1) = gst_caps_new_simple("video/x-raw-yuv", + "format", GST_TYPE_FOURCC, camerasrc->cap_fourcc, + "width", G_TYPE_INT, camerasrc->cap_width, + "height", G_TYPE_INT, camerasrc->cap_height, + NULL); + buf_cap_signal2 = NULL; + buf_cap_signal3 = NULL; + } + + /*call signal*/ + GST_INFO_OBJECT (camerasrc, "CALL: capture callback"); + + g_signal_emit( G_OBJECT (camerasrc), + gst_camerasrc_signals[SIGNAL_STILL_CAPTURE], + 0, + buf_cap_signal1, + buf_cap_signal2, + buf_cap_signal3 ); + + GST_INFO_OBJECT (camerasrc, "RETURN: capture callback"); + + if (is_jpeg || + (!is_jpeg && (camerasrc->fourcc != camerasrc->cap_fourcc || + camerasrc->width != camerasrc->cap_width || + camerasrc->height != camerasrc->cap_height))) { + /* Queue buffer */ + camerasrc_queue_buffer(camerasrc->v4l2_handle, buffer_index, &main_buf); + + /* release buffer */ + if (*buffer) { + gst_buffer_unref(*buffer); + *buffer = NULL; + } + } else { + camerasrc->num_live_buffers++; + /*escape loop for passing buffer to videosink*/ + break; + } + } else { + if (camerasrc->cap_next_time < cur_time + _CONTINUOUS_SHOT_MARGIN) { + GST_DEBUG_OBJECT(camerasrc, "check again time"); + usleep((camerasrc->cap_next_time - cur_time) * 1000); + goto CHECK_CAPTURE_INTERVAL; + } + + /* RAW capture buffer should be reach here */ + camerasrc_queue_buffer(camerasrc->v4l2_handle, buffer_index, &main_buf); + + /* Skip passing this buffer */ + *buffer = NULL; + + GST_DEBUG_OBJECT(camerasrc, "Skip pssing this buffer"); + break; + } + } + + GST_DEBUG_OBJECT (camerasrc, "LEAVED"); + + return GST_FLOW_OK; +} + + +static GstFlowReturn gst_camerasrc_read(GstCameraSrc *camerasrc, GstBuffer **buffer) +{ + int err = 0; + int command = GST_CAMERA_CONTROL_CAPTURE_COMMAND_NONE; + GstFlowReturn ret = GST_FLOW_OK; + camerasrc_state_t state = CAMERASRC_STATE_NONE; + + g_mutex_lock(camerasrc->mutex); + + if (!g_queue_is_empty(camerasrc->command_list)) { + command = (int)g_queue_pop_head(camerasrc->command_list); + GST_INFO_OBJECT(camerasrc, "popped cmd : %d", command); + } + + /* Normal Capture Routine */ + if (command == GST_CAMERA_CONTROL_CAPTURE_COMMAND_START) { + MMTA_ACUM_ITEM_BEGIN(" Shot to Shot in gstcamerasrc", FALSE); + __ta__(" gst_camerasrc_capture_start", + gst_camerasrc_capture_start(camerasrc); + ); + } + + g_mutex_unlock(camerasrc->mutex); + + switch (camerasrc->mode) { + case VIDEO_IN_MODE_PREVIEW: + case VIDEO_IN_MODE_VIDEO: + if (camerasrc->use_pad_alloc == TRUE) { + err = camerasrc_get_state(camerasrc->v4l2_handle, &state); + + if (state == CAMERASRC_STATE_READY) { + GST_INFO_OBJECT (camerasrc,"Prepare buffer"); + ret = gst_camerasrc_prepare_preview(camerasrc, camerasrc->num_alloc_buf); + } + } + + if (camerasrc->use_pad_alloc == TRUE) { + __ta__(" gst_camerasrc_read_preview_pad_alloc", + ret = gst_camerasrc_read_preview_pad_alloc(camerasrc, buffer); + ); + } else { + __ta__(" gst_camerasrc_read_preview_mmap", + ret = gst_camerasrc_read_preview_mmap(camerasrc, buffer); + ) + } + break; + case VIDEO_IN_MODE_CAPTURE: + __ta__( " gst_camerasrc_read_capture", + ret = gst_camerasrc_read_capture(camerasrc, buffer, command); + ); + break; + case VIDEO_IN_MODE_UNKNOWN: + default: + ret = GST_FLOW_ERROR; + GST_ERROR_OBJECT (camerasrc, "can't reach statement.[camerasrc->mode=%d]", camerasrc->mode); + break; + } + + if (!buffer || !(*buffer) || !GST_IS_BUFFER(*buffer)) { + /* To avoid seg fault, make dummy buffer. */ + GST_WARNING_OBJECT (camerasrc, "Make a dummy buffer"); + *buffer = gst_buffer_new(); + GST_BUFFER_DATA(*buffer) = NULL; + GST_BUFFER_SIZE(*buffer) = 0; + } + + return ret; +} + + +/* Buffer related functions */ +static void gst_camerasrc_buffer_pad_alloc_qbuf(GstCameraBuffer *buffer) +{ + int ret = 0; + gint index = 0; + GstCaps *negotiated_caps = NULL; + GstBuffer *queue_buf = NULL; + GstCameraSrc *camerasrc = NULL; + camerasrc_buffer_t camerasrc_buffer; + + if (buffer) { + camerasrc = buffer->camerasrc; + } else { + GST_ERROR("buffer is NULL"); + return; + } + + if (camerasrc) { + negotiated_caps = gst_pad_get_negotiated_caps(GST_BASE_SRC_PAD(camerasrc)); + } else { + GST_ERROR("camerasrc is NULL"); + return; + } + + index = buffer->v4l2_buffer_index; + + GST_LOG_OBJECT(camerasrc, "pad alloc qbuf"); + + ret = gst_pad_alloc_buffer(GST_BASE_SRC_PAD (camerasrc), + 0, + camerasrc->main_buf_sz, + negotiated_caps, + &queue_buf); + if (!GST_IS_BUFFER(queue_buf)) { + GST_INFO_OBJECT (camerasrc, "[DEBUG] NOT BUFFER!!?"); + } + + if (ret != GST_FLOW_OK) { + GST_ERROR_OBJECT(camerasrc, "gst_pad_alloc_buffer failed. [%d]", ret); + return; + } else { + GST_LOG_OBJECT(camerasrc, "Alloced Size = %dAlloced address : %p", + GST_BUFFER_SIZE(GST_BUFFER(queue_buf)), GST_BUFFER_DATA(GST_BUFFER(queue_buf))); + } + + gst_caps_unref(negotiated_caps); + negotiated_caps = NULL; + + camerasrc_buffer.start = GST_BUFFER_DATA(GST_BUFFER(queue_buf)); + camerasrc_buffer.length = GST_BUFFER_SIZE(GST_BUFFER(queue_buf)); + ret = camerasrc_queue_buffer(camerasrc->v4l2_handle, index, &camerasrc_buffer); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "QBUF error, [%x]", ret); + return; + } else { + GST_LOG_OBJECT (camerasrc, "QBUF : [idx=%d]", index); + } + + return; +} + + +static void gst_camerasrc_buffer_mmap_qbuf(GstCameraBuffer *buffer) +{ + int ret = 0; + gint index = 0; + GstCameraSrc *camerasrc = NULL; + camerasrc_buffer_t camerasrc_buffer; + + camerasrc = buffer->camerasrc; + index = buffer->v4l2_buffer_index; + + ret = camerasrc_queue_buffer(camerasrc->v4l2_handle, index, &camerasrc_buffer); + if (ret != CAMERASRC_SUCCESS) { + GST_ERROR_OBJECT(camerasrc, "QBUF error, [%x]", ret); + } else { + GST_LOG_OBJECT(camerasrc, "QBUF : [idx=%d]", index); + } + + return; +} + +static void gst_camerasrc_buffer_finalize(GstCameraBuffer *buffer) +{ + gint index = 0; + GstCameraSrc *camerasrc = NULL; + + camerasrc = buffer->camerasrc; + index = buffer->v4l2_buffer_index; + + GST_LOG_OBJECT(camerasrc, "finalizing buffer %p, %d [%" + GST_TIME_FORMAT " dur %" GST_TIME_FORMAT "]", buffer, index, + GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)), + GST_TIME_ARGS(GST_BUFFER_DURATION(buffer))); + + if (camerasrc->buffer_running) { + /* Buffer Q again */ + if (camerasrc->use_pad_alloc == TRUE){ + gst_camerasrc_buffer_pad_alloc_qbuf(buffer); + } else { + gst_camerasrc_buffer_mmap_qbuf(buffer); + } + } else { + GST_INFO_OBJECT(camerasrc, "It is not running. skip QBUF"); + } + + camerasrc->num_live_buffers--; + g_cond_signal(camerasrc->buffer_cond); + GST_LOG_OBJECT(camerasrc, "QBUF : [idx=%d, lvn=%d]", index, camerasrc->num_live_buffers); + + if (buffer->is_alloc_data) { + GST_DEBUG("free allocated data %p", GST_BUFFER_DATA(buffer)); + if (GST_BUFFER_DATA(buffer)) { + free(GST_BUFFER_DATA(buffer)); + GST_BUFFER_DATA(buffer) = NULL; + } + + buffer->is_alloc_data = FALSE; + } + + if (GST_BUFFER_MALLOCDATA(buffer)) { + free(GST_BUFFER_MALLOCDATA(buffer)); + GST_BUFFER_MALLOCDATA(buffer) = NULL; + } + + gst_object_unref(camerasrc); + + if (GST_MINI_OBJECT_CLASS (camera_buffer_parent_class)->finalize) { + GST_MINI_OBJECT_CLASS (camera_buffer_parent_class)->finalize (GST_MINI_OBJECT(buffer)); + } + + return; +} + + +static void gst_camerasrc_buffer_class_init(gpointer g_class, gpointer class_data) +{ + GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS(g_class); + + camera_buffer_parent_class = g_type_class_peek_parent(g_class); + mini_object_class->finalize = (GstMiniObjectFinalizeFunction)gst_camerasrc_buffer_finalize; +} + + +static GType gst_camerasrc_buffer_get_type(void) +{ + static GType _gst_camerasrc_buffer_type; + + if (G_UNLIKELY(_gst_camerasrc_buffer_type == 0)) { + static const GTypeInfo camera_buffer_info = { + sizeof (GstBufferClass), + NULL, + NULL, + gst_camerasrc_buffer_class_init, + NULL, + NULL, + sizeof (GstCameraBuffer), + 0, + NULL, + NULL + }; + + _gst_camerasrc_buffer_type = g_type_register_static(GST_TYPE_BUFFER, + "GstCameraBuffer", + &camera_buffer_info, 0); + } + + return _gst_camerasrc_buffer_type; +} + + +static GstCameraBuffer *gst_camerasrc_buffer_new(GstCameraSrc *camerasrc) +{ + GstCameraBuffer *ret = NULL; + + ret = (GstCameraBuffer *)gst_mini_object_new(GST_TYPE_CAMERASRC_BUFFER); + + GST_LOG_OBJECT(camerasrc, "creating buffer : %p", ret); + + ret->camerasrc = gst_object_ref(GST_OBJECT(camerasrc)); + ret->is_alloc_data = FALSE; + + return ret; +} + + +static void gst_camerasrc_buffer_free(gpointer data) +{ + GST_CAMERASRC_BUFFER_DATA *buffer_data = (GST_CAMERASRC_BUFFER_DATA *)data; + + GST_LOG_OBJECT(buffer_data->camerasrc, "freeing buffer with %p %d", + buffer_data->camerasrc, buffer_data->index); + + g_mutex_lock(buffer_data->camerasrc->pad_alloc_mutex); + + if (buffer_data->buffer_data_index == buffer_data->camerasrc->current_buffer_data_index) { + GST_LOG_OBJECT(buffer_data->camerasrc, "Push index [%d] to pad_alloc_list", buffer_data->index); + g_queue_push_tail(buffer_data->camerasrc->pad_alloc_list, (gpointer)buffer_data->index); + } else { + GST_WARNING_OBJECT(buffer_data->camerasrc, "Skip Pushing index [%d] to pad_alloc_list.", buffer_data->index ); + GST_WARNING_OBJECT(buffer_data->camerasrc, "buffer_data_index [%d], current_buffer_data_index [%d]", + buffer_data->buffer_data_index, + buffer_data->camerasrc->current_buffer_data_index); + } + + buffer_data->camerasrc->num_live_buffers--; + + g_mutex_unlock(buffer_data->camerasrc->pad_alloc_mutex); + g_cond_signal(buffer_data->camerasrc->buffer_cond); + + buffer_data->camerasrc = NULL; + buffer_data->index = -1; + + /* Free data argument */ + g_free(data); + + return; +} + + +static void gst_camerasrc_buffer_trace(GstCameraSrc *camerasrc) +{ + int i = 0; + GstBuffer *buf = NULL; + + if (!camerasrc) { + GST_ERROR_OBJECT(camerasrc, "Element is NULL"); + return; + } + + for (i = 0 ; i < camerasrc->num_alloc_buf ; i ++) { + buf = camerasrc->usr_buffer[i]; + if (GST_IS_BUFFER(buf)) { + GST_ELEMENT_WARNING(camerasrc, RESOURCE, FAILED, + NULL, (("Chainfunc of buf[%d]=%s()"), i, + GST_DEBUG_FUNCPTR_NAME(buf->_gst_reserved[0]))); + } else { + GST_ELEMENT_WARNING(camerasrc, RESOURCE, FAILED, + NULL, (("buf[%d] is not GstBuffer"), i)); + } + } + + return; +} + + +static unsigned char *gst_camerasrc_make_metadata_for_nonlinear_buffer(GstCameraSrc *camerasrc, guint32 index) +{ + unsigned char *meta_string = NULL; + camerasrc_frame_data_t data; + SCMN_IMGB *psimgb = NULL; + + /*GST_LOG_OBJECT (camerasrc, "index[%d],pix_format[%x]", index, camerasrc->fourcc);*/ + + psimgb = (SCMN_IMGB *)malloc(sizeof(SCMN_IMGB)); + if (psimgb == NULL) { + GST_ERROR_OBJECT(camerasrc, "failed to alloc SCMN_IMGB"); + return NULL; + } + + memset(psimgb, 0x00, sizeof(SCMN_IMGB)); + + data.index = index; + camerasrc_get_frame_data(camerasrc->v4l2_handle, &data); + + psimgb->p[0] = (void *)data.phyAddrY; + psimgb->a[0] = (void *)data.virAddrY; + psimgb->p[1] = (void *)data.phyAddrCbCr; + psimgb->a[1] = (void *)data.virAddrCbCr; + + psimgb->w[0] = camerasrc->width; + psimgb->h[0] = camerasrc->height; + + if (camerasrc->fourcc == GST_MAKE_FOURCC('S','T','1','2')) { + psimgb->cs = SCMN_CS_NV12; + psimgb->w[1] = camerasrc->width; + psimgb->h[1] = camerasrc->height >> 1; + + psimgb->s[0] = GST_ROUND_UP_16(psimgb->w[0]); + psimgb->e[0] = GST_ROUND_UP_16(psimgb->h[0]); + psimgb->s[1] = GST_ROUND_UP_16(psimgb->w[1]); + psimgb->e[1] = GST_ROUND_UP_16(psimgb->h[1]); + } else { + if (camerasrc->fourcc == GST_MAKE_FOURCC('S','N','1','2')) { + psimgb->cs = SCMN_CS_NV12_T64X32; + psimgb->w[1] = camerasrc->width; + psimgb->h[1] = camerasrc->height >> 1; + } else if (camerasrc->fourcc == GST_MAKE_FOURCC('S','U','Y','V')) { + psimgb->cs = SCMN_CS_YUYV; + } else if (camerasrc->fourcc == GST_MAKE_FOURCC('S','U','Y','2')) { + psimgb->cs = SCMN_CS_YUY2; + } else if (camerasrc->fourcc == GST_MAKE_FOURCC('S','Y','V','Y')) { + psimgb->cs = SCMN_CS_UYVY; + } else if (camerasrc->fourcc == GST_MAKE_FOURCC('S','4','2','0')) { + guint offset = 0; + + psimgb->cs = SCMN_CS_I420; + offset = CAMERASRC_ALIGN(camerasrc->width * camerasrc->height >> 2, ALIGN_SIZE_I420); + psimgb->p[2] = (void *)(data.phyAddrCbCr + offset); + psimgb->a[2] = (void *)(data.virAddrCbCr + offset); + + psimgb->w[1] = psimgb->w[2] = camerasrc->width; + psimgb->h[1] = psimgb->h[2] = camerasrc->height >> 2; + } else { + GST_WARNING_OBJECT (camerasrc, "Unknown pixel format."); + } + + psimgb->s[0] = psimgb->w[0]; + psimgb->e[0] = psimgb->h[0]; + psimgb->s[1] = psimgb->w[1]; + psimgb->e[1] = psimgb->h[1]; + psimgb->s[2] = psimgb->w[2]; + psimgb->e[2] = psimgb->h[2]; + } + + /*GST_LOG_OBJECT(camerasrc, "index[%d],Plane0[%p],Plane1[%p],Plane2[%p]", + index, psimgb->p[0], psimgb->p[1], psimgb->p[2]);*/ + meta_string = (unsigned char*)psimgb; + + return meta_string; +} + + +static gboolean gst_camerasrc_device_is_open(GstCameraSrc *camerasrc) +{ + return camerasrc_device_is_open((camsrc_handle_t)camerasrc->v4l2_handle); +} + + +static gboolean gst_camerasrc_get_timeinfo(GstCameraSrc *camerasrc, GstBuffer *buffer) +{ + int fps_nu = 0; + int fps_de = 0; + GstClock *clock = NULL; + GstClockTime timestamp = GST_CLOCK_TIME_NONE; + GstClockTime duration = GST_CLOCK_TIME_NONE; + + if (!camerasrc || !buffer) { + GST_WARNING_OBJECT (camerasrc, "Invalid pointer [hadle:%p, buffer:%p]", camerasrc, buffer); + return FALSE; + } + + /* timestamps, LOCK to get clock and base time. */ + clock = GST_ELEMENT_CLOCK(camerasrc); + if (clock) { + /* the time now is the time of the clock minus the base time */ + gst_object_ref(clock); + timestamp = gst_clock_get_time(clock) - GST_ELEMENT(camerasrc)->base_time; + gst_object_unref(clock); + + /* if we have a framerate adjust timestamp for frame latency */ + if (camerasrc->fps_auto) { + /*if fps is zero, auto fps mode*/ + duration = GST_CLOCK_TIME_NONE; + } else { + if (camerasrc->fps <= 0) { + /*if fps is zero, auto fps mode*/ + fps_nu = 0; + fps_de = 1; + } else { + fps_nu = 1; + fps_de = camerasrc->fps; + } + + if (fps_nu > 0 && fps_de > 0) { + GstClockTime latency; + + latency = gst_util_uint64_scale_int(GST_SECOND, fps_nu, fps_de); + duration = latency; + } + } + } else { + /* no clock, can't set timestamps */ + timestamp = GST_CLOCK_TIME_NONE; + } + + GST_BUFFER_TIMESTAMP(buffer) = timestamp; + GST_BUFFER_DURATION(buffer) = duration; +/* + GST_INFO_OBJECT(camerasrc, "[%"GST_TIME_FORMAT" dur %" GST_TIME_FORMAT "]", + GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)), + GST_TIME_ARGS(GST_BUFFER_DURATION(buffer))); +*/ + + return TRUE; +} + + +static int get_proper_index(GstCameraSrc *camerasrc, GstBuffer *pad_alloc_buffer) +{ + int temp_idx = 0; + + while (GST_BUFFER_DATA(GST_BUFFER(pad_alloc_buffer)) != g_present_buf.present_buffer[temp_idx++].start) { + if (temp_idx >= camerasrc->num_alloc_buf) { + return -1; + } + } + + return temp_idx - 1; /* -1 is caused that it already increased in ++ */ +} + + +/* Gstreamer general functions */ +static gboolean gst_camerasrc_src_start(GstBaseSrc *src) +{ + int ret = TRUE; + GstCameraSrc *camerasrc = GST_CAMERA_SRC (src); + + GST_DEBUG_OBJECT(camerasrc, "ENTERED"); + +#if _ENABLE_CAMERASRC_DEBUG + g_preview_frame_cnt = 0; +#endif + + camerasrc->firsttime = TRUE; + /* 'gst_camerasrc_set_caps' will call gst_camerasrc_start(). So skip to call it. */ + /*ret = gst_camerasrc_start(camerasrc);*/ + + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + + return ret; +} + + +static gboolean gst_camerasrc_src_stop(GstBaseSrc *src) +{ + int ret = 0; + GstCameraSrc *camerasrc = GST_CAMERA_SRC(src); + + GST_DEBUG_OBJECT (camerasrc, "ENTERED"); + + ret = gst_camerasrc_stop(camerasrc); + + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + + return TRUE; +} + + +static GstFlowReturn gst_camerasrc_src_create(GstPushSrc *src, GstBuffer **buffer) +{ + GstCameraSrc *camerasrc = GST_CAMERA_SRC (src); + GstFlowReturn ret; + + /*GST_DEBUG_OBJECT(camerasrc, "ENTERED");*/ + + if (camerasrc->req_negotiation) { + GST_INFO_OBJECT(camerasrc, "negotiation start"); + + GST_BASE_SRC_GET_CLASS(camerasrc)->negotiate(GST_BASE_SRC(camerasrc)); + camerasrc->req_negotiation = FALSE; + g_signal_emit(G_OBJECT(camerasrc), gst_camerasrc_signals[SIGNAL_NEGO_COMPLETE], (GQuark)NULL); + + GST_INFO_OBJECT (camerasrc, "negotiation stop"); + } + + __ta__(" gst_camerasrc_read", + ret = gst_camerasrc_read(camerasrc, buffer); + ) + /*GST_DEBUG_OBJECT (camerasrc, "LEAVED");*/ + + return ret; +} + + +static void gst_camerasrc_set_property(GObject *object, guint prop_id, + const GValue *value, GParamSpec *pspec) +{ + int tmp = 0; + GstCameraSrc *camerasrc = NULL; + + g_return_if_fail(GST_IS_CAMERA_SRC(object)); + camerasrc = GST_CAMERA_SRC(object); + + switch (prop_id) { + case ARG_REQ_NEGOTIATION: + camerasrc->req_negotiation = g_value_get_boolean(value); + break; + case ARG_CAMERA_HIGH_SPEED_FPS: + tmp = g_value_get_int(value); + camerasrc->high_speed_fps = tmp; + break; + case ARG_CAMERA_AUTO_FPS: + camerasrc->fps_auto = g_value_get_boolean(value); + GST_DEBUG_OBJECT(camerasrc, "Set AUTO_FPS:%d", camerasrc->fps_auto); + break; + case ARG_CAMERA_ID: + camerasrc->camera_id = g_value_get_int(value); + break; + case ARG_CAMERA_EXT_VIDEO_FD: + camerasrc->external_videofd = g_value_get_int(value); + break; + case ARG_CAMERA_CAPTURE_FOURCC: + camerasrc->cap_fourcc = g_value_get_uint(value); + break; + case ARG_CAMERA_CAPTURE_QUALITY: + camerasrc->cap_quality = g_value_get_enum(value); + break; + case ARG_CAMERA_CAPTURE_WIDTH: + camerasrc->cap_width = g_value_get_int(value); + break; + case ARG_CAMERA_CAPTURE_HEIGHT: + camerasrc->cap_height = g_value_get_int(value); + break; + case ARG_CAMERA_CAPTURE_INTERVAL: + camerasrc->cap_interval = g_value_get_int(value); + break; + case ARG_CAMERA_CAPTURE_COUNT: + tmp = g_value_get_int(value); + camerasrc->cap_count = tmp; + g_mutex_lock(camerasrc->jpg_mutex); + camerasrc->cap_count_reverse = tmp; + g_mutex_unlock(camerasrc->jpg_mutex); + GST_INFO("SET reverse capture count: %d", camerasrc->cap_count_reverse); + break; + case ARG_CAMERA_CAPTURE_JPG_QUALITY: + camerasrc->cap_jpg_quality = g_value_get_int(value); + GST_INFO("SET jpeg compress ratio : %d", camerasrc->cap_jpg_quality); + break; + case ARG_SIGNAL_STILLCAPTURE: + camerasrc->signal_still_capture = g_value_get_boolean(value); + break; + case ARG_USE_PAD_ALLOC: + camerasrc->use_pad_alloc = g_value_get_boolean(value); + break; + case ARG_NUM_ALLOC_BUF: + camerasrc->num_alloc_buf = g_value_get_int(value); + break; + case ARG_HOLD_AF_AFTER_CAPTURE: + camerasrc->hold_af_after_capturing = g_value_get_boolean(value); + camerasrc_set_af_hold_after_capture(camerasrc->v4l2_handle, (int)camerasrc->hold_af_after_capturing); + break; + case ARG_SENSOR_MODE: + camerasrc->sensor_mode = g_value_get_int(value); + break; + case ARG_VFLIP: + camerasrc->vflip = g_value_get_boolean(value); + break; + case ARG_HFLIP: + camerasrc->hflip = g_value_get_boolean(value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + + +static void gst_camerasrc_get_property(GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + GstCameraSrc *camerasrc; + + g_return_if_fail(GST_IS_CAMERA_SRC(object)); + camerasrc = GST_CAMERA_SRC(object); + + switch (prop_id) { + case ARG_REQ_NEGOTIATION: + g_value_set_boolean(value, camerasrc->req_negotiation); + break; + case ARG_CAMERA_HIGH_SPEED_FPS: + g_value_set_int(value, camerasrc->high_speed_fps); + break; + case ARG_CAMERA_AUTO_FPS: + g_value_set_boolean(value, camerasrc->fps_auto); + break; + case ARG_CAMERA_ID: + g_value_set_int(value, camerasrc->camera_id); + break; + case ARG_CAMERA_EXT_VIDEO_FD: + g_value_set_int(value, camerasrc->external_videofd); + break; + case ARG_CAMERA_CAPTURE_FOURCC: + g_value_set_uint(value, camerasrc->cap_fourcc); + break; + case ARG_CAMERA_CAPTURE_QUALITY: + g_value_set_enum(value, camerasrc->cap_quality); + break; + case ARG_CAMERA_CAPTURE_WIDTH: + g_value_set_int(value, camerasrc->cap_width); + break; + case ARG_CAMERA_CAPTURE_HEIGHT: + g_value_set_int(value, camerasrc->cap_height); + break; + case ARG_CAMERA_CAPTURE_INTERVAL: + g_value_set_int(value, camerasrc->cap_interval); + break; + case ARG_CAMERA_CAPTURE_COUNT: + g_value_set_int(value, camerasrc->cap_count); + break; + case ARG_CAMERA_CAPTURE_JPG_QUALITY: + g_value_set_int(value, camerasrc->cap_jpg_quality); + GST_INFO("GET jpeg compress ratio : %d", camerasrc->cap_jpg_quality); + break; + case ARG_CAMERA_CAPTURE_PROVIDE_EXIF: + camerasrc_support_embed_exif(camerasrc->v4l2_handle, &(camerasrc->cap_provide_exif)); + g_value_set_boolean(value, camerasrc->cap_provide_exif); + GST_INFO("Is Exif provided? : %d", camerasrc->cap_provide_exif); + break; + case ARG_SIGNAL_STILLCAPTURE: + g_value_set_boolean(value, camerasrc->signal_still_capture); + break; + case ARG_USE_PAD_ALLOC: + g_value_set_boolean(value, camerasrc->use_pad_alloc); + break; + case ARG_NUM_ALLOC_BUF: + g_value_set_int(value, camerasrc->num_alloc_buf); + break; + case ARG_OPERATION_STATUS: + g_value_set_int(value, 0); + gst_camerasrc_buffer_trace(camerasrc); + break; + case ARG_HOLD_AF_AFTER_CAPTURE: + g_value_set_boolean(value, camerasrc->hold_af_after_capturing); + break; + case ARG_SENSOR_MODE: + g_value_set_int(value, camerasrc->sensor_mode); + break; + case ARG_VFLIP: + g_value_set_boolean(value, camerasrc->vflip); + break; + case ARG_HFLIP: + g_value_set_boolean(value, camerasrc->hflip); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); + break; + } +} + + +static GstStateChangeReturn gst_camerasrc_change_state(GstElement *element, GstStateChange transition) +{ + GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS; + GstCameraSrc *camerasrc; + camerasrc = GST_CAMERA_SRC (element); + + switch (transition) { + case GST_STATE_CHANGE_NULL_TO_READY: + GST_INFO_OBJECT(camerasrc, "GST CAMERA SRC: NULL -> READY"); + __ta__(" gst_camerasrc_create", + if (!gst_camerasrc_create(camerasrc)){ + goto statechange_failed; + } + ); + break; + case GST_STATE_CHANGE_READY_TO_PAUSED: + GST_INFO_OBJECT(camerasrc, "GST CAMERA SRC: READY -> PAUSED"); + ret = GST_STATE_CHANGE_NO_PREROLL; + break; + case GST_STATE_CHANGE_PAUSED_TO_PLAYING: + GST_INFO_OBJECT(camerasrc, "GST CAMERA SRC: PAUSED -> PLAYING"); + break; + default: + break; + } + + ret = GST_ELEMENT_CLASS(parent_class)->change_state(element, transition); + if (ret == GST_STATE_CHANGE_FAILURE){ + return ret; + } + + switch (transition) { + case GST_STATE_CHANGE_PLAYING_TO_PAUSED: + GST_INFO_OBJECT(camerasrc, "GST CAMERA SRC: PLAYING -> PAUSED"); + ret = GST_STATE_CHANGE_NO_PREROLL; + break; + case GST_STATE_CHANGE_PAUSED_TO_READY: + GST_INFO_OBJECT(camerasrc, "GST CAMERA SRC: PAUSED -> READY"); + break; + case GST_STATE_CHANGE_READY_TO_NULL: + GST_INFO_OBJECT(camerasrc, "GST CAMERA SRC: READY -> NULL"); + __ta__(" gst_camerasrc_destroy", + if (!gst_camerasrc_destroy(camerasrc)){ + goto statechange_failed; + } + ); + break; + default: + break; + } + + return ret; + + statechange_failed: + /* subclass must post a meaningfull error message */ + GST_ERROR_OBJECT(camerasrc, "state change failed"); + + return GST_STATE_CHANGE_FAILURE; +} + + +static void gst_camerasrc_finalize(GObject *object) +{ + GstCameraSrc *camerasrc = GST_CAMERA_SRC(object); + +#if _ENABLE_CAMERASRC_DEBUG + GST_INFO_OBJECT(camerasrc, "total [%u] preview frame(s) outputed.",g_preview_frame_cnt); +#endif + + g_mutex_free(camerasrc->jpg_mutex); + g_mutex_free(camerasrc->mutex); + g_cond_free(camerasrc->cond); + g_mutex_free(camerasrc->buffer_lock); + g_cond_broadcast(camerasrc->buffer_cond); + g_cond_free(camerasrc->buffer_cond); + + if (camerasrc->command_list != NULL) { + g_queue_free(camerasrc->command_list); + camerasrc->command_list = NULL; + } + + G_OBJECT_CLASS(parent_class)->finalize(object); +} + + +void gst_camerasrc_set_capture_command(GstCameraSrc *camerasrc, GstCameraControlCaptureCommand cmd) +{ + gboolean is_zsl = FALSE; + + if (camerasrc == NULL) { + GST_ERROR_OBJECT(camerasrc, "camerasrc is NULL"); + return; + } + + GST_INFO_OBJECT(camerasrc, "ENTERED"); + + g_mutex_lock(camerasrc->mutex); + + /* Do not push command when ZSL mode */ + if (!is_zsl) { + g_queue_push_tail(camerasrc->command_list, (gpointer)cmd); + GST_INFO_OBJECT(camerasrc, "ACTION: Push capture command [%d] finished.", cmd); + } + + if (cmd == GST_CAMERA_CONTROL_CAPTURE_COMMAND_STOP) { + g_cond_signal(camerasrc->cond); + GST_INFO_OBJECT(camerasrc, "Send signal for CAPTURE STOP"); + } + + g_mutex_unlock(camerasrc->mutex); + + return; +} + + +static gboolean gst_camerasrc_negotiate (GstBaseSrc * basesrc) +{ + GstCaps *thiscaps; + GstCaps *caps = NULL; + GstCaps *peercaps = NULL; + gboolean result = FALSE; + GstStructure *s; + GstCameraSrc *camerasrc = GST_CAMERA_SRC(basesrc); + + GST_INFO_OBJECT(camerasrc, "ENTERED"); + /* first see what is possible on our source pad */ + thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc)); + GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps); + + /* nothing or anything is allowed, we're done */ + if (thiscaps == NULL || gst_caps_is_any (thiscaps)) + goto no_nego_needed; + + /* get the peer caps */ + peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc)); + GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps); + //LOG_CAPS (basesrc, peercaps); + if (peercaps && !gst_caps_is_any (peercaps)) { + GstCaps *icaps = NULL; + int i; + + /* Prefer the first caps we are compatible with that the peer proposed */ + for (i = 0; i < gst_caps_get_size (peercaps); i++) { + /* get intersection */ + GstCaps *ipcaps = gst_caps_copy_nth (peercaps, i); + + icaps = gst_caps_intersect (thiscaps, ipcaps); + gst_caps_unref (ipcaps); + + if (!gst_caps_is_empty (icaps)) + break; + + gst_caps_unref (icaps); + icaps = NULL; + } + + GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, icaps); + if (icaps) { + /* If there are multiple intersections pick the one with the smallest + * resolution strictly bigger than the first peer caps */ + if (gst_caps_get_size (icaps) > 1) { + s = gst_caps_get_structure (peercaps, 0); + int best = 0; + int twidth, theight; + int width = G_MAXINT, height = G_MAXINT; + + if (gst_structure_get_int (s, "width", &twidth) + && gst_structure_get_int (s, "height", &theight)) { + + /* Walk the structure backwards to get the first entry of the + * smallest resolution bigger (or equal to) the preferred resolution) + */ + for (i = gst_caps_get_size (icaps) - 1; i >= 0; i--) { + GstStructure *is = gst_caps_get_structure (icaps, i); + int w, h; + + if (gst_structure_get_int (is, "width", &w) + && gst_structure_get_int (is, "height", &h)) { + if (w >= twidth && w <= width && h >= theight && h <= height) { + width = w; + height = h; + best = i; + } + } + } + } + + caps = gst_caps_copy_nth (icaps, best); + gst_caps_unref (icaps); + } else { + caps = icaps; + } + } + gst_caps_unref (thiscaps); + gst_caps_unref (peercaps); + } else { + /* no peer or peer has ANY caps, work with our own caps then */ + caps = thiscaps; + } + if (caps) { + caps = gst_caps_make_writable (caps); + gst_caps_truncate (caps); + + /* now fixate */ + if (!gst_caps_is_empty (caps)) { + gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps); + GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps); + + if (gst_caps_is_any (caps)) { + /* hmm, still anything, so element can do anything and + * nego is not needed */ + result = TRUE; + } else if (gst_caps_is_fixed (caps)) { + /* yay, fixed caps, use those then */ + if (gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps)) + result = TRUE; + } + } + gst_caps_unref (caps); + } + return result; + +no_nego_needed: + { + GST_DEBUG_OBJECT (basesrc, "no negotiation needed"); + if (thiscaps) + gst_caps_unref (thiscaps); + return TRUE; + } +} + + +static GstCaps *gst_camerasrc_get_caps(GstBaseSrc *src) +{ + GstCameraSrc *camerasrc = GST_CAMERA_SRC(src); + GstCaps *ret = NULL; + + GST_DEBUG_OBJECT(camerasrc, "ENTERED"); + + if (camerasrc->mode == VIDEO_IN_MODE_UNKNOWN) { + GST_INFO_OBJECT(camerasrc, "Unknown mode. Just return template caps."); + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + + return gst_caps_copy(gst_pad_get_pad_template_caps(GST_BASE_SRC_PAD(camerasrc))); + } + + /*FIXME: Using "VIDIOC_ENUM_FMT".*/ + ret = gst_caps_copy(gst_pad_get_pad_template_caps(GST_BASE_SRC_PAD(camerasrc))); + + GST_INFO_OBJECT(camerasrc, "probed caps: %x", ret); + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + + return ret; +} + + +static gboolean _gst_camerasrc_get_raw_pixel_info(int fourcc, int *pix_format, int *colorspace) +{ + switch (fourcc) { + case GST_MAKE_FOURCC('I','4','2','0'): /* V4L2_PIX_FMT_YUV420 */ + case GST_MAKE_FOURCC('I','Y','U','V'): + case GST_MAKE_FOURCC('Y','U','1','2'): + case GST_MAKE_FOURCC('S','4','2','0'): + *pix_format = CAMERASRC_PIX_YUV420P; + *colorspace = CAMERASRC_COL_RAW; + break; + case GST_MAKE_FOURCC('Y','U','Y','V'): /* V4L2_PIX_FMT_YUYV */ + case GST_MAKE_FOURCC('Y','U','Y','2'): /* V4L2_PIX_FMT_YUYV */ + case GST_MAKE_FOURCC('S','U','Y','V'): + case GST_MAKE_FOURCC('S','U','Y','2'): + *pix_format = CAMERASRC_PIX_YUY2; + *colorspace = CAMERASRC_COL_RAW; + break; + case GST_MAKE_FOURCC('U','Y','V','Y'): /* V4L2_PIX_FMT_UYVY */ + case GST_MAKE_FOURCC('S','Y','V','Y'): /* V4L2_PIX_FMT_UYVY */ + *pix_format = CAMERASRC_PIX_UYVY; + *colorspace = CAMERASRC_COL_RAW; + break; + case GST_MAKE_FOURCC('4','2','2','P'): /* V4L2_PIX_FMT_YUV422P */ + case GST_MAKE_FOURCC('Y','4','2','B'): /* V4L2_PIX_FMT_YUV422P */ + *pix_format = CAMERASRC_PIX_YUV422P; + *colorspace = CAMERASRC_COL_RAW; + break; + case GST_MAKE_FOURCC('N','V','1','2'): /* V4L2_PIX_FMT_NV12 */ + *pix_format = CAMERASRC_PIX_NV12; + *colorspace = CAMERASRC_COL_RAW; + break; + case GST_MAKE_FOURCC('S','N','1','2'): /* V4L2_PIX_FMT_NV12 non-linear */ + *pix_format = CAMERASRC_PIX_SN12; + *colorspace = CAMERASRC_COL_RAW; + break; + case GST_MAKE_FOURCC('S','T','1','2'): /* V4L2_PIX_FMT_NV12 tiled non-linear */ + *pix_format = CAMERASRC_PIX_ST12; + *colorspace = CAMERASRC_COL_RAW; + break; + case GST_MAKE_FOURCC('J','P','E','G'): + case GST_MAKE_FOURCC('j','p','e','g'): + *pix_format = CAMERASRC_PIX_RGGB8; + *colorspace = CAMERASRC_COL_JPEG; + break; + case GST_MAKE_FOURCC('Y','4','1','P'): /* V4L2_PIX_FMT_Y41P */ + case GST_MAKE_FOURCC('N','V','2','1'): /* V4L2_PIX_FMT_NV21 */ + case GST_MAKE_FOURCC('Y','4','1','B'): /* V4L2_PIX_FMT_YUV411P */ + case GST_MAKE_FOURCC('Y','V','1','2'): /* V4L2_PIX_FMT_YVU420 */ + default: + /* ERROR */ + *pix_format = CAMERASRC_PIX_NONE; + *colorspace = CAMERASRC_COL_NONE; + break; + } + + return TRUE; +} + + +static gboolean _gst_camerasrc_get_frame_size(int fourcc, int width, int height, unsigned int *outsize) +{ + switch (fourcc) { + case GST_MAKE_FOURCC('I','4','2','0'): /* V4L2_PIX_FMT_YUV420 */ + case GST_MAKE_FOURCC('I','Y','U','V'): + case GST_MAKE_FOURCC('Y','U','1','2'): + case GST_MAKE_FOURCC('Y','V','1','2'): + case GST_MAKE_FOURCC('S','4','2','0'): /* V4L2_PIX_FMT_NV12 tiled non-linear */ + *outsize = GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height); + *outsize += 2 * ((GST_ROUND_UP_8 (width) / 2) * (GST_ROUND_UP_2 (height) / 2)); + break; + case GST_MAKE_FOURCC('Y','U','Y','V'): /* V4L2_PIX_FMT_YUYV */ + case GST_MAKE_FOURCC('Y','U','Y','2'): /* V4L2_PIX_FMT_YUYV */ + case GST_MAKE_FOURCC('S','U','Y','V'): + case GST_MAKE_FOURCC('S','U','Y','2'): + case GST_MAKE_FOURCC('U','Y','V','Y'): /* V4L2_PIX_FMT_UYVY */ + case GST_MAKE_FOURCC('S','Y','V','Y'): /* V4L2_PIX_FMT_UYVY */ + case GST_MAKE_FOURCC('4','2','2','P'): /* V4L2_PIX_FMT_YUV422P */ + case GST_MAKE_FOURCC('Y','4','2','B'): /* V4L2_PIX_FMT_YUV422P */ + case GST_MAKE_FOURCC('Y','4','1','P'): /* V4L2_PIX_FMT_Y41P */ + *outsize = (GST_ROUND_UP_2 (width) * 2) * height; + break; + case GST_MAKE_FOURCC('Y','4','1','B'): /* V4L2_PIX_FMT_YUV411P */ + *outsize = GST_ROUND_UP_4 (width) * height; + *outsize += 2 * ((GST_ROUND_UP_8 (width) / 4) * height); + break; + case GST_MAKE_FOURCC('N','V','1','2'): /* V4L2_PIX_FMT_NV12 */ + case GST_MAKE_FOURCC('N','V','2','1'): /* V4L2_PIX_FMT_NV21 */ + case GST_MAKE_FOURCC('S','N','1','2'): /* V4L2_PIX_FMT_NV12 non-linear */ + case GST_MAKE_FOURCC('S','T','1','2'): /* V4L2_PIX_FMT_NV12 tiled non-linear */ + *outsize = GST_ROUND_UP_4 (width) * GST_ROUND_UP_2 (height); + *outsize += (GST_ROUND_UP_4 (width) * height) / 2; + break; + case GST_MAKE_FOURCC('J','P','E','G'): + case GST_MAKE_FOURCC('j','p','e','g'): + /* jpeg size can't be calculated here. */ + *outsize = 0; + break; + default: + /* unkown format!! */ + *outsize = 0; + break; + } + + return TRUE; +} + +static gboolean _gst_camerasrc_get_normal_buffer(GstCameraSrc *camerasrc, int fourcc, + unsigned char *base_buffer, int width, int height, + unsigned char **new_buffer, unsigned int *new_buffer_length) +{ + unsigned char *copy_data = NULL; + guint length_Y = 0; + + if (camerasrc == NULL || base_buffer == NULL || + new_buffer == NULL || new_buffer_length == NULL) { + GST_WARNING_OBJECT(camerasrc, "something is NULL(%p,%p,%p,%p)", + camerasrc, base_buffer, new_buffer, new_buffer_length); + return FALSE; + } + + switch (fourcc) { + case GST_MAKE_FOURCC('I','4','2','0'): + case GST_MAKE_FOURCC('S','4','2','0'): + { + guint length_Cb = 0; + guint length_Cr = 0; + guint offset_Cb = 0; + guint offset_Cr = 0; + + length_Y = width * height; + length_Cb = length_Cr = (width * height) >> 2; + + copy_data = (unsigned char*)malloc(length_Y + length_Cb + length_Cr); + if (copy_data == NULL) { + GST_ERROR_OBJECT(camerasrc, "New buffer data ALLOC FAILED"); + gst_camerasrc_error_handler(camerasrc, CAMERASRC_ERR_ALLOCATION); + return FALSE; + } + + offset_Cb = CAMERASRC_ALIGN(length_Y, ALIGN_SIZE_I420); + offset_Cr = offset_Cb + CAMERASRC_ALIGN(length_Cb, ALIGN_SIZE_I420); + + memcpy(copy_data, base_buffer, length_Y); + memcpy(copy_data + length_Y, base_buffer + offset_Cb, length_Cb); + memcpy(copy_data + length_Y + length_Cb, base_buffer + offset_Cr, length_Cr); + + *new_buffer = copy_data; + *new_buffer_length = length_Y + length_Cb + length_Cr; + + GST_DEBUG_OBJECT(camerasrc, "Total(0x%x), I420(S420) length Y(0x%x),Cb(0x%x),Cr(0x%x), offset Cb(0x%x),Cr(0x%x)", + *new_buffer_length, length_Y, length_Cb, length_Cr, offset_Cb, offset_Cr); + break; + } + case GST_MAKE_FOURCC('N','V','1','2'): + case GST_MAKE_FOURCC('S','N','1','2'): + { + guint length_CbCr = 0; + guint offset_CbCr = 0; + + length_Y = width * height; + length_CbCr = (width * height) >> 1; + + copy_data = (unsigned char*)malloc(length_Y + length_CbCr); + if (copy_data == NULL) { + GST_ERROR_OBJECT(camerasrc, "New buffer data ALLOC FAILED"); + gst_camerasrc_error_handler(camerasrc, CAMERASRC_ERR_ALLOCATION); + return FALSE; + } + + offset_CbCr = CAMERASRC_ALIGN(length_Y, ALIGN_SIZE_NV12); + + memcpy(copy_data, base_buffer, length_Y); + memcpy(copy_data + length_Y, base_buffer + offset_CbCr, length_CbCr); + + *new_buffer = copy_data; + *new_buffer_length = length_Y + length_CbCr; + + GST_DEBUG_OBJECT(camerasrc, "Total(0x%x), NV12(SN12) length Y(0x%x),CbCr(0x%x), offset CbCr(0x%x)", + *new_buffer_length, length_Y, length_CbCr, offset_CbCr); + break; + } + default: + { + char *pfourcc = (char*)&fourcc; + GST_WARNING_OBJECT(camerasrc, "Unknown fourcc(%c%c%c%c)", + pfourcc[0], pfourcc[1], pfourcc[2], pfourcc[3]); + *new_buffer = NULL; + *new_buffer_length = 0; + return FALSE; + } + } + + GST_DEBUG_OBJECT(camerasrc, "Done."); + + return TRUE; +} + + +static gboolean gst_camerasrc_get_caps_info(GstCameraSrc *camerasrc, GstCaps *caps, guint *size) +{ + gint fps_n = 0; + gint fps_d = 0; + gint w = 0; + gint h = 0; + gint rot = 0; + gchar *caps_string = NULL; + const gchar *mimetype; + const GValue *framerate; + GstStructure *structure = NULL; + + GST_INFO_OBJECT(camerasrc, "ENTERED Collect data for given caps.(caps:%x)", caps); + + structure = gst_caps_get_structure(caps, 0); + + if (!gst_structure_get_int(structure, "width", &w)) { + goto _caps_info_failed; + } + + if (!gst_structure_get_int(structure, "height", &h)) { + goto _caps_info_failed; + } + + if (!gst_structure_get_int(structure, "rotate", &rot)) { + GST_WARNING_OBJECT(camerasrc, "Failed to get rotate info in caps. set default 0."); + camerasrc->use_rotate_caps = FALSE; + } else { + GST_INFO_OBJECT(camerasrc, "Succeed to get rotate[%d] info in caps", rot); + camerasrc->use_rotate_caps = TRUE; + } + + /* set default size if there is no capsfilter */ + if (w == 1) { + w = _DEFAULT_WIDTH * 2; + } + + if (h == 1) { + h = _DEFAULT_HEIGHT * 2; + } + + camerasrc->width = w; + camerasrc->height = h; + camerasrc->rotate = rot; + + framerate = gst_structure_get_value(structure, "framerate"); + if (!framerate) { + GST_INFO("Set FPS as default"); + + /* set default fps if framerate is not existed in caps */ + fps_n = _DEFAULT_FPS; + fps_d = 1; + } else { + fps_n = gst_value_get_fraction_numerator(framerate); + fps_d = gst_value_get_fraction_denominator(framerate); + + /* numerator and denominator should be bigger than zero */ + if (fps_n <= 0) { + GST_WARNING("numerator of FPS is %d. make it default(15).", fps_n); + fps_n = _DEFAULT_FPS; + } + + if (fps_d <= 0) { + GST_WARNING("denominator of FPS is %d. make it 1.", fps_d); + fps_d = 1; + } + } + + camerasrc->fps = (int)((float)fps_n / (float)fps_d); + + mimetype = gst_structure_get_name (structure); + + *size = 0; + + if (!strcmp(mimetype, "video/x-raw-yuv")) { + gst_structure_get_fourcc(structure, "format", &camerasrc->fourcc); + if (camerasrc->fourcc == 0) { + GST_INFO_OBJECT(camerasrc, "Getting fourcc is zero."); + goto _caps_info_failed; + } + + _gst_camerasrc_get_frame_size(camerasrc->fourcc, w, h, size); + _gst_camerasrc_get_raw_pixel_info(camerasrc->fourcc, &(camerasrc->pix_format), &(camerasrc->colorspace)); + } else if (!strcmp (mimetype, "video/x-raw-rgb")) { + gint depth = 0; + gint endianness = 0; + gint r_mask = 0; + + gst_structure_get_int(structure, "depth", &depth); + gst_structure_get_int(structure, "endianness", &endianness); + gst_structure_get_int(structure, "red_mask", &r_mask); + + switch (depth) { + case 8: /* V4L2_PIX_FMT_RGB332 */ + camerasrc->pix_format = CAMERASRC_PIX_RGGB8; + camerasrc->colorspace = CAMERASRC_COL_RAW; + break; + case 15: /* V4L2_PIX_FMT_RGB555 : V4L2_PIX_FMT_RGB555X */ + camerasrc->pix_format = CAMERASRC_PIX_NONE; + camerasrc->colorspace = CAMERASRC_COL_NONE; + break; + case 16: /* V4L2_PIX_FMT_RGB565 : V4L2_PIX_FMT_RGB565X */ + camerasrc->pix_format = CAMERASRC_PIX_NONE; + camerasrc->colorspace = CAMERASRC_COL_NONE; + break; + case 24: /* V4L2_PIX_FMT_BGR24 : V4L2_PIX_FMT_RGB24 */ + camerasrc->pix_format = CAMERASRC_PIX_NONE; + camerasrc->colorspace = CAMERASRC_COL_NONE; + break; + case 32: /* V4L2_PIX_FMT_BGR32 : V4L2_PIX_FMT_RGB32 */ + camerasrc->pix_format = CAMERASRC_PIX_NONE; + camerasrc->colorspace = CAMERASRC_COL_NONE; + break; + } + } else if (strcmp(mimetype, "video/x-dv") == 0) { /* V4L2_PIX_FMT_DV */ + camerasrc->pix_format = CAMERASRC_PIX_NONE; + camerasrc->colorspace = CAMERASRC_COL_NONE; + } else if (strcmp(mimetype, "image/jpeg") == 0) { /* V4L2_PIX_FMT_JPEG */ + camerasrc->pix_format = CAMERASRC_PIX_RGGB8; /* default */ + camerasrc->colorspace = CAMERASRC_COL_JPEG; + } + + caps_string = gst_caps_to_string(caps); + GST_INFO_OBJECT(camerasrc, "pixformat %d, colorspace %d, size %d, caps : [%s]", + camerasrc->pix_format, camerasrc->colorspace, *size, caps_string); + if (caps_string) { + g_free(caps_string); + caps_string = NULL; + } + + return TRUE; + +_caps_info_failed: + GST_INFO_OBJECT(camerasrc, "Failed to get caps info."); + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + return FALSE; +} + + +static gboolean gst_camerasrc_set_caps(GstBaseSrc *src, GstCaps *caps) +{ + guint size; + GstCameraSrc *camerasrc = NULL; + + camerasrc = GST_CAMERA_SRC(src); + + GST_INFO_OBJECT(camerasrc, "ENTERED"); + + if (camerasrc->mode == VIDEO_IN_MODE_PREVIEW || + camerasrc->mode == VIDEO_IN_MODE_VIDEO) { + GST_INFO_OBJECT(camerasrc, "Proceed set_caps"); + __ta__(" gst_camerasrc_stop", + if (!gst_camerasrc_stop(camerasrc)) { + GST_INFO_OBJECT(camerasrc, "Cam sensor stop failed."); + } + ); + } else if (camerasrc->mode == VIDEO_IN_MODE_CAPTURE) { + GST_ERROR_OBJECT(camerasrc, "A mode of avsystem camera is capture. Not to proceed set_caps."); + GST_DEBUG_OBJECT(camerasrc, "LEAVED"); + return FALSE; + } else { + GST_INFO_OBJECT(camerasrc, "A mode of avsystem camera is unknown[%d]. Proceed set_caps.", camerasrc->mode); + } + + /* we want our own v4l2 type of fourcc codes */ + if (!gst_camerasrc_get_caps_info(camerasrc, caps, &size)) { + GST_INFO_OBJECT(camerasrc, "can't get capture information from caps %x", caps); + return FALSE; + } + + __ta__(" gst_camerasrc_start", + if (!gst_camerasrc_start(camerasrc)) { + GST_INFO_OBJECT (camerasrc, "Cam sensor start failed."); + } + ); + + GST_INFO_OBJECT (camerasrc, "LEAVED"); + + return TRUE; +} + + +static void gst_camerasrc_base_init(gpointer klass) +{ + GstElementClass *element_class = GST_ELEMENT_CLASS(klass); + + GST_DEBUG_CATEGORY_INIT(camerasrc_debug, "camerasrc", 0, "camerasrc element"); + + GST_INFO("ENTERED"); + + gst_element_class_add_pad_template(element_class, gst_static_pad_template_get (&src_factory)); + gst_element_class_set_details(element_class, &camerasrc_details); + + GST_INFO("LEAVED"); +} + + +static void gst_camerasrc_class_init(GstCameraSrcClass *klass) +{ + GObjectClass *gobject_class; + GstElementClass *element_class; + GstBaseSrcClass *basesrc_class; + GstPushSrcClass *pushsrc_class; + + GST_DEBUG("ENTERED"); + + gobject_class = G_OBJECT_CLASS(klass); + element_class = GST_ELEMENT_CLASS(klass); + basesrc_class = GST_BASE_SRC_CLASS(klass); + pushsrc_class = GST_PUSH_SRC_CLASS(klass); + + gobject_class->set_property = gst_camerasrc_set_property; + gobject_class->get_property = gst_camerasrc_get_property; + gobject_class->finalize = gst_camerasrc_finalize; + element_class->change_state = gst_camerasrc_change_state; + basesrc_class->start = gst_camerasrc_src_start; + basesrc_class->stop = gst_camerasrc_src_stop; + basesrc_class->get_caps = gst_camerasrc_get_caps; + basesrc_class->set_caps = gst_camerasrc_set_caps; + basesrc_class->negotiate = gst_camerasrc_negotiate; + pushsrc_class->create = gst_camerasrc_src_create; + + g_object_class_install_property(gobject_class, ARG_REQ_NEGOTIATION, + g_param_spec_boolean("req-negotiation", "Request re-negotiation", + "Request to negotiate while on playing", + FALSE, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, ARG_CAMERA_HIGH_SPEED_FPS, + g_param_spec_int("high-speed-fps", "Fps for high speed recording", + "If this value is 0, the element doesn't activate high speed recording.", + 0, G_MAXINT, _DEFAULT_HIGH_SPEED_FPS, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_CAMERA_AUTO_FPS, + g_param_spec_boolean("fps-auto", "FPS Auto", + "Field for auto fps setting", + _DEFAULT_FPS_AUTO, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_CAMERA_EXT_VIDEO_FD, + g_param_spec_int("extern_videofd", "External video file descriptor", + "External video file descriptor", + _FD_MIN, _FD_MAX, _FD_MIN, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, ARG_CAMERA_ID, + g_param_spec_int("camera-id", "index number of camera to activate", + "index number of camera to activate", + _FD_MIN, _FD_MAX, 0, + G_PARAM_READWRITE)); + + /*Capture*/ + g_object_class_install_property(gobject_class, ARG_CAMERA_CAPTURE_FOURCC, + g_param_spec_uint("capture-fourcc", "Capture format", + "Fourcc value for capture format", + 0, G_MAXUINT, 0, + G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, ARG_CAMERA_CAPTURE_QUALITY, + g_param_spec_enum("capture-quality", "Capture quality", + "Quality of capture image (JPEG: 'high', RAW: 'high' or 'low')", + GST_TYPE_CAMERASRC_QUALITY, _DEFAULT_CAP_QUALITY, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_CAMERA_CAPTURE_WIDTH, + g_param_spec_int("capture-width", "Capture width", + "Width for camera size to capture", + 0, G_MAXINT, _DEFAULT_CAP_WIDTH, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_CAMERA_CAPTURE_HEIGHT, + g_param_spec_int("capture-height", "Capture height", + "Height for camera size to capture", + 0, G_MAXINT, _DEFAULT_CAP_HEIGHT, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_CAMERA_CAPTURE_INTERVAL, + g_param_spec_int("capture-interval", "Capture interval", + "Interval time to capture (millisecond)", + 0, G_MAXINT, _DEFAULT_CAP_INTERVAL, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_CAMERA_CAPTURE_COUNT, + g_param_spec_int("capture-count", "Capture count", + "Capture conut for multishot", + 1, G_MAXINT, _DEFAULT_CAP_COUNT, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_CAMERA_CAPTURE_JPG_QUALITY, + g_param_spec_int("capture-jpg-quality", "JPEG Capture compress ratio", + "Quality of capture image compress ratio", + 1, 100, _DEFAULT_CAP_JPG_QUALITY, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_CAMERA_CAPTURE_PROVIDE_EXIF, + g_param_spec_boolean("provide-exif", "Whether EXIF is provided", + "Does capture provide EXIF?", + _DEFAULT_CAP_PROVIDE_EXIF, + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_SIGNAL_STILLCAPTURE, + g_param_spec_boolean("signal-still-capture", "Signal still capture", + "Send a signal before pushing the buffer", + _DEFAULT_SIGNAL_STILL_CAPTURE, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_USE_PAD_ALLOC, + g_param_spec_boolean("use-pad-alloc", "Use pad alloc", + "Use gst_pad_alloc_buffer() for image frame buffer", + _DEFAULT_USE_PAD_ALLOC, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_NUM_ALLOC_BUF, + g_param_spec_int("num-alloc-buf", "Number alloced buffer", + "Number of buffer to alloc using gst_pad_alloc_buffer()", + 1, _MAX_NUM_ALLOC_BUF, _DEFAULT_NUM_ALLOC_BUF, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_OPERATION_STATUS, + g_param_spec_int("operation-status", "Check operation status of camerasrc", + "This value has bit flags that describe current operation status.", + G_MININT, G_MAXINT, 0, + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_HOLD_AF_AFTER_CAPTURE, + g_param_spec_boolean("hold-af-after-capturing", "Hold af after capturing", + "Hold af position after capturing", + FALSE, G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, ARG_FAKE_ESD, + g_param_spec_boolean("fake-esd", "Fake ESD generate", + "Generate fake esd situation", + FALSE, G_PARAM_READWRITE)); + + g_object_class_install_property(gobject_class, ARG_SENSOR_MODE, + g_param_spec_int("sensor-mode", "Sensor mode", + "Set sensor mode as CAMERA or MOVIE(camcorder)", + CAMERASRC_SENSOR_MODE_CAMERA, CAMERASRC_SENSOR_MODE_MOVIE, CAMERASRC_SENSOR_MODE_CAMERA, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_VFLIP, + g_param_spec_boolean("vflip", "Flip vertically", + "Flip camera input vertically", + 0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + g_object_class_install_property(gobject_class, ARG_HFLIP, + g_param_spec_boolean("hflip", "Flip horizontally", + "Flip camera input horizontally", + 0, + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + + /** + * GstCameraSrc::still-capture: + * @camerasrc: the camerasrc instance + * @buffer: the buffer that will be pushed - Main + * @buffer: the buffer that will be pushed - Thumbnail + * @buffer: the buffer that will be pushed - Screennail + * + * This signal gets emitted before sending the buffer. + */ + gst_camerasrc_signals[SIGNAL_STILL_CAPTURE] = + g_signal_new("still-capture", + G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET(GstCameraSrcClass, still_capture), + NULL, + NULL, + gst_camerasrc_VOID__OBJECT_OBJECT, + G_TYPE_NONE, + 3, /* Number of parameter */ + GST_TYPE_BUFFER, /* Main image buffer */ + GST_TYPE_BUFFER, /* Thumbnail image buffer */ + GST_TYPE_BUFFER); /* Screennail image buffer */ + + + /* wh01.cho:req-negotiation:+:To notify user of camerasrc, after changing resolution. */ + /** + * GstCameraSrc::nego-complete: + * @camerasrc: the camerasrc instance + * @start: when re-negotiation is finished. + * + */ + gst_camerasrc_signals[SIGNAL_NEGO_COMPLETE] = + g_signal_new("nego-complete", + G_TYPE_FROM_CLASS (klass), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET(GstCameraSrcClass, nego_complete), + NULL, + NULL, + gst_marshal_VOID__VOID, + G_TYPE_NONE, 0); + + GST_DEBUG("LEAVED"); +} + + +static void gst_camerasrc_init(GstCameraSrc *camerasrc, GstCameraSrcClass *klass) +{ + GST_DEBUG_OBJECT (camerasrc, "ENTERED"); + + camerasrc->v4l2_handle = NULL; + camerasrc->mode = VIDEO_IN_MODE_UNKNOWN; + camerasrc->firsttime = TRUE; + camerasrc->main_buf_sz = 0; + camerasrc->cap_count_current = -1; + camerasrc->cap_count_reverse = _DEFAULT_CAP_COUNT; + camerasrc->cap_next_time = 0UL; + camerasrc->command_list = g_queue_new (); + camerasrc->cond = g_cond_new (); + camerasrc->mutex = g_mutex_new (); + + /*camera*/ + camerasrc->width = _DEFAULT_WIDTH; + camerasrc->height = _DEFAULT_HEIGHT; + camerasrc->fps = _DEFAULT_FPS; + camerasrc->rotate = 0; + camerasrc->use_rotate_caps = FALSE; + camerasrc->high_speed_fps = _DEFAULT_HIGH_SPEED_FPS; + camerasrc->fps_auto = _DEFAULT_FPS_AUTO; + camerasrc->pix_format = _DEFAULT_PIX_FORMAT; + camerasrc->colorspace = _DEFAULT_COLORSPACE; + camerasrc->fourcc = _DEFAULT_FOURCC; + camerasrc->req_negotiation = FALSE; + camerasrc->num_live_buffers = _DEFAULT_NUM_LIVE_BUFFER; + camerasrc->buffer_count = _DEFAULT_BUFFER_COUNT; + camerasrc->buffer_running = _DEFAULT_BUFFER_RUNNING; + camerasrc->buffer_lock = g_mutex_new (); + camerasrc->buffer_cond = g_cond_new (); + camerasrc->bfirst = TRUE; + camerasrc->pad_alloc_list = g_queue_new (); + camerasrc->pad_alloc_mutex = g_mutex_new (); + camerasrc->current_buffer_data_index = 0; + camerasrc->sensor_mode = CAMERASRC_SENSOR_MODE_CAMERA; + camerasrc->vflip = 0; + camerasrc->hflip = 0; + + /* Initialize usr buffer to be alloced by sink to NULL */ + { + int i = 0; + for(i = 0; i < MAX_USR_BUFFER_NUM; i++) { + camerasrc->usr_buffer[i] = NULL; + } + } + camerasrc->use_pad_alloc = FALSE; + camerasrc->num_alloc_buf = 0; + camerasrc->camera_id = _DEFAULT_CAMERA_ID; + + /*capture*/ + camerasrc->cap_fourcc = _DEFAULT_FOURCC; + camerasrc->cap_quality = _DEFAULT_CAP_QUALITY; + camerasrc->cap_width = _DEFAULT_CAP_WIDTH; + camerasrc->cap_height = _DEFAULT_CAP_HEIGHT; + camerasrc->cap_interval = _DEFAULT_CAP_INTERVAL; + camerasrc->cap_count = _DEFAULT_CAP_COUNT; + camerasrc->cap_jpg_quality = _DEFAULT_CAP_JPG_QUALITY; + camerasrc->cap_provide_exif = _DEFAULT_CAP_PROVIDE_EXIF; + camerasrc->signal_still_capture = _DEFAULT_SIGNAL_STILL_CAPTURE; + camerasrc->hold_af_after_capturing = FALSE; + camerasrc->create_jpeg = FALSE; + camerasrc->jpg_mutex = g_mutex_new(); + camerasrc->first_invokation = TRUE; + camerasrc->external_videofd = _FD_DEFAULT; + +#ifdef _SPEED_UP_RAW_CAPTURE + camerasrc->cap_stream_diff = FALSE; +#endif /* _SPEED_UP_RAW_CAPTURE */ + + /* we operate in time */ + gst_base_src_set_format(GST_BASE_SRC(camerasrc), GST_FORMAT_TIME); + gst_base_src_set_live(GST_BASE_SRC(camerasrc), TRUE); + gst_base_src_set_do_timestamp(GST_BASE_SRC(camerasrc), TRUE); + + GST_DEBUG("LEAVED"); +} + + +static void GenerateYUV420BlackFrame(unsigned char *buf, int buf_size, int width, int height) +{ + int i; + int y_len = 0; + int yuv_len = 0; + + y_len = width * height; + yuv_len = (width * height * 3) >> 1; + + if (buf_size < yuv_len) { + return; + } + + if (width % 4) { + for (i = 0 ; i < y_len ; i++) { + buf[i] = 0x10; + } + + for ( ; i < yuv_len ; i++) { + buf[i] = 0x80; + } + } else { + /* faster way */ + int *ibuf = NULL; + short *sbuf = NULL; + ibuf = (int *)buf; + + for (i = 0 ; i < y_len / 4 ; i++) { + ibuf[i] = 0x10101010; /* set YYYY */ + } + + sbuf = (short*)(&buf[y_len]); + + for (i = 0 ; i < (yuv_len - y_len) / 2 ; i++) { + sbuf[i] = 0x8080; /* set UV */ + } + } + + return; +} + + +static void GenerateYUV422BlackFrame(unsigned char *buf, int buf_size, int width, int height) +{ + /* YUYV */ + int i; + int yuv_len = 0; + int *ibuf = NULL; + + ibuf = (int *)buf; + + yuv_len = (width * height * 2); + + if (buf_size < yuv_len) { + return; + } + + for (i = 0 ; i < yuv_len / 4 ; i++) { + ibuf[i] = 0x80108010; /* YUYV -> 0xVYUY */ + } + + return; +} + + +static gboolean GenerateST12BlackFrame(GstCameraSrc *camerasrc, unsigned char *buf, int buf_size, int width, int height) +{ + int i; + int y_len = 0; + int yuv_len = 0; + int ystride = 0; + int uvstride = 0; + int yelevation = 0; + int uvelevation = 0; + gboolean boolret = TRUE; + unsigned char *blkframe = NULL; + + /* Calculation of stride. need to be confirmation from driver team */ + if ((width % 128) != 0 ) { + ystride = width + (128 - (width % 128)); + uvstride = ystride; + } else { + ystride = width; + uvstride = width; + } + + /* Calculation of elevation. need to be confirmation from driver team */ + /* Y elevation */ + if ((height % 32) != 0) { + yelevation= height + (32 - (height % 32)); + } else { + yelevation = height; + } + /* UV elevation */ + if (((height >>1) % 32) != 0) { + uvelevation = (height >> 1) + (32 - ((height >> 1) % 32)); + } else { + uvelevation = (height >> 1); + } + + GST_DEBUG("ystride = %d and uvstride = %d", ystride, uvstride); + GST_DEBUG("yelevation = %d and uvelevation = %d", yelevation, uvelevation); + + y_len = ystride * yelevation; + yuv_len = y_len + uvstride * uvelevation; + + /* Generate black frame */ + blkframe = (unsigned char*)g_malloc(yuv_len); + + if (blkframe == NULL) { + GST_ERROR ("ST12Blk: Failed to allocate memory..."); + boolret = FALSE; + goto FINISH; + } + + for (i = 0 ; i < y_len ; i++) { + blkframe [i] = 0x10; + } + + for (; i < yuv_len ; i++) { + blkframe [i] = 0x80; + } + + +FINISH: + if (blkframe != NULL) { + g_free (blkframe); + } + + return boolret; + +} + + +static unsigned long gst_get_current_time(void) +{ + struct timeval lc_time; + + gettimeofday(&lc_time, NULL); + + return ((unsigned long)(lc_time.tv_sec * 1000L) + (unsigned long)(lc_time.tv_usec / 1000L)); +} + + +#if _ENABLE_CAMERASRC_DEBUG +#include <stdio.h> +static int __util_write_file(char *filename, void *data, int size) +{ + FILE *fp = NULL; + + fp = fopen(filename, "wb"); + if (!fp) { + return FALSE; + } + + fwrite(data, 1, size, fp); + fclose(fp); + + return TRUE; +} +#endif + + +static gboolean plugin_init(GstPlugin *plugin) +{ + gboolean error; + + error = gst_element_register(plugin, "camerasrc", GST_RANK_PRIMARY + 100, GST_TYPE_CAMERA_SRC); + + return error; +} + + +GST_PLUGIN_DEFINE(GST_VERSION_MAJOR, + GST_VERSION_MINOR, + "camerasrc", + "Camera source plug-in", + plugin_init, + PACKAGE_VERSION, + "LGPL", + "Samsung Electronics Co", + "http://www.samsung.com") + +/* GstURIHandler interface */ +static GstURIType gst_camerasrc_uri_get_type (void) +{ + return GST_URI_SRC; +} + +static gchar ** gst_camerasrc_uri_get_protocols (void) +{ + static gchar *protocols[] = { (char *) "camera", NULL }; + return protocols; +} + +static const gchar * gst_camerasrc_uri_get_uri (GstURIHandler * handler) +{ + GstCameraSrc *camerasrc = GST_CAMERA_SRC (handler); + + gchar uri[256]; + g_snprintf (uri, sizeof (uri), "camera://0"); + return g_intern_string (uri); +} + +static gboolean gst_camerasrc_uri_set_uri (GstURIHandler * handler, const gchar * uri) +{ + GstCameraSrc *camerasrc = GST_CAMERA_SRC (handler); + const gchar *device = "0"; + if (strcmp (uri, "camera://") != 0) { + device = uri + 9; + } + g_object_set (camerasrc, "camera-id", atoi(device), NULL); + + return TRUE; +} + + +static void gst_camerasrc_uri_handler_init (gpointer g_iface, gpointer iface_data) +{ + GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface; + + iface->get_type = gst_camerasrc_uri_get_type; + iface->get_protocols = gst_camerasrc_uri_get_protocols; + iface->get_uri = gst_camerasrc_uri_get_uri; + iface->set_uri = gst_camerasrc_uri_set_uri; +} +/* EOF */ diff --git a/camerasrc/src/gstcamerasrccolorbalance.c b/camerasrc/src/gstcamerasrccolorbalance.c new file mode 100644 index 0000000..242f9db --- /dev/null +++ b/camerasrc/src/gstcamerasrccolorbalance.c @@ -0,0 +1,117 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gst/gst.h> +#include "gstcamerasrccolorbalance.h" + +GST_BOILERPLATE( GstCameraSrcColorBalanceChannel, + gst_camerasrc_color_balance_channel, + GstColorBalanceChannel, + GST_TYPE_COLOR_BALANCE_CHANNEL ); + +#ifndef GST_CAT_DEFAULT +GST_DEBUG_CATEGORY_EXTERN(camerasrc_debug); +#define GST_CAT_DEFAULT camerasrc_debug +#endif /* GST_CAT_DEFAULT */ + +static void +gst_camerasrc_color_balance_channel_base_init( gpointer g_class ) +{ +} + +static void +gst_camerasrc_color_balance_channel_class_init( GstCameraSrcColorBalanceChannelClass* klass ) +{ +} + +static void +gst_camerasrc_color_balance_channel_init( GstCameraSrcColorBalanceChannel* camerasrc_color_channel, GstCameraSrcColorBalanceChannelClass* klass ) +{ + camerasrc_color_channel->id = (guint32) - 1; +} + +static G_GNUC_UNUSED gboolean +gst_camerasrc_color_balance_contains_channel( GstCameraSrc* camerasrc, GstCameraSrcColorBalanceChannel* camerasrc_color_channel ) +{ + const GList *item; + + for( item = camerasrc->colors ; item != NULL ; item = item->next ) + { + if (item->data == camerasrc_color_channel) + return TRUE; + } + + return FALSE; +} + +const GList * +gst_camerasrc_color_balance_list_channels( GstCameraSrc* camerasrc ) +{ + return camerasrc->colors; +} + +void +gst_camerasrc_color_balance_set_value( GstCameraSrc* camerasrc, GstColorBalanceChannel* color_channel, gint value ) +{ + int error = CAMERASRC_ERR_UNKNOWN; + + GstCameraSrcColorBalanceChannel *camerasrc_color_channel = GST_CAMERASRC_COLOR_BALANCE_CHANNEL( color_channel ); + + /* assert that we're opened and that we're using a known item */ + g_return_if_fail( camerasrc ); + g_return_if_fail( gst_camerasrc_color_balance_contains_channel( camerasrc, camerasrc_color_channel ) ); + + error = camerasrc_set_control( camerasrc->v4l2_handle, camerasrc_color_channel->id, value ); + + if( error != CAMERASRC_SUCCESS ) + { + GST_WARNING("Failed to Set ColorBalance[%s],value[%d]", camerasrc_color_channel->parent.label, value); + } +} + +gint +gst_camerasrc_color_balance_get_value( GstCameraSrc* camerasrc, GstColorBalanceChannel* color_channel ) +{ + int error, value; + GstCameraSrcColorBalanceChannel *camerasrc_color_channel = GST_CAMERASRC_COLOR_BALANCE_CHANNEL( color_channel ); + + /* assert that we're opened and that we're using a known item */ + g_return_val_if_fail( camerasrc, FALSE ); + g_return_val_if_fail( gst_camerasrc_color_balance_contains_channel( camerasrc, camerasrc_color_channel ), FALSE ); + + error = camerasrc_get_control( camerasrc->v4l2_handle, camerasrc_color_channel->id, &value ); + + if( error != CAMERASRC_SUCCESS ) + { + GST_WARNING("Failed to Get ColorBalance[%s].", camerasrc_color_channel->parent.label); + return FALSE; + } + + return value; +} + + diff --git a/camerasrc/src/gstcamerasrccontrol.c b/camerasrc/src/gstcamerasrccontrol.c new file mode 100644 index 0000000..2cb910f --- /dev/null +++ b/camerasrc/src/gstcamerasrccontrol.c @@ -0,0 +1,1098 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <gst/gst.h> +#include "gstcamerasrccontrol.h" +#include "camerasrc-common.h" + +#define gst_camerasrc_debug(fmt, args...) GST_INFO(fmt, ##args) + +#define CAMERA_CONTROL_AF_STOP_TOTALTIME 2000000 +#define CAMERA_CONTROL_AF_STOP_INTERVAL 20000 + +GST_BOILERPLATE (GstCamerasrcControlChannel, + gst_camerasrc_control_channel, + GstCameraControlChannel, GST_TYPE_CAMERA_CONTROL_CHANNEL); + + +static void +gst_camerasrc_control_channel_base_init( gpointer g_class ) +{ + gst_camerasrc_debug( "" ); +} + +static void +gst_camerasrc_control_channel_class_init( GstCamerasrcControlChannelClass* klass ) +{ + gst_camerasrc_debug( "" ); +} + +static void +gst_camerasrc_control_channel_init( GstCamerasrcControlChannel* control_channel, GstCamerasrcControlChannelClass* klass ) +{ + gst_camerasrc_debug( "" ); + + control_channel->id = (guint32) - 1; +} + +static G_GNUC_UNUSED gboolean +gst_camerasrc_control_contains_channel( GstCameraSrc* camerasrc, GstCamerasrcControlChannel* camerasrc_control_channel ) +{ + gst_camerasrc_debug( "" ); + + const GList* item; + + for( item = camerasrc->camera_controls ; item != NULL ; item = item->next ) + { + if( item->data == camerasrc_control_channel ) + { + return TRUE; + } + } + + return FALSE; +} + +const GList* +gst_camerasrc_control_list_channels( GstCameraSrc* camerasrc ) +{ + gst_camerasrc_debug( "" ); + + return camerasrc->camera_controls; +} + +gboolean +gst_camerasrc_control_set_value( GstCameraSrc* camerasrc, GstCameraControlChannel *control_channel, gint value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + GstCamerasrcControlChannel *camerasrc_control_channel = GST_CAMERASRC_CONTROL_CHANNEL( control_channel ); + + g_return_val_if_fail( camerasrc, FALSE ); + g_return_val_if_fail( gst_camerasrc_control_contains_channel( camerasrc, camerasrc_control_channel ), FALSE ); + + error = camerasrc_set_control( camerasrc->v4l2_handle, camerasrc_control_channel->id, value ); + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to set value. Ctrl-id [%d], value [%d], err code [%d]", camerasrc_control_channel->id, value, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_value( GstCameraSrc* camerasrc, GstCameraControlChannel *control_channel, gint *value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + GstCamerasrcControlChannel *camerasrc_control_channel = GST_CAMERASRC_CONTROL_CHANNEL( control_channel ); + + g_return_val_if_fail( camerasrc, FALSE ); + g_return_val_if_fail( gst_camerasrc_control_contains_channel( camerasrc, camerasrc_control_channel ), FALSE ); + + error = camerasrc_get_control( camerasrc->v4l2_handle, camerasrc_control_channel->id, value ); + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get control value. Ctrl-id [%d], err code[%x]", camerasrc_control_channel->id, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_set_exposure( GstCameraSrc* camerasrc, gint type, gint value1, gint value2 ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + camerasrc_frac_t frac; + + g_return_val_if_fail( camerasrc, FALSE ); + + /* TODO : F number */ + switch( type ) + { + case GST_CAMERA_CONTROL_F_NUMBER: + error = CAMERASRC_SUCCESS; + break; + case GST_CAMERA_CONTROL_SHUTTER_SPEED: + frac.numerator = value1; + frac.denominator = value2; + __ta__(" camerasrc_set_shutter_speed", + error = camerasrc_set_shutter_speed( camerasrc->v4l2_handle, frac ); + ) + break; + case GST_CAMERA_CONTROL_ISO: + __ta__(" camerasrc_set_iso_value", + error = camerasrc_set_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_ISO, value1 ); + ) + break; + case GST_CAMERA_CONTROL_PROGRAM_MODE: + __ta__(" camerasrc_set_control", + error = camerasrc_set_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_PROGRAM_MODE, value1 ); + ) + break; + case GST_CAMERA_CONTROL_EXPOSURE_MODE: + __ta__(" camerasrc_set_exposure_mode", + error = camerasrc_set_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_PHOTOMETRY, value1 ); + ) + break; + case GST_CAMERA_CONTROL_EXPOSURE_VALUE: + frac.numerator = value1; + frac.denominator = value2; + __ta__(" camerasrc_set_exposure_value", + error = camerasrc_set_exposure_value( camerasrc->v4l2_handle, frac ); + ) + break; + default: + gst_camerasrc_debug( "Not supported type." ); + return FALSE; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to set exposure. Type[%d],value1[%d],value2[%d],err code[%x]", type, value1, value2, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_exposure( GstCameraSrc* camerasrc, gint type, gint* value1, gint* value2 ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + camerasrc_frac_t frac; + + g_return_val_if_fail( camerasrc, FALSE ); + + /* TODO : F number */ + switch( type ) + { + case GST_CAMERA_CONTROL_F_NUMBER: + break; + case GST_CAMERA_CONTROL_SHUTTER_SPEED: + error = camerasrc_get_shutter_speed( camerasrc->v4l2_handle, &frac ); + if( error == CAMERASRC_SUCCESS ) + { + *value1 = frac.numerator; + *value2 = frac.denominator; + } + break; + case GST_CAMERA_CONTROL_ISO: + error = camerasrc_get_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_ISO, value1 ); + break; + case GST_CAMERA_CONTROL_PROGRAM_MODE: + error = camerasrc_get_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_PROGRAM_MODE, value1 ); + break; + case GST_CAMERA_CONTROL_EXPOSURE_MODE: + error = camerasrc_get_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_PHOTOMETRY, value1 ); + break; + case GST_CAMERA_CONTROL_EXPOSURE_VALUE: + error = camerasrc_get_exposure_value( camerasrc->v4l2_handle, &frac ); + if( error == CAMERASRC_SUCCESS ) + { + *value1 = frac.numerator; + *value2 = frac.denominator; + } + break; + default: + gst_camerasrc_debug( "Not supported type." ); + return FALSE; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get exposure. Type [%d]", type ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_set_capture_mode( GstCameraSrc* camerasrc, gint type, gint value ) +{ + /* TODO : single/multishot select(capture mode), output mode, frame count, JPEG quality */ + + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + switch( type ) + { + case GST_CAMERA_CONTROL_CAPTURE_MODE: + break; + case GST_CAMERA_CONTROL_OUTPUT_MODE: + break; + case GST_CAMERA_CONTROL_FRAME_COUNT: + break; + case GST_CAMERA_CONTROL_JPEG_QUALITY: + break; + default: + gst_camerasrc_debug( "Not supported type." ); + return FALSE; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to set capture mode. Type[%d],value[%d],err code[%x]", type, value, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_capture_mode( GstCameraSrc* camerasrc, gint type, gint *value ) +{ + /* TODO : single/multishot select(capture mode), output mode, frame count, JPEG quality */ + + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + switch( type ) + { + case GST_CAMERA_CONTROL_CAPTURE_MODE: + break; + case GST_CAMERA_CONTROL_OUTPUT_MODE: + break; + case GST_CAMERA_CONTROL_FRAME_COUNT: + break; + case GST_CAMERA_CONTROL_JPEG_QUALITY: + break; + default: + gst_camerasrc_debug( "Not supported type." ); + return FALSE; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to set capture mode. Type[%d],value[%d],err code[%x]", type, value, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_set_strobe( GstCameraSrc* camerasrc, gint type, gint value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + switch( type ) + { + case GST_CAMERA_CONTROL_STROBE_MODE: + error = camerasrc_set_strobe_mode( camerasrc->v4l2_handle, value ); + break; + case GST_CAMERA_CONTROL_STROBE_CONTROL: + case GST_CAMERA_CONTROL_STROBE_CAPABILITIES: + case GST_CAMERA_CONTROL_STROBE_STATUS: + case GST_CAMERA_CONTROL_STROBE_EV: + default: + gst_camerasrc_debug( "Not supported type[%d], return CAMERASRC_ERR_DEVICE_NOT_SUPPORT.", type ); + error = CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + break; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to set strobe. Type[%d],value[%d],err code[%x]", type, value, error ); + return FALSE; + } + + gst_camerasrc_debug( "Succeed to set strobe. Type[%d],value[%d]", type, value ); + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_strobe( GstCameraSrc* camerasrc, gint type, gint* value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + switch( type ) + { + case GST_CAMERA_CONTROL_STROBE_MODE: + error = camerasrc_get_strobe_mode( camerasrc->v4l2_handle, (camerasrc_strobe_mode_t *)value ); + break; + case GST_CAMERA_CONTROL_STROBE_CONTROL: + case GST_CAMERA_CONTROL_STROBE_CAPABILITIES: + case GST_CAMERA_CONTROL_STROBE_STATUS: + case GST_CAMERA_CONTROL_STROBE_EV: + default: + gst_camerasrc_debug( "Not supported type[%d].", type ); + error = CAMERASRC_ERR_DEVICE_NOT_SUPPORT; + break; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get strobe. Type[%d],err code[%x]", type, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_set_detect( GstCameraSrc* camerasrc, gint type, gint value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + /* TODO : detection number, focus select, select number, detect status */ + switch( type ) + { + case GST_CAMERA_CONTROL_FACE_DETECT_MODE: + if( value == 0 ) + { + error = camerasrc_stop_facedetection(camerasrc->v4l2_handle ); + } + else if( value == 1 ) + { + error = camerasrc_start_facedetection(camerasrc->v4l2_handle ); + } + break; + case GST_CAMERA_CONTROL_FACE_DETECT_NUMBER: + break; + case GST_CAMERA_CONTROL_FACE_FOCUS_SELECT: + break; + case GST_CAMERA_CONTROL_FACE_SELECT_NUMBER: + break; + case GST_CAMERA_CONTROL_FACE_DETECT_STATUS: + break; + default: + gst_camerasrc_debug( "Not supported type." ); + return FALSE; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to set detect. Type[%d],value[%d],err code[%x]", type, value, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_detect( GstCameraSrc* camerasrc, gint type, gint *value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + /* TODO : detection number, focus select, select number, detect status */ + switch( type ) + { + case GST_CAMERA_CONTROL_FACE_DETECT_MODE: + error = camerasrc_get_facedetection(camerasrc->v4l2_handle, value); + break; + case GST_CAMERA_CONTROL_FACE_DETECT_NUMBER: + break; + case GST_CAMERA_CONTROL_FACE_FOCUS_SELECT: + break; + case GST_CAMERA_CONTROL_FACE_SELECT_NUMBER: + break; + case GST_CAMERA_CONTROL_FACE_DETECT_STATUS: + break; + default: + gst_camerasrc_debug( "Not supported type." ); + return FALSE; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get detect. Type[%d],err code[%x]", type, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_set_zoom( GstCameraSrc* camerasrc, gint type, gint value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + switch( type ) + { + case GST_CAMERA_CONTROL_DIGITAL_ZOOM: + error = camerasrc_set_control (camerasrc->v4l2_handle, CAMERASRC_CTRL_DIGITAL_ZOOM, value ); + break; + case GST_CAMERA_CONTROL_OPTICAL_ZOOM: + error = camerasrc_set_control (camerasrc->v4l2_handle, CAMERASRC_CTRL_OPTICAL_ZOOM, value ); + break; + default: + gst_camerasrc_debug( "Not supported type." ); + return FALSE; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to set zoom. Type[%d],value[%d],err code[%x]", type, value, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_zoom( GstCameraSrc* camerasrc, gint type, gint *value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + switch( type ) + { + case GST_CAMERA_CONTROL_DIGITAL_ZOOM: + error = camerasrc_get_control (camerasrc->v4l2_handle, CAMERASRC_CTRL_DIGITAL_ZOOM, value ); + break; + case GST_CAMERA_CONTROL_OPTICAL_ZOOM: + error = camerasrc_get_control (camerasrc->v4l2_handle, CAMERASRC_CTRL_OPTICAL_ZOOM, value ); + break; + default: + gst_camerasrc_debug( "Not supported type." ); + return FALSE; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get zoom. Type[%d],err code[%x]", type, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_set_focus( GstCameraSrc* camerasrc, gint focus_mode, gint focus_range ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + error = camerasrc_init_autofocusing_mode( camerasrc->v4l2_handle, focus_mode, focus_range ); + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to set AF mode." ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_focus( GstCameraSrc* camerasrc, gint *focus_mode, gint *focus_range ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + error = camerasrc_get_autofocusing_mode(camerasrc->v4l2_handle, (camerasrc_af_mode_t *)focus_mode, (camerasrc_af_scan_range_t *)focus_range); + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get AF mode." ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_start_auto_focus( GstCameraSrc* camerasrc ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + MMTA_ACUM_ITEM_BEGIN( "AutoFocus operating time", FALSE ); + + error = camerasrc_start_autofocusing( camerasrc->v4l2_handle ); + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to start AF. error[%x]", error ); + return FALSE; + } + else + { + gst_camerasrc_debug( "Succeeded to start AF." ); + return TRUE; + } +} + +gboolean +gst_camerasrc_control_stop_auto_focus( GstCameraSrc* camerasrc ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + int try_count = 0; + camerasrc_auto_focus_status_t af_status = CAMERASRC_AUTO_FOCUS_STATUS_RELEASED; + + g_return_val_if_fail( camerasrc, FALSE ); + + error = camerasrc_stop_autofocusing( camerasrc->v4l2_handle ); + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to stop autofocus." ); + return FALSE; + } + + while( try_count++ < CAMERA_CONTROL_AF_STOP_TOTALTIME/CAMERA_CONTROL_AF_STOP_INTERVAL ) + { + error = camerasrc_get_autofocusing_status( camerasrc->v4l2_handle, &af_status ); + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get af status.(%x)", error ); + return FALSE; + } + + if( af_status == CAMERASRC_AUTO_FOCUS_STATUS_RELEASED ) + { + gst_camerasrc_debug( "AF Stop done. try count[%d]", try_count ); + break; + } + + usleep( CAMERA_CONTROL_AF_STOP_INTERVAL ); + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_set_focus_level( GstCameraSrc* camerasrc, gint focus_level ) +{ + /* TODO : */ + + gst_camerasrc_debug( "Not support" ); + return FALSE; +} + +gboolean +gst_camerasrc_control_get_focus_level( GstCameraSrc* camerasrc, gint *focus_level ) +{ + /* TODO : */ + + gst_camerasrc_debug( "Not support" ); + return FALSE; +} + +gboolean +gst_camerasrc_control_set_auto_focus_area( GstCameraSrc* camerasrc, GstCameraControlRectType rect ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + camerasrc_rect_t camerasrc_rect = { 0, 0, 0, 0 }; + + g_return_val_if_fail( camerasrc, FALSE ); + + if (camerasrc->camera_id == CAMERASRC_DEV_ID_SECONDARY) { + GST_INFO_OBJECT(camerasrc, "It's secondary camera. Skip setting..."); + return TRUE; + } + + camerasrc_rect.x = rect.x; + camerasrc_rect.y = rect.y; + camerasrc_rect.width = rect.width; + camerasrc_rect.height = rect.height; + + GST_INFO_OBJECT(camerasrc, "Set AF area %d,%d,%dx%d", + camerasrc_rect.x, camerasrc_rect.y, + camerasrc_rect.width, camerasrc_rect.height); + + error = camerasrc_set_autofocusing_area( camerasrc->v4l2_handle, &camerasrc_rect ); + + if( error != CAMERASRC_SUCCESS ) + { + GST_ERROR_OBJECT(camerasrc, "Failed to set auto focus area."); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_auto_focus_area( GstCameraSrc* camerasrc, GstCameraControlRectType* rect ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + camerasrc_rect_t camerasrc_rect = { 0, 0, 0, 0 }; + + g_return_val_if_fail( camerasrc, FALSE ); + g_return_val_if_fail( rect, FALSE ); + + error = camerasrc_get_autofocusing_area( camerasrc->v4l2_handle, &camerasrc_rect ); + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get auto focus area." ); + + rect->x = rect->y = -1; + rect->width = rect->height = -1; + + return FALSE; + } + + rect->x = camerasrc_rect.x; + rect->y = camerasrc_rect.y; + rect->width = camerasrc_rect.width; + rect->height = camerasrc_rect.height; + + return TRUE; +} + +gboolean +gst_camerasrc_control_set_wdr( GstCameraSrc* camerasrc, gint value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + error = camerasrc_set_control (camerasrc->v4l2_handle, CAMERASRC_CTRL_WIDE_DYNAMIC_RANGE, value ); + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to set wdr. value[%d],err code[%x]", value, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_wdr( GstCameraSrc* camerasrc, gint* value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + error = camerasrc_get_control (camerasrc->v4l2_handle, CAMERASRC_CTRL_WIDE_DYNAMIC_RANGE, value ); + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get wdr. err code[%x]", error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_set_ahs( GstCameraSrc* camerasrc, gint value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + error = camerasrc_set_control (camerasrc->v4l2_handle, CAMERASRC_CTRL_ANTI_HANDSHAKE, value ); + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to set ahs. value[%d],err code[%x]", value, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_ahs( GstCameraSrc* camerasrc, gint* value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + error = camerasrc_get_control (camerasrc->v4l2_handle, CAMERASRC_CTRL_ANTI_HANDSHAKE, value ); + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get ahs. err code[%x]", error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_set_part_color( GstCameraSrc* camerasrc, gint type, gint value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + switch( type ) + { + case GST_CAMERA_CONTROL_PART_COLOR_SRC: + error = camerasrc_set_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_PARTCOLOR_SRC, value ); + break; + case GST_CAMERA_CONTROL_PART_COLOR_DST: + error = camerasrc_set_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_PARTCOLOR_DST, value ); + break; + case GST_CAMERA_CONTROL_PART_COLOR_MODE: + error = camerasrc_set_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_PARTCOLOR_MODE, value ); + break; + default: + gst_camerasrc_debug( "Not supported type." ); + return FALSE; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to set part color. Type[%d],value[%d],err code[%x]", type, value, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_part_color( GstCameraSrc* camerasrc, gint type, gint* value ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + switch( type ) + { + case GST_CAMERA_CONTROL_PART_COLOR_SRC: + error = camerasrc_get_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_PARTCOLOR_SRC, value ); + break; + case GST_CAMERA_CONTROL_PART_COLOR_DST: + error = camerasrc_get_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_PARTCOLOR_DST, value ); + break; + case GST_CAMERA_CONTROL_PART_COLOR_MODE: + error = camerasrc_get_control( camerasrc->v4l2_handle, CAMERASRC_CTRL_PARTCOLOR_MODE, value ); + break; + default: + gst_camerasrc_debug( "Not supported type." ); + return FALSE; + } + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get part color. Type[%d],err code[%x]", type, error ); + return FALSE; + } + + return TRUE; +} + +gboolean +gst_camerasrc_control_get_exif_info( GstCameraSrc* camerasrc, GstCameraControlExifInfo* info ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + camerasrc_exif_t exif_struct; + + g_return_val_if_fail( camerasrc, FALSE ); + + error = camerasrc_get_exif_info( camerasrc->v4l2_handle, &exif_struct ); + + if( error != CAMERASRC_SUCCESS ) + { + gst_camerasrc_debug( "Failed to get exif info. err code[%x]", error ); + return FALSE; + } + + /* Dynamic value */ + info->exposure_time_numerator = exif_struct.exposure_time_numerator; + info->exposure_time_denominator = exif_struct.exposure_time_denominator; + info->shutter_speed_numerator = exif_struct.shutter_speed_numerator; + info->shutter_speed_denominator = exif_struct.shutter_speed_denominator; + info->brigtness_numerator = exif_struct.brigtness_numerator; + info->brightness_denominator = exif_struct.brightness_denominator; + info->iso = exif_struct.iso; + info->flash = exif_struct.flash; + info->metering_mode = exif_struct.metering_mode; + info->exif_image_width = exif_struct.exif_image_width; + info->exif_image_height = exif_struct.exif_image_height; + info->software_used = exif_struct.software_used; + info->exposure_bias_in_APEX = exif_struct.exposure_bias_in_APEX; + + /* Fixed value */ + info->component_configuration = exif_struct.component_configuration; + info->colorspace = exif_struct.colorspace; + info->max_lens_aperture_in_APEX = exif_struct.max_lens_aperture_in_APEX; + + /*FIXME*/ + if (camerasrc->camera_id == CAMERASRC_DEV_ID_PRIMARY) { + info->focal_len_numerator = CAMERASRC_PRIMARY_FOCAL_LEGNTH_NUM; + info->focal_len_denominator = CAMERASRC_PRIMARY_FOCAL_LEGNTH_DENOM; + info->aperture_f_num_numerator = CAMERASRC_PRIMARY_F_NUMBER_NUM; + info->aperture_f_num_denominator = CAMERASRC_PRIMARY_F_NUMBER_DENOM; + } else { + info->focal_len_numerator = CAMERASRC_SECONDARY_FOCAL_LEGNTH_NUM; + info->focal_len_denominator = CAMERASRC_SECONDARY_FOCAL_LEGNTH_DENOM; + info->aperture_f_num_numerator = CAMERASRC_SECONDARY_F_NUMBER_NUM; + info->aperture_f_num_denominator = CAMERASRC_SECONDARY_F_NUMBER_DENOM; + } + info->aperture_in_APEX = CAMERASRC_EXIF_APERTURE_VALUE_IN_APEX(info->aperture_f_num_numerator, info->aperture_f_num_denominator); + + return TRUE; +} + +gboolean gst_camerasrc_control_get_basic_dev_info ( GstCameraSrc* camerasrc, gint dev_id, GstCameraControlCapsInfoType* info ) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + /** + * Just implementation issue, but at this time, we assume + * GstCameraControlCapsInfoType is exactly same with camerasrc_caps_info_t + * For performance. + * Here is plugin code. we can do like this? + */ +#if 1 + error = camerasrc_read_basic_dev_info(dev_id, (camerasrc_caps_info_t*)info); + if(error != CAMERASRC_SUCCESS) + { + return FALSE; + } +#else + int i, j, k; + camerasrc_caps_info_t caps_info; + + error = camerasrc_read_basic_dev_info(dev_id, &caps_info); + if(error != CAMERASRC_SUCCESS) + { + return FALSE; + } + + if(caps_info.num_fmt_desc != 0) + { + info->num_fmt_desc = caps_info.num_fmt_desc; + for (i=0; i<caps_info.num_fmt_desc; i++) { + if(caps_info.fmt_desc[i].num_resolution != 0) + { + info->fmt_desc[i].fcc = caps_info.fmt_desc[i].fcc; + info->fmt_desc[i].num_resolution = caps_info.fmt_desc[i].num_resolution; + for (j=0; j<caps_info.fmt_desc[i].num_resolution; j++) + { + if(caps_info.fmt_desc[i].resolutions[j].num_avail_tpf != 0) + { + info->fmt_desc[i].resolutions[j].w = caps_info.fmt_desc[i].resolutions[j].w; + info->fmt_desc[i].resolutions[j].h = caps_info.fmt_desc[i].resolutions[j].h; + info->fmt_desc[i].resolutions[j].num_avail_tpf = caps_info.fmt_desc[i].resolutions[j].num_avail_tpf; + for(k=0; k< caps_info.fmt_desc[i].resolutions[j].num_avail_tpf; k++) + { + info->fmt_desc[i].resolutions[j].tpf[k].num = caps_info.fmt_desc[i].resolutions[j].tpf[k].num; + info->fmt_desc[i].resolutions[j].tpf[k].den = caps_info.fmt_desc[i].resolutions[j].tpf[k].den; + } + } + else + { + /* No available timeperframe */ + return FALSE; + } + } + } + else + { + /* No available resolution set */ + return FALSE; + } + } + } + else + { + /* No available image format(fourcc) */ + return FALSE; + } +#endif + return TRUE; +} + +gboolean gst_camerasrc_control_get_misc_dev_info( GstCameraSrc* camerasrc, gint dev_id, GstCameraControlCtrlListInfoType * info) +{ + gst_camerasrc_debug( "" ); + + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + /** + * Just implementation issue, but at this time, we assume + * GstCameraControlCtrlListInfoType is exactly same with camerasrc_ctrl_list_info_t + * For performance. + * Here is plugin code. we can do like this? + */ +#if 1 + error = camerasrc_read_misc_dev_info(dev_id, (camerasrc_ctrl_list_info_t*)info); + if(error != CAMERASRC_SUCCESS) + { + return FALSE; + } +#else + int i, j; + camerasrc_ctrl_list_info_t ctrl_info; + + error = camerasrc_read_misc_dev_info(dev_id, &ctrl_info); + if(error != CAMERASRC_SUCCESS) + { + return FALSE; + } + + if(ctrl_info.num_ctrl_list_info != 0) + { + info->num_ctrl_list_info = ctrl_info.num_ctrl_list_info; + for(i=0; i< ctrl_info.num_ctrl_list_info; i++) + { + info->ctrl_info[i].camerasrc_ctrl_id = ctrl_info.ctrl_info[i].camerasrc_ctrl_id; + info->ctrl_info[i].v4l2_ctrl_id = ctrl_info.ctrl_info[i].v4l2_ctrl_id; + info->ctrl_info[i].ctrl_type = ctrl_info.ctrl_info[i].ctrl_type; + info->ctrl_info[i].max = ctrl_info.ctrl_info[i].max; + info->ctrl_info[i].min = ctrl_info.ctrl_info[i].min; + info->ctrl_info[i].step = ctrl_info.ctrl_info[i].step; + info->ctrl_info[i].default_val = ctrl_info.ctrl_info[i].default_val; + info->ctrl_info[i].num_ctrl_menu = ctrl_info.ctrl_info[i].num_ctrl_menu; + memcpy(info->ctrl_info[i].ctrl_name,ctrl_info.ctrl_info[i].ctrl_name,MAX_SZ_CTRL_NAME_STRING); + if(ctrl_info.ctrl_info[i].ctrl_type == CTRL_TYPE_ARRAY && ctrl_info.ctrl_info[i].num_ctrl_menu != 0) + { + for(j=0; j<ctrl_info.ctrl_info[i].num_ctrl_menu; j++) + { + info->ctrl_info[i].ctrl_menu[j].menu_index = ctrl_info.ctrl_info[i].ctrl_menu[j].menu_index; + memcpy(info->ctrl_info[i].ctrl_menu[j].menu_name, ctrl_info.ctrl_info[i].ctrl_menu[j].menu_name, MAX_SZ_CTRL_NAME_STRING); + } + } + else + { + /* Not a menu type or not available menus */ + return FALSE; + } + } + } + else + { + /* Not avaliable controls */ + return FALSE; + } +#endif + return TRUE; +} + +gboolean gst_camerasrc_control_get_extra_dev_info( GstCameraSrc* camerasrc, gint dev_id, GstCameraControlExtraInfoType * info) +{ + int error = CAMERASRC_ERROR; + + g_return_val_if_fail( camerasrc, FALSE ); + + gst_camerasrc_debug( "" ); + + /** + * Just implementation issue, but at this time, we assume + * GstCameraControlCtrlListInfoType is exactly same with camerasrc_ctrl_list_info_t + * For performance. + * Here is plugin code. we can do like this? + */ + + error = camerasrc_read_extra_dev_info(dev_id, (camerasrc_extra_info_t*)info); + if(error != CAMERASRC_SUCCESS) + { + return FALSE; + } + + return TRUE; +} + +void gst_camerasrc_control_set_capture_command( GstCameraSrc* camerasrc, GstCameraControlCaptureCommand cmd ) +{ + gst_camerasrc_debug( "" ); + + if( camerasrc == NULL ) + { + gst_camerasrc_debug( "camerasrc is NULL" ); + return; + } + + gst_camerasrc_set_capture_command( camerasrc, cmd ); + + return; +} diff --git a/camerasrc/src/include/camerasrc-common.h b/camerasrc/src/include/camerasrc-common.h new file mode 100644 index 0000000..207cdd3 --- /dev/null +++ b/camerasrc/src/include/camerasrc-common.h @@ -0,0 +1,408 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __CAMERASRC_COMMON_H__ +#define __CAMERASRC_COMMON_H__ + +#include <stdio.h> +#include <malloc.h> +#include <pthread.h> +#include <errno.h> /*EXXX*/ +#include <sys/ioctl.h> /*ioctl*/ +#include <string.h> /*memcpy*/ + +#include <sys/types.h> /*open*/ +#include <sys/stat.h> +#include <fcntl.h> + +#include <unistd.h> /*mmap*/ +#include <sys/mman.h> /*alloc series, free..*/ +#include <sys/time.h> /*gettimeofday*/ +#include <math.h> /*log2*/ +#include <gst/gst.h> + +#undef __ASM_ARM_TYPES_H +#undef __ASSEMBLY_ +#undef _I386_TYPES_H + +#include <asm/types.h> +#include <linux/videodev2.h> /* V4L2 APIs */ +#include <linux/videodev2_exynos_camera.h> +#include <linux/videodev2_exynos_media.h> + +#include "camerasrc.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * Memory utility definitions + */ +#if !defined (PAGE_SHIFT) + #define PAGE_SHIFT sysconf(_SC_PAGESIZE) +#endif +#if !defined (PAGE_SIZE) + #define PAGE_SIZE (1UL << PAGE_SHIFT) +#endif +#if !defined (PAGE_MASK) + #define PAGE_MASK (~(PAGE_SIZE-1)) +#endif + +#define PAGE_ALIGN(addr) (((addr)+PAGE_SIZE-1)&PAGE_MASK) +#define CLEAR(x) memset (&(x), 0, sizeof (x)) + +#define CAMERASRC_MAX_WIDTH 2560 +#define CAMERASRC_MAX_HEIGHT 1920 +#define CAMERASRC_CID_NOT_SUPPORT -1 +#define CAMERASRC_USRPTR_MAX_BUFFER_NUM 12 +#define CAMERASRC_MAX_FILENAME_LEN 255 +#define CAMERASRC_DEV_NODE_PREFIX "/dev/video" +#define CAMERASRC_DEV_FD_INIT -1 +#define CAMERASRC_DEV_ON_ACCESS_STR "ONACCESS" +#define CAMERASRC_DEV_RELEASED_STR "RELEASED" +#define CAMERASRC_OPENED_CHK_FILENAME "/tmp/.dev_chk" +#define CAMERASRC_THREAD_KILL -999 +#define CAMERASRC_DEV_FD_EMERGENCY_CLOSED -999 +#define CAMERASRC_ERRMSG_MAX_LEN 128 +#define CAMERASRC_FRAME_DUMP_PATH "/tmp/" +#define CAMERASRC_DBG_SCRIPT_PATH "/mnt/mmc/cam_dbg_script" +#define CAMERASRC_PRIMARY_BASIC_INFO_PATH "/tmp/.camprimarybasicinfo" +#define CAMERASRC_PRIMARY_MISC_INFO_PATH "/tmp/.camprimarymiscinfo" +#define CAMERASRC_PRIMARY_EXTRA_INFO_PATH "/tmp/.camprimaryextrainfo" +#define CAMERASRC_SECONDARY_BASIC_INFO_PATH "/tmp/.camsecondarybasicinfo" +#define CAMERASRC_SECONDARY_MISC_INFO_PATH "/tmp/.camsecondarymiscinfo" +#define CAMERASRC_SECONDARY_EXTRA_INFO_PATH "/tmp/.camsecondaryextrainfo" +#define CAMERASRC_MAX_IMAGE_BUFFER_PLANES 3 + +#define USE_OPEN_CHK /*< Using open check with temporary file */ +#define USE_SENSOR_MODE 1 +/*#define USE_SKIP_FRAME*/ /*< Skip frame toggle */ +/*#define USE_IOCTL_DEBUG*/ /*< For debugging ioctl name, argument, address, etc */ +/*#define USE_FRAME_COPY_BOUNDARY_CHECK*/ /*< Copy boundary checks occurs seg fault when overrun */ +/*#define USE_SKIP_FRAME_AT_RAW_FRAME*/ /*< In pumping raw frame, initial 2-3 frames are darker. so skip it */ +/*#define USE_CAMERASRC_FRAME_DUMP*/ /*< Debug system annoying me. Use printf!!!! */ +/*#define USE_USERPTR_DEBUG*/ +/*#define ENABLE_Q_ERROR*/ + +#ifndef GST_CAT_DEFAULT +GST_DEBUG_CATEGORY_EXTERN(camerasrc_debug); +#define GST_CAT_DEFAULT camerasrc_debug +#endif /* GST_CAT_DEFAULT */ + + +#define camsrc_info(msg, args...) GST_INFO(msg, ##args) +#define camsrc_warning(msg, args...) GST_WARNING(msg, ##args) +#define camsrc_error(msg, args...) GST_ERROR(msg, ##args) +#define camsrc_critical(msg, args...) GST_ERROR(msg, ##args) +#define camsrc_assert(condition) { \ + if (!(condition)) { \ + GST_ERROR("failed [%s]", #condition); \ + } \ +} + +/* + * Values for internal + */ +enum camerasrc_op_mode_t { + CAMERASRC_OP_PREVIEW = 0, + CAMERASRC_OP_CAPTURE, + CAMERASRC_OP_VIDEO, + CAMERASRC_OP_REGISTER_VALUE, + CAMERASRC_OP_NUM, +}; + +/* + * Values for internal + */ +enum camerasrc_ctrl_property_t{ + CAMERASRC_CTRL_SUPPORT = 0, + CAMERASRC_CTRL_MAX_VALUE, + CAMERASRC_CTRL_MIN_VALUE, + CAMERASRC_CTRL_CID_VALUE, + CAMERASRC_CTRL_CURRENT_VALUE, + CAMERASRC_CTRL_PROPERTY_NUM, +}; + +/* + * Values for internal + */ +enum camerasrc_quality_t{ + CAMERASRC_QUALITY_NORMAL = 0, + CAMERASRC_QUALITY_HIGH, + CAMERASRC_QUALITY_NUM, +}; + +enum camerasrc_dev_recog_t{ + CAMERASRC_DEV_RECOG_ID = 0, + CAMERASRC_DEV_RECOG_INDEX, + CAMERASRC_DEV_RECOG_NUM, +}; + +/** + * Phase, camerasrc consist of two phase, running and non-running. + */ +typedef enum { + CAMERASRC_PHASE_RUNNING = 0, + CAMERASRC_PHASE_NON_RUNNING, + CAMERASRC_PHASE_NUM, +} _camerasrc_phase_t; + +typedef enum { + CAMERASRC_MISC_STILL_SIGNAL = 0, + CAMERASRC_MISC_SKIP_FRAME, + CAMERASRC_MISC_FUNC_NUM, +} _camerasrc_misc_func_t; + +typedef enum{ + _CAMERASRC_CMD_AF_CONTROL = 0, + _CAMERASRC_CMD_AF_AREA, + _CAMERASRC_CMD_STROBE_MODE, + _CAMERASRC_CMD_FACEDETECTION, + _CAMERASRC_CMD_SHUTTER_SPEED, + _CAMERASRC_CMD_SUPPORT_EMBED_EXIF, + _CAMERASRC_CMD_SUPPORT_JPEG_ENCODING, + _CAMERASRC_CMD_CHECK_ESD, + _CAMERASRC_CMD_JPEG_COMPRESS_RATIO, + _CAMERASRC_CMD_JPEG_LENGTH, + _CAMERASRC_CMD_JPEG_THMBNL_LENGTH, + _CAMERASRC_CMD_JPEG_THMBNL_OFFSET, + _CAMERASRC_CMD_JPEG_SCRNL_LENGTH, + _CAMERASRC_CMD_JPEG_SCRNL_OFFSET, + _CAMERASRC_CMD_EXPOSURE_VALUE, + _CAMERASRC_CMD_ESD_CHECK, + _CAMERASRC_CMD_FRAME_DATA, + _CAMERASRC_CMD_EXIF_INFO, + _CAMERASRC_CMD_CTRL, + _CAMERASRC_CMD_ROTATION, + _CAMERASRC_CMD_SENSOR_MODE, + _CAMERASRC_CMD_VFLIP, + _CAMERASRC_CMD_HFLIP, + _CAMERASRC_CMD_NUM, +}_camsrc_cmd_t; + +typedef struct{ + int cid; + int value; +} _camerasrc_ctrl_t; + +enum { + _CAMERASRC_FACEDETECTION_START = 0, + _CAMERASRC_FACEDETECTION_STOP, + _CAMERASRC_FACEDETECTION_NUM, +}; + +enum { + _CAMERASRC_AF_START = 0, + _CAMERASRC_AF_STOP, + _CAMERASRC_AF_RELEASE, + _CAMERASRC_AF_INIT, + _CAMERASRC_AF_DESTROY, +}; + +// U T I L I T Y D E F I N I T I O N +/** + * Mapping device index - Device ID + */ +#define _CAMERASRC_GET_DEV_INDEX(dev_id) _camerasrc_dev_index[dev_id][CAMERASRC_DEV_RECOG_INDEX] +#define _CAMERASRC_GET_DEV_ID(dev_idx) _camerasrc_dev_index[dev_idx][CAMERASRC_DEV_RECOG_ID] + +/** + * For colorspace - pixel format combinability check + */ +#define _CAMERASRC_MATCH_COL_TO_PIX(dev_id, colorspace, pixel_fmt, quality) _camerasrc_match_col_to_pix[dev_id][colorspace][pixel_fmt][quality] + +/** + * For control capability check + */ +#define _CAMERASRC_CHK_SUPPORT_CONTROL(ctrl_id, dev_id) _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_SUPPORT] +#define _CAMERASRC_MAX_VALUE(ctrl_id, dev_id) _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_MAX_VALUE] +#define _CAMERASRC_MIN_VALUE(ctrl_id, dev_id) _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_MIN_VALUE] +#define _CAMERASRC_GET_CID(ctrl_id, dev_id) _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_CID_VALUE] +#define _CAMERASRC_GET_CURRENT_VALUE(ctrl_id, dev_id) _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_CURRENT_VALUE] +#define _CAMERASRC_SET_CURRENT_VALUE(ctrl_id, dev_id, value) _camerasrc_ctrl_list[dev_id][ctrl_id][CAMERASRC_CTRL_CURRENT_VALUE] = value + +/** + * Need miscellaneous function on operation? + */ +#define _CAMERASRC_NEED_MISC_FUNCTION(dev_id, operation, colorspace, misc_func) _camerasrc_misc_func_list[dev_id][operation][colorspace][misc_func] +#define _CAMERASRC_SUPPORT_AF(dev_id) _camerasrc_af_support[dev_id] + +/** + * Utility definitions + */ +#define CAMERASRC_SET_STATE(handle, state) { \ + handle->prev_state = handle->cur_state; \ + handle->cur_state = state; \ + camsrc_info("Set state [%d] -> [%d]", handle->prev_state, handle->cur_state); \ +} +#define CAMERASRC_SET_PHASE(handle, phase) handle->cur_phase = phase; +#define CAMERASRC_STATE(handle) (handle->cur_state) +#define CAMERASRC_PREV_STREAM_STATE(handle) -1 +#define CAMERASRC_PHASE(handle) (handle->cur_phase) +#define CAMERASRC_HANDLE(handle) ((camerasrc_handle_t*) handle) +#define CAMERASRC_CURRENT_DEV_ID(handle) (handle->dev_id) +#define CAMERASRC_IS_DEV_CLOSED(p) (p->dev_fd == -1 || p->dev_fd == CAMERASRC_DEV_FD_EMERGENCY_CLOSED) + +#define YUV422_SIZE(handle) ((handle->format.img_size.dim.height * handle->format.img_size.dim.width) << 1) +#define YUV420_SIZE(handle) ((handle->format.img_size.dim.height * handle->format.img_size.dim.width * 3) >> 1) +#define RGB565_SIZE(handle) ((handle->format.img_size.dim.height * handle->format.img_size.dim.width) << 1) + +#define ISO_APPROXIMATE_VALUE(iso_in, iso_approximated) { \ + if(iso_in > 8.909 && iso_in <= 11.22) iso_approximated = 10; \ + else if(iso_in > 11.22 && iso_in <= 14.14) iso_approximated = 12; \ + else if(iso_in > 14.14 && iso_in <= 17.82) iso_approximated = 16; \ + else if(iso_in > 17.82 && iso_in <= 22.45) iso_approximated = 20; \ + else if(iso_in > 22.45 && iso_in <= 28.28) iso_approximated = 25; \ + else if(iso_in > 28.28 && iso_in <= 35.64) iso_approximated = 32; \ + else if(iso_in > 35.64 && iso_in <= 44.90) iso_approximated = 40; \ + else if(iso_in > 44.90 && iso_in <= 56.57) iso_approximated = 50; \ + else if(iso_in > 56.57 && iso_in <= 71.27) iso_approximated = 64; \ + else if(iso_in > 71.27 && iso_in <= 89.09) iso_approximated = 80; \ + else if(iso_in > 89.09 && iso_in <= 112.2) iso_approximated = 100; \ + else if(iso_in > 112.2 && iso_in <= 141.4) iso_approximated = 125; \ + else if(iso_in > 141.4 && iso_in <= 178.2) iso_approximated = 160; \ + else if(iso_in > 178.2 && iso_in <= 224.5) iso_approximated = 200; \ + else if(iso_in > 224.5 && iso_in <= 282.8) iso_approximated = 250; \ + else if(iso_in > 282.8 && iso_in <= 356.4) iso_approximated = 320; \ + else if(iso_in > 356.4 && iso_in <= 449.0) iso_approximated = 400; \ + else if(iso_in > 449.0 && iso_in <= 565.7) iso_approximated = 500; \ + else if(iso_in > 565.7 && iso_in <= 712.7) iso_approximated = 640; \ + else if(iso_in > 712.7 && iso_in <= 890.9) iso_approximated = 800; \ + else if(iso_in > 890.9 && iso_in <= 1122) iso_approximated = 1000; \ + else if(iso_in > 1122 && iso_in <= 1414) iso_approximated = 1250; \ + else if(iso_in > 1414 && iso_in <= 1782) iso_approximated = 1600; \ + else if(iso_in > 1782 && iso_in <= 2245) iso_approximated = 2000; \ + else if(iso_in > 2245 && iso_in <= 2828) iso_approximated = 2500; \ + else if(iso_in > 2828 && iso_in <= 3564) iso_approximated = 3200; \ + else if(iso_in > 3564 && iso_in <= 4490) iso_approximated = 4000; \ + else if(iso_in > 4490 && iso_in <= 5657) iso_approximated = 5000; \ + else if(iso_in > 5657 && iso_in <= 7127) iso_approximated = 6400; \ + else if(iso_in > 7127 && iso_in <= 8909) iso_approximated = 8000; \ + else { \ + camsrc_warning("Invalid parameter(Maybe kernel failure).. give default value, 100");\ + iso_approximated = 100;\ + }\ +} + +#define PHOTOMETRY_MODE_TO_METERING_MODE(photometry_mode, metering_mode) { \ + if(photometry_mode == V4L2_PHOTOMETRY_MULTISEG) metering_mode = 1; \ + else if (photometry_mode == V4L2_PHOTOMETRY_CWA) metering_mode = 2; \ + else if (photometry_mode == V4L2_PHOTOMETRY_SPOT) metering_mode = 3; \ + else if (photometry_mode == V4L2_PHOTOMETRY_AFSPOT) metering_mode = 3; \ + else metering_mode = 1; \ +} + +#define CAMERASRC_EXIF_SHUTTERSPEED_VALUE_IN_APEX(NUM, DEN) (int)(-(log2((double)((double)NUM/(double)DEN)))) +#define CAMERASRC_EXIF_APERTURE_VALUE_IN_APEX(NUM, DEN) (int)(2 * (log2((double)((double)NUM/(double)DEN)))) + + +typedef void *(*camerasrc_signal_func_t) (camsrc_handle_t handle); +typedef int (*camerasrc_skip_frame_func_t) (camsrc_handle_t handle, long int timeout, int skip_frame); + +typedef struct _camerasrc_handle_t { + int is_async_open; + + /* device information */ + int dev_fd; + int cur_dev_id; + camerasrc_buffer_t alter_frame; + int check_esd; + int errnum; + int lens_rotation; /* physical rotation of lens */ + + /* state information */ + int prev_stream_state; + int prev_state; + int cur_state; + int cur_phase; + + /* image format information */ + int is_highquality; + int is_preset; + camerasrc_resol_name_t resolution; + camerasrc_format_t format; + + /* buffer information */ + camerasrc_io_method_t io_method; + camerasrc_usr_buf_t *present_buf; + int buffer_idx; + int num_buffers; + int queued_buffer_count; + int first_frame; + struct v4l2_buffer queued_buf_list[CAMERASRC_USRPTR_MAX_BUFFER_NUM]; + camerasrc_buffer_t *buffer; + camerasrc_buffer_t scrnl_buf; /* screennail buffer of captured JPEG image */ + + /* autofocusing information */ + camerasrc_af_mode_t cur_af_mode; + camerasrc_af_scan_range_t cur_af_range; + camerasrc_callback_t af_cb; + pthread_t focusing_thread; + pthread_cond_t af_wait_cond; + camerasrc_auto_focus_status_t af_status; + camerasrc_auto_focus_cmd_t af_cmd; + int af_dev_val; + void *af_usr_data; + int hold_af_after_capturing; + int af_init_called; /* whether af init was called after device open. */ + struct timeval set_af_area_time; /* for AF start delay after set AF area */ + + /* Jpg Still information */ + camerasrc_skip_frame_func_t skip_frame_func; + + /* Shutter & exposure value */ + int isAutoexposure; + + /* fps */ + camerasrc_frac_t timeperframe; + + /* sensor mode */ + camerasrc_sensor_mode_t sensor_mode; + + /* flip */ + int vflip; + int hflip; + + /* thread safe mechanism */ + pthread_mutex_t mutex; + pthread_mutex_t af_mutex; + pthread_cond_t cond; +} camerasrc_handle_t; + +typedef struct { + int (*_ioctl) (camerasrc_handle_t *handle, int request, void *arg); + int (*_ioctl_once) (camerasrc_handle_t *handle, int request, void *arg); + void *(*_run_autofocusing) (camerasrc_handle_t *handle); + int (*_skip_frame) (camerasrc_handle_t *handle, long int timeout, int skip_frame); + int (*_copy_frame) (camerasrc_handle_t *handle, camerasrc_buffer_t *src_buffer, camerasrc_buffer_t *dst_buffer, int isThumbnail); + int (*_set_cmd) (camerasrc_handle_t *handle, _camsrc_cmd_t cmd, void *value); + int (*_get_cmd) (camerasrc_handle_t *handle, _camsrc_cmd_t cmd, void *value); +} CAMERASRC_DEV_DEPENDENT_MISC_FUNC; + +#ifdef __cplusplus +} +#endif + +#endif /*__CAMERASRC_COMMON_H__*/ diff --git a/camerasrc/src/include/camerasrc-error.h b/camerasrc/src/include/camerasrc-error.h new file mode 100644 index 0000000..4ea8c7f --- /dev/null +++ b/camerasrc/src/include/camerasrc-error.h @@ -0,0 +1,140 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __CAMERASRC_ERROR_H__ +#define __CAMERASRC_ERROR_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * CAMSRC CLASS + */ +#define CAMERASRC_SUCCESS (0x00000000) /**< No Error */ +#define CAMERASRC_ERROR (0x80000000) /**< Error Class */ +#define CAMERASRC_WARING (0x70000000) /**< Waring Class */ + +/* + * Detail enumeration + */ +enum { + CAMSRC_IN_UNKNOWN = 0, + CAMSRC_IN_PARAMETER, + CAMSRC_IN_HANDLE, + CAMSRC_IN_POINTER, + CAMSRC_IN_VALUE, + CAMSRC_IN_OVERRUN, + CAMSRC_IN_UNDERRUN, + CAMSRC_IN_RANGE_OVER, + CAMSRC_IN_RANGE_UNDER, + CAMSRC_IN_MODE, + CAMSRC_IN_FORMAT, + CAMSRC_IN_CHANNEL, + CAMSRC_IN_SAMPLERATE, + CAMSRC_IN_LAYER, + CAMSRC_IN_ROTATE, + CAMSRC_IN_ALLOC, + CAMSRC_IN_INTERNAL, + /*Extra state for camera*/ + CAMSRC_IN_IO_CONTROL, + CAMSRC_IN_DEVICE_OPEN, + CAMSRC_IN_DEVICE_BUSY, + CAMSRC_IN_DEVICE_NOT_FOUND, + CAMSRC_IN_DEVICE_REGISTER_TROUBLE, + CAMSRC_IN_WAIT_RES, + CAMSRC_IN_SUPPORT, + CAMSRC_IN_STATE, + CAMSRC_IN_PRIVILEGE, + CAMSRC_IN_SECURITY_SERVICE, +}; + +/* + * CAMERASRC_WARNING + */ +#define CAMERASRC_WAR_INVALID_PARAMETER (CAMERASRC_WARING | CAMSRC_IN_PARAMETER) +#define CAMERASRC_WAR_INVALID_HANDLE (CAMERASRC_WARING | CAMSRC_IN_HANDLE) +#define CAMERASRC_WAR_INVALID_VALUE (CAMERASRC_WARING | CAMSRC_IN_VALUE) +#define CAMERASRC_WAR_INVALID_MODE (CAMERASRC_WARING | CAMSRC_IN_MODE) +#define CAMERASRC_WAR_INVALID_FORMAT (CAMERASRC_WARING | CAMSRC_IN_FORMAT) +#define CAMERASRC_WAR_INVALID_CHANNEL (CAMERASRC_WARING | CAMSRC_IN_CHANNEL) +#define CAMERASRC_WAR_INVALID_SAMPLERATE (CAMERASRC_WARING | CAMSRC_IN_SAMPLERATE) +#define CAMERASRC_WAR_INVALID_LAYER (CAMERASRC_WARING | CAMSRC_IN_LAYER) +#define CAMERASRC_WAR_INVALID_ROTATE (CAMERASRC_WARING | CAMSRC_IN_ROTATE) +#define CAMERASRC_WAR_NULL_POINTER (CAMERASRC_WARING | CAMSRC_IN_POINTER) +#define CAMERASRC_WAR_UNDERRUN (CAMERASRC_WARING | CAMSRC_IN_UNDERRUN) +#define CAMERASRC_WAR_OVERRUN (CAMERASRC_WARING | CAMSRC_IN_OVERRUN) +#define CAMERASRC_WAR_RANGE_OVER (CAMERASRC_WARING | CAMSRC_IN_RANGE_OVER) +#define CAMERASRC_WAR_RANGE_UNDER (CAMERASRC_WARING | CAMSRC_IN_RANGE_UNDER) +#define CAMERASRC_WAR_ALLOCATION (CAMERASRC_WARING | CAMSRC_IN_ALLOC) +#define CAMERASRC_WAR_INTERNAL (CAMERASRC_WARING | CAMSRC_IN_INTERNAL) +/*Extra warning for camera*/ +#define CAMERASRC_WAR_IO_CONTROL (CAMERASRC_WARING | CAMSRC_IN_IO_CONTROL) +#define CAMERASRC_WAR_DEVICE_OPEN (CAMERASRC_WARING | CAMSRC_IN_DEVICE_OPEN) +#define CAMERASRC_WAR_DEVICE_BUSY (CAMERASRC_WARING | CAMSRC_IN_DEVICE_BUSY) +#define CAMERASRC_WAR_DEVICE_NOT_FOUND (CAMERASRC_WARING | CAMSRC_IN_DEVICE_NOT_FOUND) +#define CAMERASRC_WAR_DEVICE_UNAVAILABLE (CAMERASRC_WARING | CAMSRC_IN_DEVICE_REGISTER_TROUBLE) +#define CAMERASRC_WAR_DEVICE_WAIT_TIMEOUT (CAMERASRC_WARING | CAMSRC_IN_WAIT_RES) +#define CAMERASRC_WAR_DEVICE_NOT_SUPPORT (CAMERASRC_WARING | CAMSRC_IN_SUPPORT) +#define CAMERASRC_WAR_INVALID_STATE_TRANSITION (CAMERASRC_WARING | CAMSRC_IN_STATE) + +/** + * CAMERASRC_ERROR + */ +#define CAMERASRC_ERR_INVALID_PARAMETER (CAMERASRC_ERROR | CAMSRC_IN_PARAMETER) +#define CAMERASRC_ERR_INVALID_HANDLE (CAMERASRC_ERROR | CAMSRC_IN_HANDLE) +#define CAMERASRC_ERR_INVALID_VALUE (CAMERASRC_ERROR | CAMSRC_IN_VALUE) +#define CAMERASRC_ERR_INVALID_MODE (CAMERASRC_ERROR | CAMSRC_IN_MODE) +#define CAMERASRC_ERR_INVALID_FORMAT (CAMERASRC_ERROR | CAMSRC_IN_FORMAT) +#define CAMERASRC_ERR_INVALID_CHANNEL (CAMERASRC_ERROR | CAMSRC_IN_CHANNEL) +#define CAMERASRC_ERR_INVALID_SAMPLERATE (CAMERASRC_ERROR | CAMSRC_IN_SAMPLERATE) +#define CAMERASRC_ERR_INVALID_LAYER (CAMERASRC_ERROR | CAMSRC_IN_LAYER) +#define CAMERASRC_ERR_INVALID_ROTATE (CAMERASRC_ERROR | CAMSRC_IN_ROTATE) +#define CAMERASRC_ERR_NULL_POINTER (CAMERASRC_ERROR | CAMSRC_IN_POINTER) +#define CAMERASRC_ERR_UNDERRUN (CAMERASRC_ERROR | CAMSRC_IN_UNDERRUN) +#define CAMERASRC_ERR_OVERRUN (CAMERASRC_ERROR | CAMSRC_IN_OVERRUN) +#define CAMERASRC_ERR_RANGE_OVER (CAMERASRC_ERROR | CAMSRC_IN_RANGE_OVER) +#define CAMERASRC_ERR_RANGE_UNDER (CAMERASRC_ERROR | CAMSRC_IN_RANGE_UNDER) +#define CAMERASRC_ERR_ALLOCATION (CAMERASRC_ERROR | CAMSRC_IN_ALLOC) +#define CAMERASRC_ERR_INTERNAL (CAMERASRC_ERROR | CAMSRC_IN_INTERNAL) +#define CAMERASRC_ERR_UNKNOWN (CAMERASRC_ERROR | CAMSRC_IN_UNKNOWN) /**< unknown error */ +/*Extra warning for camera*/ +#define CAMERASRC_ERR_IO_CONTROL (CAMERASRC_ERROR | CAMSRC_IN_IO_CONTROL) +#define CAMERASRC_ERR_DEVICE_OPEN (CAMERASRC_ERROR | CAMSRC_IN_DEVICE_OPEN) +#define CAMERASRC_ERR_DEVICE_BUSY (CAMERASRC_ERROR | CAMSRC_IN_DEVICE_BUSY) +#define CAMERASRC_ERR_DEVICE_NOT_FOUND (CAMERASRC_ERROR | CAMSRC_IN_DEVICE_NOT_FOUND) +#define CAMERASRC_ERR_DEVICE_UNAVAILABLE (CAMERASRC_ERROR | CAMSRC_IN_DEVICE_REGISTER_TROUBLE) +#define CAMERASRC_ERR_DEVICE_WAIT_TIMEOUT (CAMERASRC_ERROR | CAMSRC_IN_WAIT_RES) +#define CAMERASRC_ERR_DEVICE_NOT_SUPPORT (CAMERASRC_ERROR | CAMSRC_IN_SUPPORT) +#define CAMERASRC_ERR_INVALID_STATE (CAMERASRC_ERROR | CAMSRC_IN_STATE) +#define CAMERASRC_ERR_PRIVILEGE (CAMERASRC_ERROR | CAMSRC_IN_PRIVILEGE) +#define CAMERASRC_ERR_SECURITY_SERVICE (CAMERASRC_ERROR | CAMSRC_IN_SECURITY_SERVICE) + + +#define CAMERASRC_IS_FAIL(_A_) (CAMERASRC_ERROR & (_A_)) + +#ifdef __cplusplus +} +#endif + +#endif /* __CAMERASRC_ERROR_H__ */ diff --git a/camerasrc/src/include/camerasrc-internal.h b/camerasrc/src/include/camerasrc-internal.h new file mode 100644 index 0000000..6b97589 --- /dev/null +++ b/camerasrc/src/include/camerasrc-internal.h @@ -0,0 +1,247 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __CAMERASRC_INTERNAL_H__ +#define __CAMERASRC_INTERNAL_H__ + +#include "camerasrc-common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Video 4 linux control ID definitions (Extended by kernel team) + * extended pixel format for V4l2 + */ + + +/** + * Miscellaneous camera-dependent definitions + */ +#define CAMERASRC_DEV_INDEX_MAX 2 +#define CAMERASRC_AF_TOTALTIME 5000000 +#define CAMERASRC_AF_INTERVAL 20000 +#define CAMERASRC_AF_STOP_INTERVAL 10000 +#define CAMERASRC_MEGA_DEV_NAME "/dev/video0" +#define CAMERASRC_VGA_DEV_NAME "/dev/video0" +#define CAMERASRC_JPG_STILL_H_SYNC 2048 +#define CAMERASRC_JPG_STILL_V_SYNC 2048 +#define CAMERASRC_JPG_STILL_SKIP_FRAME 0 +#define CAMERASRC_RAW_STILL_THUMBNAIL_OFFSET 1689600 +#define CAMERASRC_JPG_STILL_THUMBNAIL_OFFSET 0x1bd000 +#define CAMERASRC_MAX_MAIN_JPEG_SIZE CAMERASRC_JPG_STILL_H_SYNC * CAMERASRC_JPG_STILL_V_SYNC /* 0x1bd000 */ +#define CAMERASRC_MAX_THM_JPEG_SIZE 0 +#define CAMERASRC_PREVIEW_BUFFER_NUM 6 +#define CAMERASRC_VIDEO_BUFFER_NUM 7 +#define CAMERASRC_STILL_BUFFER_NUM 1 +#define CAMERASRC_YUV_THMBNL_SIZE 320 * 240 * 2 +#define CAMERASRC_TIMEOUT_CRITICAL_VALUE 5000 +#define CAMERASRC_PRE_AF_INTERVAL 0 +#define CAMERASRC_REGISTER_SET_RETRY_NO 200 +#define CAMERASRC_V4L2_PREVIEW_PIX_FMT_DEFAULT V4L2_PIX_FMT_YUYV +#define CAMERASRC_V4L2_JPEG_CAPTURE_PIX_FMT_DEFAULT V4L2_PIX_FMT_JPEG +#define CAMERASRC_V4L2_JPG_YUV_CAPTURE_PIX_FMT_DEFAULT V4L2_PIX_FMT_MJPEG +#define CAMERASRC_V4L2_RGB_CAPTURE_PIX_FMT_DEFAULT V4L2_PIX_FMT_RGB565 + +#define CAMERASRC_SYNC_KEY_PATH "FUJITSU_M5MOLS" +#define CAMERASRC_SYNC_KEY_PREFIX 0x55 + +#define CAMERASRC_AF_DELAY_AFTER_SET_AF_AREA 350 /* msec */ + +#define _CAMERASRC_EXIF_COMP_CONF (0x00000000) | (0x00000001) | (0x00000002 << 8) | (0x00000003 << 16) /* Y Cb Cr - */ +#define _CAMERASRC_EXIF_COLORSPACE 0x00000001 +#define _CAMERASRC_EXIF_FOCAL_LEN_NUM 454 +#define _CAMERASRC_EXIF_FOCAL_LEN_DEN 100 +#define _CAMERASRC_EXIF_F_NUM_NUM 28 +#define _CAMERASRC_EXIF_F_NUM_DEN 10 +#define _CAMERASRC_EXIF_MAX_F_NUM_NUM_IN_APEX 3 + +#define _CAMERASRC_CMD_SUPPORT_EMBED_EXIF_DEF 0 + +/* + * |------------------------------------------------------------| + * | | Primary | Secondary | Extension| Unknown | + * |------------------------------------------------------------| + * | Index | 2 | 1 | 0 | -1 | + * |------------------------------------------------------------| + * + * |------------------------------------------------------------| + * | | 0 | 1 | 2 | 3 | + * |------------------------------------------------------------| + * | ID | Unknown | Secondary | Primary | Extension | + * |------------------------------------------------------------| + */ + +static int _camerasrc_dev_index[CAMERASRC_DEV_ID_NUM][CAMERASRC_DEV_RECOG_NUM] = +{ + {CAMERASRC_DEV_ID_SECONDARY, 0}, + {CAMERASRC_DEV_ID_PRIMARY, 1}, + {CAMERASRC_DEV_ID_EXTENSION, 2}, + {CAMERASRC_DEV_ID_UNKNOWN, -1}, +}; + + +/** + * preset size index + * |-------------------------------------------------------------------------| + * | | | YUV422P | YUV420P | SRGGB8 | SRGGB10 | + * |-------------------------------------------------------------------------| + * | | Highquality | O | X | X | X | + * | RAW |-----------------------------------------------------------| + * | | Normal | O | X | X | X | + * |-------------------------------------------------------------------------| + * | | Highquality | X | X | O | X | + * | JPEG |-----------------------------------------------------------| + * | | Normal | X | X | X | X | + * |-------------------------------------------------------------------------| + */ +static char _camerasrc_match_col_to_pix[CAMERASRC_DEV_ID_EXTENSION][CAMERASRC_COL_NUM][CAMERASRC_PIX_NUM][CAMERASRC_QUALITY_NUM] = +{ + {/*SECONDARY*/ + /*422P, 420P, SRGGB8, SRGGB10*/ + {{0x1,0x0}, {0x0,0x0}, {0x0,0x0}, {0x0,0x0}}, /**< RAW */ + {{0x0,0x0}, {0x0,0x0}, {0x0,0x0}, {0x0,0x0}}, /**< JPG */ + }, + {/*PRIMARY*/ + /*422P, 420P, SRGGB8, SRGGB10 */ + {{0x1,0x0}, {0x0,0x0}, {0x0,0x0}, {0x0,0x0}}, /**< RAW */ + {{0x0,0x0}, {0x0,0x0}, {0x0,0x1}, {0x0,0x1}}, /**< JPG */ + }, +}; /* {Normal quality, High quality} */ + +/* + * |--------------------------------------------------------------------------------| + * | | Support | MAX value | MIN value | CID | + * |--------------------------------------------------------------------------------| + * | Brightness | 1 | 8 | 0 | V4L2_CID_BRIGHTNESS | + * |--------------------------------------------------------------------------------| + * | Contrast | 1 | 9 | 0 | -1 | + * |--------------------------------------------------------------------------------| + * | Digital zoom | 1 | 100 | 0 | V4L2_CID_BASE+29 | + * |--------------------------------------------------------------------------------| + * | Optical zoom | 0 | -1 | -1 | -1 | + * |--------------------------------------------------------------------------------| + * | White balance | 1 | 9 | 0 | V4L2_CID_DO_WHITE_BALANCE | + * |--------------------------------------------------------------------------------| + * | Color tone | 1 | 14 | 0 | V4L2_CID_BASE+28 | + * |--------------------------------------------------------------------------------| + * | Program mode | 0 | -1 | -1 | -1 | + * |--------------------------------------------------------------------------------| + * | Flip | 1 | 1 | 0 | V4L2_CID_FLIP | + * |--------------------------------------------------------------------------------| + * | Flash | 1 | 2 | 0 | V4L2_CID_BASE+32 | + * |--------------------------------------------------------------------------------| + */ + +/* CUSTOM V4L2 CONTROL ID DEFINITIONS */ + +#define CAM_AF_STATUS_ONGOING 0 +#define CAM_AF_STATUS_FOCUSED 1 +#define CAM_AF_STATUS_FAILED 2 + +enum { + CAMERASRC_SENSOR_AF_STATUS_ONGOING = CAM_AF_STATUS_ONGOING, + CAMERASRC_SENSOR_AF_STATUS_FOCUSED = CAM_AF_STATUS_FOCUSED, + CAMERASRC_SENSOR_AF_STATUS_FAILED = CAM_AF_STATUS_FAILED +}; + +/* FACE_DETECTION CMD & STATUS */ +#define V4L2_CID_FACE_DETECTION (V4L2_CID_PRIVATE_BASE + 38) + +#define CAM_FACE_DETECTION_OFF 0 +#define CAM_FACE_DETECTION_ON 1 + +/* ESD INTERRUPT CHECK */ +/*To read interrupt status of "Data output stop from sensor"*/ +#define V4L2_CID_ESD_INT (V4L2_CID_PRIVATE_BASE + 40) + +#define CAM_ESD_INT_NORMAL 0 +#define CAM_ESD_INT_TROUBLE 1 + + +/* CUSTOM V4L2 CONTROL ID DEFINITIONS (END) */ +static int _camerasrc_ctrl_list[CAMERASRC_DEV_ID_EXTENSION][CAMERASRC_CTRL_NUM][CAMERASRC_CTRL_PROPERTY_NUM] = +{ /* { SUPPORT, MAX_VALUE, MIN_VALUE, CID, CURRENT_VALUE } */ + { /* Primary camera */ + {-1, 4, -4, V4L2_CID_EXPOSURE, 0}, /* Brightness */ + {-1, 3, -3, V4L2_CID_CONTRAST, 0}, /* Contrast */ + {-1, 30, 0, V4L2_CID_CAMERA_ZOOM, 0}, /* Digital zoom */ + {0, -1, -1, -1, -1}, /* Optical zoom */ + {-1, CAMERASRC_WHITEBALANCE_HORIZON, CAMERASRC_WHITEBALANCE_AUTO, V4L2_CID_WHITE_BALANCE_PRESET, CAMERASRC_WHITEBALANCE_AUTO}, /* White balance */ + {-1, CAMERASRC_COLORTONE_ANTIQUE, CAMERASRC_COLORTONE_NONE, V4L2_CID_COLORFX, CAMERASRC_COLORTONE_NONE}, /* Colortone */ + {-1, CAMERASRC_PROGRAM_MODE_BACK_LIGHT, CAMERASRC_PROGRAM_MODE_NORMAL, V4L2_CID_CAMERA_SCENE_MODE, CAMERASRC_PROGRAM_MODE_NORMAL}, /* program mode */ + {0, -1, -1, -1, -1}, /* Flip. V4L2_CID_VFLIP/HFLIP */ + {0, -1, -1, -1, -1}, /* PARTCOLOR_SRC */ + {0, -1, -1, -1, -1}, /* PARTCOLOR_DST */ + {0, -1, -1, -1, -1}, /* PARTCOLOR_MODE */ + {-1, 3, 0, V4L2_CID_CAM_STABILIZE, 0}, /* ANTI_HANDSHAKE */ + {-1, 2, 0, V4L2_CID_CAM_DR, 0}, /* WIDE_DYNAMIC_RANGE */ + {-1, 3, -3, V4L2_CID_SATURATION, 0}, /* SATURATION */ + {-1, 3, -3, V4L2_CID_SHARPNESS, 0}, /* SHARPNESS */ + {-1, -1, -1, V4L2_CID_CAMERA_ISO, -1}, /* ISO */ + {-1, -1, -1, V4L2_CID_CAMERA_METERING, -1}, /* PHOTOMETRY */ + }, + { /* Secondary camera */ + {0, -1, -1, V4L2_CID_CAMERA_BRIGHTNESS, -1}, /* Brightness */ + {0, -1, -1, V4L2_CID_CONTRAST, -1}, /* Contrast */ + {0, -1, -1, V4L2_CID_ZOOM_ABSOLUTE, -1}, /* Digital zoom */ + {0, -1, -1, -1, -1}, /* Optical zoom */ + {0, -1, -1, V4L2_CID_WHITE_BALANCE_PRESET, -1},/* White balance */ + {0, -1, -1, V4L2_CID_COLORFX, -1}, /* Colortone */ + {0, -1, -1, V4L2_CID_CAMERA_SCENE_MODE, -1}, /* program mode */ + {0, -1, -1, -1, -1}, /* Flip */ + {0, -1, -1, -1, -1}, /* PARTCOLOR_SRC */ + {0, -1, -1, -1, -1}, /* PARTCOLOR_DST */ + {0, -1, -1, -1, -1}, /* PARTCOLOR_MODE */ + {0, -1, -1, V4L2_CID_CAM_STABILIZE, -1}, /* ANTI_HANDSHAKE */ + {0, -1, -1, V4L2_CID_CAM_DR, -1}, /* WIDE_DYNAMIC_RANGE */ + {0, -1, -1, V4L2_CID_SATURATION, -1}, /* SATURATION */ + {0, -1, -1, V4L2_CID_SHARPNESS, -1}, /* SHARPNESS */ + {0, -1, -1, V4L2_CID_CAMERA_ISO, -1}, /* ISO */ + {0, -1, -1, V4L2_CID_CAMERA_METERING, -1}, /* PHOTOMETRY */ + }, +}; + +static char _camerasrc_misc_func_list[CAMERASRC_DEV_ID_EXTENSION][CAMERASRC_OP_REGISTER_VALUE][CAMERASRC_COL_NUM][CAMERASRC_MISC_FUNC_NUM] = +{ + {/*Primary*/ + /*Raw, JPG*/ + {{0x0,0x1}, {0x0,0x0}}, /*Preview*/ + {{0x0,0x1}, {0x0,0x0}}, /*Capture*/ + {{0x0,0x1}, {0x0,0x0}}, /*Video*/ + }, + {/*Secondary*/ + {{0x0,0x0}, {0x0,0x0}}, /*Preview*/ + {{0x0,0x0}, {0x0,0x0}}, /*Capture*/ + {{0x0,0x0}, {0x0,0x0}}, /*Video*/ + }/*{{Signal,Skip}, {Signal,Skip}}*/ +}; + +static int _camerasrc_af_support[CAMERASRC_DEV_ID_NUM] = {1, 0, 0, 0}; + +#ifdef __cplusplus +} +#endif + +#endif /*__CAMERASRC_INTERNAL_H__*/ diff --git a/camerasrc/src/include/camerasrc.h b/camerasrc/src/include/camerasrc.h new file mode 100644 index 0000000..2917f29 --- /dev/null +++ b/camerasrc/src/include/camerasrc.h @@ -0,0 +1,1488 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __CAMERASRC_H__ +#define __CAMERASRC_H__ + +#include <stdint.h> /* to use uint64_t */ +#include <camerasrc-error.h> +#include <mm_ta.h> +#ifdef __cplusplus +extern "C" { +#endif + +/* GENERAL DEFINITIONS */ +/** + * Type definition of av camera src handle. + */ +typedef void *camsrc_handle_t; + + +/* ENUMERATION DEFINITIONS */ +/*! @enum camerasrc_state_t + * @brief Enumeration type for state transition + */ +typedef enum { + CAMERASRC_STATE_NONE = 0, + CAMERASRC_STATE_CREATED, + CAMERASRC_STATE_REALIZED, + CAMERASRC_STATE_READY, + CAMERASRC_STATE_PREVIEW, + CAMERASRC_STATE_STILL, + CAMERASRC_STATE_VIDEO, + CAMERASRC_STATE_UNREALIZED, + CAMERASRC_STATE_DESTROYED, + CAMERASRC_STATE_AF_IN_PROGRESS, +}camerasrc_state_t; + +/*! @enum camerasrc_dev_id_t + * @brief Enumeration type for camera device ID + * + * Devices will be managed by this IDs. (Independent with device index of V4L2) + */ +typedef enum { + CAMERASRC_DEV_ID_PRIMARY, /**< Higher resolution camera*/ + CAMERASRC_DEV_ID_SECONDARY, /**< Lower resolution camera*/ + CAMERASRC_DEV_ID_EXTENSION, /**< reserved for extension*/ + CAMERASRC_DEV_ID_UNKNOWN, /**< reserved for extension*/ + CAMERASRC_DEV_ID_NUM, /**< Number of IDs*/ +}camerasrc_dev_id_t; + +typedef enum { + CAMERASRC_COLOR_VIOLET = 0, + CAMERASRC_COLOR_PURPLE, + CAMERASRC_COLOR_MAGENTA_1, + CAMERASRC_COLOR_MAGENTA_2, + CAMERASRC_COLOR_RED_1, + CAMERASRC_COLOR_RED_2, + CAMERASRC_COLOR_BROWN, + CAMERASRC_COLOR_YELLOW, + CAMERASRC_COLOR_GREEN_1, + CAMERASRC_COLOR_GREEN_2, + CAMERASRC_COLOR_GREEN_3, + CAMERASRC_COLOR_GREEN_4, + CAMERASRC_COLOR_COBALT_BLUE, + CAMERASRC_COLOR_CYAN, + CAMERASRC_COLOR_BLUE_1, + CAMERASRC_COLOR_BLUE_2, + CAMERASRC_COLOR_GRAY, + CAMERASRC_COLOR_NUM, +}camerasrc_color_t; + +typedef enum { + CAMERASRC_PARTCOLOR_MODE_NONE = 0, + CAMERASRC_PARTCOLOR_MODE_SWAP, + CAMERASRC_PARTCOLOR_MODE_ACCENT, + CAMERASRC_PARTCOLOR_MODE_NUM, +}camerasrc_partcolor_mode_t; + +/*! @enum camerasrc_ctrl_t + * @brief Enumeration type for camera controls + * + * Special control entries for camera effects + * + * @remark Strobo can be controlled by this entry and ::camerasrc_set_strobo_status + */ +typedef enum { + CAMERASRC_CTRL_BRIGHTNESS = 0, /**< Brightness control entry*/ + CAMERASRC_CTRL_CONTRAST, /**< Contrast control entry*/ + CAMERASRC_CTRL_DIGITAL_ZOOM, /**< Digital zoom control entry*/ + CAMERASRC_CTRL_OPTICAL_ZOOM, /**< Optical zoom control entry*/ + CAMERASRC_CTRL_WHITE_BALANCE, /**< White balance control entry*/ + CAMERASRC_CTRL_COLOR_TONE, /**< Color tone control entry*/ + CAMERASRC_CTRL_PROGRAM_MODE, /**< Program mode control entry*/ + CAMERASRC_CTRL_FLIP, /**< Flip control entry*/ + CAMERASRC_CTRL_PARTCOLOR_SRC, /**< Partcolor effect source */ + CAMERASRC_CTRL_PARTCOLOR_DST, /**< Partcolor effect destination */ + CAMERASRC_CTRL_PARTCOLOR_MODE, /**< Partcolor effect mode */ + CAMERASRC_CTRL_ANTI_HANDSHAKE, /**< Anti-handshake control, 0:OFF / 1:ON / 2:AUTO / 3:MOVIE */ + CAMERASRC_CTRL_WIDE_DYNAMIC_RANGE, /**< wide dynamic control, 0:OFF / 1:ON / 2:AUTO */ + CAMERASRC_CTRL_SATURATION, /**< Saturation value control */ + CAMERASRC_CTRL_SHARPNESS, /**< Sharpness value control */ + CAMERASRC_CTRL_ISO, /**< Sensor sensitivity*/ + CAMERASRC_CTRL_PHOTOMETRY, /**< Exposure mode*/ + CAMERASRC_CTRL_NUM, /**< Number of Controls*/ +}camerasrc_ctrl_t; + +/*! @enum camerasrc_af_mode_t + * @brief AF operation mode + */ +typedef enum { + CAMERASRC_AF_MODE_AUTO = 0, /**< Auto Focus */ + CAMERASRC_AF_MODE_MANUAL, /**< Manual Focus */ + CAMERASRC_AF_MODE_PAN, /**< Pan Focus */ + CAMERASRC_AF_MODE_TOUCH_AUTO, /**< Touch Auto Focus */ + CAMERASRC_AF_MODE_CONTINUOUS, /**< Continuous Focus */ + CAMERASRC_AF_MODE_NUM, /**< Number of AF modes */ +}camerasrc_af_mode_t; + +/*! @enum camerasrc_af_scan_range_t + * @brief AF scan range + * AF scan range + */ +typedef enum { + CAMERASRC_AF_RANGE_NORMAL = 0, /**< Scan autofocus in normal range */ + CAMERASRC_AF_RANGE_MACRO, /**< Scan autofocus in macro range(close distance) */ + CAMERASRC_AF_RANGE_FULL, /**< Scan autofocus in full range(all range scan, limited by dev spec) */ + CAMERASRC_AF_RANGE_NUM, /**< Number of AF range types */ +}camerasrc_af_scan_range_t; + +/*! @enum camerasrc_resol_name_t + * @brief Enumeration type of resolution settings based on traditional resolution name + * Means pixel order of contents. + * @remark In the Grandprix, only YUV422P & RGGB8 is used + */ +typedef enum { + CAMERASRC_RESOL_QQCIF = 0, /**< 88 x 72 */ + CAMERASRC_RESOL_QQVGA, /**< 160 x 120 */ + CAMERASRC_RESOL_QCIF, /**< 176 x 144 */ + CAMERASRC_RESOL_QVGA, /**< 320 x 240 */ + CAMERASRC_RESOL_CIF, /**< 352 x 288 */ + CAMERASRC_RESOL_VGA, /**< 640 x 480 */ + CAMERASRC_RESOL_WVGA, /**< 800 x 480 */ + CAMERASRC_RESOL_SVGA, /**< 800 x 600 */ + CAMERASRC_RESOL_WSXGA, /**< 1280 x 960 (1M) */ + CAMERASRC_RESOL_UXGA, /**< 1600 x 1200 (2M) */ + CAMERASRC_RESOL_QXGA, /**< 2048 x 1536 (3M) */ + CAMERASRC_RESOL_WQSXGA, /**< 2560 x 1920 (5M) */ + CAMERASRC_RESOL_720P, /**< 1280 x 720 (720P) */ + CAMERASRC_RESOL_WQVGA, /**< 400 x 240 */ + CAMERASRC_RESOL_RQVGA, /**< 240 x 320 */ + CAMERASRC_RESOL_RWQVGA, /**< 240 x 400 */ + CAMERASRC_RESOL_QVGA_60FPS, /**< 320 x 240 60FPS(Slow motion I) */ + CAMERASRC_RESOL_QVGA_120FPS, /**< 320 x 240 60FPS(Slow motion II) */ + CAMERASRC_RESOL_NUM, +}camerasrc_resol_name_t; + +/*! @enum camerasrc_pix_format_t + * @brief Means order of pixel of contents + * Means pixel order of contents. + * @remark In the Grandprix, only YUV422P & RGGB8 is used + */ +typedef enum { + CAMERASRC_PIX_NONE = -1, /**< Default value or Not supported */ + CAMERASRC_PIX_YUV422P = 0, /**< Pixel format like YYYYYYYYUUUUVVVV*/ + CAMERASRC_PIX_YUV420P, /**< Pixel format like YYYYYYYYUUVV*/ + CAMERASRC_PIX_YUV420, /**< Pixel format like YYYYYYYYUVUV*/ + CAMERASRC_PIX_SN12, /**< YUV420 (interleaved, non-linear) */ + CAMERASRC_PIX_ST12, /**< YUV420 (interleaved, tiled, non-linear) */ + CAMERASRC_PIX_YUY2, /**< YUV 4:2:2 as for UYVY but with different component ordering within the u_int32 macropixel */ + CAMERASRC_PIX_RGGB8, /**< Raw RGB Pixel format like CCD order, a pixel consists of 8 bits, Actually means JPEG + JPEG image output */ + CAMERASRC_PIX_RGGB10, /**< Raw RGB Pixel format like CCD order, a pixel consists of 10 bits, Actually means JPEG + YUV image output */ + CAMERASRC_PIX_RGB565, /**< Raw RGB Pixel format like CCD order, a pixel consists of 10 bits, Actually means JPEG + YUV image output */ + CAMERASRC_PIX_UYVY, /**< YUV 4:2:2 */ + CAMERASRC_PIX_NV12, /**< YUV 4:2:0, 8-bit Y plane followed by an interleaved U/V plane with 2x2 subsampling */ + CAMERASRC_PIX_INTERLEAVED, /**< JPEG/YUYV interleaved data format for zero shutter lag */ + CAMERASRC_PIX_NUM, /**< Number of pixel formats*/ +}camerasrc_pix_format_t; + +/*! @enum camerasrc_colorspace_t + * @brief Means stored order or compressed status of image. + * Means stored order or compressed status of image. supplements of camerasrc_pix_format_t + * + * @note RAW means RGB/YUV pixel data, JPEG means compressed JPG file with marker information(header) + */ +typedef enum { + CAMERASRC_COL_NONE = -1, /**< Default value or Not supported */ + CAMERASRC_COL_RAW, /**< Non-compressed RGB/YUV pixel data*/ + CAMERASRC_COL_JPEG, /**< Compressed jpg data*/ + CAMERASRC_COL_NUM, /**< Number of colorspace data*/ +}camerasrc_colorspace_t; + +/*! @enum camerasrc_auto_focus_status_t + * @brief AF status + * AF status + */ +typedef enum { + CAMERASRC_AUTO_FOCUS_STATUS_RELEASED, /**< AF status released.*/ + CAMERASRC_AUTO_FOCUS_STATUS_ONGOING, /**< AF in progress*/ + CAMERASRC_AUTO_FOCUS_STATUS_NUM, /**< Number of AF status*/ +}camerasrc_auto_focus_status_t; + +/*! @enum camerasrc_auto_focus_cmd_t + * @brief AF status + * AF status + */ +typedef enum { + CAMERASRC_AUTO_FOCUS_CMD_NULL, /**< Null command.*/ + CAMERASRC_AUTO_FOCUS_CMD_START, /**< Start AF.*/ + CAMERASRC_AUTO_FOCUS_CMD_STOP, /**< Stop AF.*/ + CAMERASRC_AUTO_FOCUS_CMD_KILL, /**< Kill AF thread.*/ + CAMERASRC_AUTO_FOCUS_CMD_NUM, /**< Number of AF command*/ +}camerasrc_auto_focus_cmd_t; + +/*! @enum camerasrc_auto_focus_result_t + * @brief AF status + * AF status + */ +typedef enum { + CAMERASRC_AUTO_FOCUS_RESULT_FOCUSED = 2, /**< Focused.*/ + CAMERASRC_AUTO_FOCUS_RESULT_FAILED, /**< AF failed.*/ + CAMERASRC_AUTO_FOCUS_RESULT_NUM, /**< Number of AF result*/ +}camerasrc_auto_focus_result_t; + +/*! @enum camerasrc_ae_lock_t + * @brief + */ +typedef enum { + CAMERASRC_AE_LOCK = 0, + CAMERASRC_AE_UNLOCK, + CAMERASRC_AE_NUM, +}camerasrc_ae_lock_t; + +/*! @enum camerasrc_io_method_t + * @brief + */ +typedef enum { + CAMERASRC_IO_METHOD_READ= 0, + CAMERASRC_IO_METHOD_MMAP, + CAMERASRC_IO_METHOD_USRPTR, + CAMERASRC_IO_METHOD_NUM, +}camerasrc_io_method_t; + +/*! @enum camerasrc_buffer_queued_status + * @brief + */ +typedef enum { + CAMERASRC_BUFFER_QUEUED = 0, + CAMERASRC_BUFFER_DEQUEUED = 1, +}camerasrc_buffer_queued_status; + + +/* STRUCTURE DEFINITIONS */ + +typedef struct _camerasrc_rect_t { + int x; + int y; + int width; + int height; +} camerasrc_rect_t; + +/*! @struct camsrc_frac_t + * @brief Time per frame or frame per second will be expressed by this structure + * Time per frame or frame per second will be expressed by this structure + */ +typedef struct _camerasrc_frac_t { + int numerator; /**< Upper number of fraction*/ + int denominator; /**< Lower number of fraction*/ +} camerasrc_frac_t; + +/*! @struct camerasrc_buffer_t + * @brief data buffer + * Image data buffer + */ +typedef struct _camerasrc_buffer_t { + unsigned int length; /**< Size of stored data*/ + unsigned char* start; /**< Start address of data*/ + camerasrc_buffer_queued_status queued_status; /**< Queued or Dequeued status */ +} camerasrc_buffer_t; + +/*! @struct camerasrc_usr_buf_t + * @brief data buffer set to present usrptr buffer to camsrctem + * Image data buffer set + */ +typedef struct { + camerasrc_buffer_t* present_buffer; + unsigned int num_buffer; +} camerasrc_usr_buf_t; + +/*! @struct camerasrc_dimension_t + * @brief For non-regular size resolution + * width and height can be set independently + */ +typedef struct _camerasrc_dimension_t { + int width; + int height; +} camerasrc_dimension_t; + +/*! @union camerasrc_size_t + * @brief Size can be expressed by resolution name(predefined) and dimension(x, y) + */ +typedef union _camerasrc_size_t { + camerasrc_resol_name_t res; /**< Predefined resolution name */ + camerasrc_dimension_t dim; /**< Dimensional expression */ +} camerasrc_size_t; + +/*! @struct camerasrc_format_t + * @brief Format description structure + * in/output format description structure just like v4l2_format + */ +typedef struct _camerasrc_format_t { + camerasrc_size_t img_size; + camerasrc_size_t thumb_size; /**< Thumbnail size. Only effective with CAMERASRC_PIX_RGGB8 or CAMERASRC_PIX_RGGB10 */ + camerasrc_pix_format_t pix_format; /**< pixel order format*/ + int num_planes; /**< bytes per a line*/ + int bytesperline; /**< bytes per a line*/ + int sizeimage; /**< size of whole image*/ + camerasrc_colorspace_t colorspace; /**< stored status of image*/ + unsigned int quality; /**< jpeg compress ratio*/ + unsigned int is_highquality_mode; /**< picture quality is high or normal */ + int rotation; /**< Rotation angle of camera input */ +} camerasrc_format_t; + +typedef struct _camerasrc_ctrl_query_t { + int support; /**<1: support, 0: Not support, -1: extra support(Non v4l2)*/ + int max; /**<Integer max value(includes enums)*/ + int min; /**<Integer min value(includes enums)*/ +}camerasrc_ctrl_query_t; + +typedef struct _camerasrc_exif_t { + /* Dynamic value */ + unsigned int exposure_time_numerator; /**< Exposure time, given in seconds */ + unsigned int exposure_time_denominator; + int shutter_speed_numerator; /**< Shutter speed, given in APEX(Additive System Photographic Exposure) */ + int shutter_speed_denominator; + int brigtness_numerator; /**< Value of brightness, before firing flash, given in APEX value */ + int brightness_denominator; + unsigned short int iso; /**< Sensitivity value of sensor */ + unsigned short int flash; /**< Whether flash is fired(1) or not(0) */ + int metering_mode; /**< metering mode in EXIF 2.2 */ + int exif_image_width; /**< Size of image */ + int exif_image_height; + int exposure_bias_in_APEX; /**< Exposure bias in APEX standard */ + int software_used; /**< Firmware S/W version */ + int focal_len_numerator; /**< Lens focal length (f = 4.5mm) */ + int focal_len_denominator; + int aperture_f_num_numerator; /**< Aperture value (f_num = 2.8) */ + int aperture_f_num_denominator; + int aperture_in_APEX; /**< Aperture value in APEX standard */ + int max_lens_aperture_in_APEX; /**< Max aperture value in APEX standard */ + + /* Fixed value */ + int component_configuration; /**< color components arrangement (YCbCr = 1230) */ + int colorspace; /**< colorspace information (sRGB=1) */ +}camerasrc_exif_t; + +typedef struct _camerasrc_frame_data_t { + int index; + unsigned int phyAddrY; + unsigned int phyAddrCbCr; + unsigned int virAddrY; + unsigned int virAddrCbCr; +}camerasrc_frame_data_t; + +/* JPEG/YUV interleaved data */ +#define INTERLEAVED_JPEG_MAX_SIZE (1024*1024*6) /* 6 Mbyte */ + +/* Fixed focal length and aperture f-number */ +#define CAMERASRC_PRIMARY_FOCAL_LEGNTH_NUM 397 +#define CAMERASRC_PRIMARY_FOCAL_LEGNTH_DENOM 100 +#define CAMERASRC_PRIMARY_F_NUMBER_NUM 265 +#define CAMERASRC_PRIMARY_F_NUMBER_DENOM 100 +#define CAMERASRC_SECONDARY_FOCAL_LEGNTH_NUM 273 +#define CAMERASRC_SECONDARY_FOCAL_LEGNTH_DENOM 100 +#define CAMERASRC_SECONDARY_F_NUMBER_NUM 28 +#define CAMERASRC_SECONDARY_F_NUMBER_DENOM 10 + +/* For Query functionalities + For Querying capabilities */ +/*! Use static size of structures for querying because of performance + */ + +#define MAX_NUM_FMT_DESC 32 +#define MAX_NUM_RESOLUTION 32 +#define MAX_NUM_AVAILABLE_TPF 16 +#define MAX_NUM_AVAILABLE_FPS 16 +#define MAX_NUM_CTRL_LIST_INFO 64 +#define MAX_NUM_CTRL_MENU 64 +#define MAX_SZ_CTRL_NAME_STRING 32 +#define MAX_SZ_DEV_NAME_STRING 32 + +enum{ + CAMERASRC_FCC_USE_NONE = 0x00000001, + CAMERASRC_FCC_USE_REC_PREVIEW = 0x00000010, + CAMERASRC_FCC_USE_CAP_PREVIEW = 0x00000100, + CAMERASRC_FCC_USE_RECORDING = 0x00001000, + CAMERASRC_FCC_USE_NORMAL_CAPTURE = 0x00010000, + CAMERASRC_FCC_USE_CONT_CAPTURE = 0x00100000, + CAMERASRC_FCC_USE_NUM = 6, +}; + +/*! @struct camerasrc_tpf_frac_t + * @brief For timeperframe as fraction type + * Elapse time consumed by one frame, reverse of FPS + */ +typedef struct { + int num; + int den; +}camerasrc_tpf_frac_t; + +/*! @struct camerasrc_resolution_t + * @brief For querying supported resolutions + */ +typedef struct { + int w; + int h; + + /* Available time per frame(tpf) as each pixelformat */ + int num_avail_tpf; + camerasrc_tpf_frac_t tpf[MAX_NUM_AVAILABLE_TPF]; +} camerasrc_resolution_t; + +/*! @struct camerasrc_fmt_desc_t + * @brief For querying supported format type + */ +typedef struct { + /* fourcc name of each pixelformat */ + unsigned int fcc; + int fcc_use; + + /* Available resolutions as each pixelformat */ + int num_resolution; + camerasrc_resolution_t resolutions[MAX_NUM_RESOLUTION]; +} camerasrc_fmt_desc_t; + +/*! @struct camerasrc_caps_info_t + * @brief For querying image input capabilities + */ +typedef struct { + char dev_name[MAX_SZ_DEV_NAME_STRING]; + camerasrc_dev_id_t input_id; + int num_fmt_desc; + camerasrc_fmt_desc_t fmt_desc[MAX_NUM_FMT_DESC]; + + int num_preview_resolution; + int preview_resolution_width[MAX_NUM_RESOLUTION]; + int preview_resolution_height[MAX_NUM_RESOLUTION]; + + int num_capture_resolution; + int capture_resolution_width[MAX_NUM_RESOLUTION]; + int capture_resolution_height[MAX_NUM_RESOLUTION]; + + int num_preview_fmt; + unsigned int preview_fmt[MAX_NUM_FMT_DESC]; + + int num_capture_fmt; + unsigned int capture_fmt[MAX_NUM_FMT_DESC]; + + int num_fps; + camerasrc_frac_t fps[MAX_NUM_AVAILABLE_FPS]; +} camerasrc_caps_info_t; + +/* For Querying controls */ +enum { + CTRL_TYPE_RANGE = 0, /**< Integer, range type */ + CTRL_TYPE_BOOL, /**< Boolean type, 1 equals positive and 0 is negative */ + CTRL_TYPE_ARRAY, /**< Array type, also called menu type. each integer(enumeration) value can be set */ + CTRL_TYPE_UNKNOWN, /**< Unknown type, for error control */ + CTRL_TYPE_NUM, +}; + +/*! @struct camerasrc_ctrl_menu_t + * @brief For querying menu of specified controls + */ +typedef struct { + int menu_index; /**< What number is used for accessing this menu */ + char menu_name[MAX_SZ_CTRL_NAME_STRING]; /**< name of each menu */ +}camerasrc_ctrl_menu_t; + +/*! @struct camerasrc_ctrl_info_t + * @brief For querying controls detail + */ +typedef struct { + camerasrc_ctrl_t camsrc_ctrl_id; /**< camsrc camera control ID for controlling this */ + int v4l2_ctrl_id; /**< v4l2 ctrl id, user not need to use this. see @struct camerasrc_ctrl_t */ + int ctrl_type; /**< Type of this control */ + char ctrl_name[MAX_SZ_CTRL_NAME_STRING]; /**< Name of this control */ + int min; /**< minimum value */ + int max; /**< maximum value */ + int step; /**< unit of the values */ + int default_val; /**< Default value of the array or range */ + int num_ctrl_menu; /**< In the case of array type control, number of supported menu information */ + camerasrc_ctrl_menu_t ctrl_menu[MAX_NUM_CTRL_MENU]; /**< @struct camerasrc_ctrl_menu_t for detailed each menu information*/ +} camerasrc_ctrl_info_t; + +/*! @struct camerasrc_ctrl_list_info_t + * @brief For querying controls + */ +typedef struct { + int num_ctrl_list_info; /**< Number of supported controls */ + camerasrc_ctrl_info_t ctrl_info[MAX_NUM_CTRL_LIST_INFO]; /**< @struct camerasrc_ctrl_info_t for each control information */ +} camerasrc_ctrl_list_info_t; + + +/* capabilities field */ +#define CAMERASRC_STROBE_CAP_NONE 0x0000 /* No strobe supported */ +#define CAMERASRC_STROBE_CAP_OFF 0x0001 /* Always flash off mode */ +#define CAMERASRC_STROBE_CAP_ON 0x0002 /* Always use flash light mode */ +#define CAMERASRC_STROBE_CAP_AUTO 0x0004 /* Flashlight works automatic */ +#define CAMERASRC_STROBE_CAP_REDEYE 0x0008 /* Red-eye reduction */ +#define CAMERASRC_STROBE_CAP_SLOWSYNC 0x0010 /* Slow sync */ +#define CAMERASRC_STROBE_CAP_FRONT_CURTAIN 0x0020 /* Front curtain */ +#define CAMERASRC_STROBE_CAP_REAR_CURTAIN 0x0040 /* Rear curtain */ +#define CAMERASRC_STROBE_CAP_PERMANENT 0x0080 /* keep turned on until turning off */ +#define CAMERASRC_STROBE_CAP_EXTERNAL 0x0100 /* use external strobe */ + +typedef struct _camerasrc_extra_info_t{ + unsigned int strobe_caps; /**< Use above caps field */ + unsigned int detection_caps; /**< Just boolean */ + unsigned int reserved[4]; +} camerasrc_extra_info_t; +/* END For Query functionalities */ + +/*! @def CAMERASRC_SET_SIZE_BY_DIMENSION + * @brief Utility definitions for setting non-regular size + */ +#define CAMERASRC_SET_SIZE_BY_DIMENSION(format, img_width, img_height) { \ + format.img_size.dim.width = img_width; \ + format.img_size.dim.height = img_height; \ +} + + +/* CALLBACK DEFINITIONS */ +/*! @typedef camerasrc_callback_t + * @brief Called back when auto-focusing returns + * This callback will be called when the lens properly auto-focused + */ +typedef int (*camerasrc_callback_t) (camsrc_handle_t handle, int state, void* usr_data); + +/* Static variables */ +/** + * Label for camera control. This static variable has a label for each of camerasrc_ctrl_t enumeration. + * When enumeration of camerasrc_ctrl_t is increased, this variable should be increased, too. + * This string could be used as a key by user. + * Reference : camerasrc_ctrl_t, _camerasrc_ctrl_list + */ + +static char *camerasrc_ctrl_label[CAMERASRC_CTRL_NUM] = +{ + "brightness", /**< label for CAMERASRC_CTRL_BRIGHTNESS */ + "contrast", /**< label for CAMERASRC_CTRL_CONTRAST */ + "digital zoom", /**< label for CAMERASRC_CTRL_DIGITAL_ZOOM */ + "optical zoom", /**< label for CAMERASRC_CTRL_OPTICAL_ZOOM */ + "white balance", /**< label for CAMERASRC_CTRL_WHITE_BALANCE */ + "color tone", /**< label for CAMERASRC_CTRL_COLOR_TONE */ + "program mode", /**< label for CAMERASRC_CTRL_PROGRAM_MODE */ + "flip", /**< label for CAMERASRC_CTRL_FLIP */ + "partcolor src", /**< label for CAMERASRC_CTRL_PARTCOLOR_SRC */ + "partcolor dst", /**< label for CAMERASRC_CTRL_PARTCOLOR_DST */ + "partcolor mode", /**< label for CAMERASRC_CTRL_PARTCOLOR_MODE */ + "anti handshake", /**< label for CAMERASRC_CTRL_ANTI_HANDSHAKE */ + "wide dynamic range", /**< label for CAMERASRC_CTRL_WIDE_DYNAMIC_RANGE */ + "saturation", /**< label for CAMERASRC_CTRL_SATURATION */ + "sharpness", /**< label for CAMERASRC_CTRL_SHARPNESS */ + "iso", /**< label for CAMERASRC_CTRL_ISO */ + "photometry", /**< label for CAMERASRC_CTRL_PHOTOMETRY */ +}; + +/* FUNCTION DEFINITIONS */ + +/**** M A I N O P E R A T I O N ****/ + +/** + * allocate the handle, set initial state & settings + * + * @param[in] phandle ::camsrc_handle_t camerasrc context handle to be created + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t code + * @see camerasrc_destroy + * @note State transition : [CAMERASRC_STATE_NONE] => [CAMERASRC_STATE_CREATED] + * Phase description : Non-running phase + */ +int camerasrc_create(camsrc_handle_t *phandle); + +/** + * proceed fd close, other finalization routines + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t code + * + * @see <camerasrc_create> + * + * @note State transition : [CAMERASRC_STATE_UNREALIZED] => [CAMERASRC_STATE_DESTROYED] + * Phase description : Non-running phase + */ +int camerasrc_destroy(camsrc_handle_t handle); + +/** + * free device context handle, other finalization routines + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t code + * @see camerasrc_create + * @note State transition : [CAMERASRC_STATE_UNREALIZED] => [CAMERASRC_STATE_DESTROYED] + * Phase description : Non-running phase + */ +int camerasrc_close_device(camsrc_handle_t handle); + +/** + * Get the state of camerasrc context handle + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[out] state ::camerasrc_state_t camerasrc context current state + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t code + * + */ +int camerasrc_get_state(camsrc_handle_t handle, camerasrc_state_t* state); + +/** + * Allocate the device context handle, open device node and do the miscellaneous settings + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @return Success on ::camerasrc_handle_t or returns NULL code, and displays debug message + * @see camerasrc_unrealize + * @note State transition : [CAMERASRC_STATE_CREATED] => [CAMERASRC_STATE_REALIZED] + * Phase description : Non-running phase + * device name can be dependent on kernel module + */ +int camerasrc_realize(camsrc_handle_t handle); + +/** + * Deallocate the device structure of buffers, close device + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @return Success on ::camerasrc_handle_t or returns NULL code, and displays debug message + * @see camerasrc_realize + * @note State transition : [CAMERASRC_STATE_READY] => [CAMERASRC_STATE_UNREALIZED] + * Phase description : Transit to Non-running phase + */ +int camerasrc_unrealize(camsrc_handle_t handle); + +/** + * Prepare Handle to be ready to capture + * Can change settings like below at this state + * - camera device ID setting + * - color format setting + * - image size setting + * - image storing method + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @return Success on ::camerasrc_handle_t or returns NULL code, and displays debug message + * @see camerasrc_stop + * @note State transition : [CAMERASRC_STATE_REALIZED] => [CAMERASRC_STATE_READY] + * Phase description : Running phase + */ +int camerasrc_start(camsrc_handle_t handle); + +/** + * Present user buffer to camerasrc and force to use that buffer. + * After calling this API, all core routine of camerasrc camera will use + * User pointer method for managing buffers. + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[in] present_buf ::camerasrc_usr_buf_t Buffer set to present to camerasrc + * @param[in] io_method ::camerasrc_io_method_t Enum type represent to IO method + * @return Success on ::camsrc_handle_t or returns NULL code, and displays debug message + * @see camerasrc_io_method_t + * + */ +int camerasrc_present_usr_buffer(camsrc_handle_t handle, camerasrc_usr_buf_t* present_buf, camerasrc_io_method_t io_method); + +/** + * Get total number of buffer which managed in camerasrc currently. + * If this called, it will return default number of buffer in MMAP mode. + * but use this API after calling ::camerasrc_present_usr_buffer , It will + * return User specfied buffer number. + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[in] num_buffer Number of buffer that's managed in camerasrc currently + * @return Success on ::camsrc_handle_t or returns NULL code, and displays debug message + * @see camerasrc_io_method_t + * + */ +int camerasrc_get_num_buffer(camsrc_handle_t handle, unsigned int* num_buffer); + +/** + * Get Input/Output method which is used when access camera driver + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[in] io_method ::camerasrc_io_method_t method enum value + * @return Success on ::camsrc_handle_t or returns NULL code, and displays debug message + * @see camerasrc_io_method_t + * + */ +int camerasrc_get_io_method(camsrc_handle_t handle, camerasrc_io_method_t* io_method); + +/** + * Inner ring buffer start refreshing. refresh process occurs asynchronously, and + * ::camerasrc_wait_frame_available function can anounce when it is available. + * + * Camera is grabbing High quality, maybe low speed frame(dependent on device) + * - Cant approach the [AF] state + * - preview frames arent always automatically fed. If not, must request repeatedly + * - If device supports continuous focusing, it can be enabled at this state in case + * of multishot. + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note State transition : [CAMERASRC_STATE_READY] => [CAMERASRC_STATE_STILL] + * Phase description : Running phase + */ +int camerasrc_start_still_stream(camsrc_handle_t handle); + +/** + * Inner ring buffer start refreshing. refresh process occurs asynchronously, and + * ::camerasrc_wait_frame_available function can anounce when it is available. + * + * Camera is grabbing low quality, high speed frame + * - Can attempt the [AF] state only at this state + * - preview frames are always automatically fed + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note State transition : [CAMERASRC_STATE_READY] => [CAMERASRC_STATE_PREVIEW] + * Phase description : Running phase + */ +int camerasrc_start_preview_stream(camsrc_handle_t handle); + +/** + * Stop frame refreshing. Ring buffers don't be refreshed any more + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @return Success on ::camerasrc_handle_t or returns NULL code, and displays debug message + * @see camerasrc_stop + * @note State transition : [CAMERASRC_STATE_STILL/PREVIEW/VIDEO] => [CAMERASRC_STATE_READY] + * Phase description : Running phase + */ +int camerasrc_stop_stream(camsrc_handle_t handle); + +/** + * Query image buffer size. buffer allocation guide function. + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[out] main_img_size main image maximum size + * @param[out] thm_img_size thumb nail image maximum size + * @return Success on ::camsrc_handle_t or returns NULL code, and displays debug message + * + */ +int camerasrc_query_img_buf_size(camsrc_handle_t handle, unsigned int* main_img_size, unsigned int* thm_img_size); + +/** + * non-busy waiting function for image frame available + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[in] timeout main image maximum size + * @return Success on ::camsrc_handle_t or returns NULL code, and displays debug message + * + */ +int camerasrc_wait_frame_available(camsrc_handle_t handle, long int timeout); + +/** + * Check emp shock status + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[out] check_val + * @return Success on ::camsrc_handle_t or returns NULL code, and displays debug message + * + */ +int camerasrc_check_esd_shock(camsrc_handle_t *handle, int *check_val); + +/** + * Queue(in user space, almost same with free buffer) buffer to dev's ring buffer + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @param[in] buffer ::camerasrc_buffer_t buffer + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * + */ +int camerasrc_queue_buffer(camsrc_handle_t handle, int buf_index, camerasrc_buffer_t *buffer); + +/** + * Dequeue(Pop) buffer from v4l2 driver to usr space. + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[out] buf_index main buffer index number to be dequeued. + * @param[out] buffer main image buffer + * @param[out] thm_buffer thumbnail image buffer + * @return Success on ::camsrc_handle_t or returns NULL code, and displays debug message + * + */ +int camerasrc_dequeue_buffer(camsrc_handle_t handle, int *buf_index, camerasrc_buffer_t *buffer, camerasrc_buffer_t *thm_buffer); + +/** + * Read frame from camera device. + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[out] buffer ::camerasrc_buffer_t main image buffer to be get. + * @param[out] thm_buffer ::camerasrc_buffer_t thumbnail image buffer to be get. + * @param[out] buffer_index ::int v4l2 buffer index. + * @note if thm_buffer is NULL, thumbnail image will be discarded + * + */ +int camerasrc_read_frame(camsrc_handle_t handle, camerasrc_buffer_t *main_img_buffer, camerasrc_buffer_t *thm_img_buffer, int *buffer_index); + +/** + * Get screennail buffer + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[out] scrnl_buf ::camerasrc_buffer_t screennail buffer to be gotten + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_get_screennail_buffer(camsrc_handle_t handle, camerasrc_buffer_t *scrnl_buf); + +/** + * Set autofocus callback. ::camerasrc_callback_t type defined function can be set + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @param[in] cb ::camerasrc_callback_t callback after focusing over + * @param[in] use_data ::void * user data pointer that will be passed to callback + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note Callback function can be set on READY or REALIZED state + * + */ +int camerasrc_set_focused_callback(camsrc_handle_t handle, camerasrc_callback_t cb, void *usr_data); + +/** + * Set autofocusing area. autofocusing will be performed refer this rect of the preview + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[in] rect ::camerasrc_rect_t rectangle area for auto focusing + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_set_autofocusing_area(camsrc_handle_t handle, camerasrc_rect_t* rect); + +/** + * Get autofocusing area. + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[out] rect ::camerasrc_rect_t rectangle area for auto focusing + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_get_autofocusing_area(camsrc_handle_t handle, camerasrc_rect_t* rect); + +/** + * Start auto focusing with ::camerasrc_af_mode. After ::interval time, call the callback + * function with ::camerasrc_af_status value. + * Auto-focus is in progress. Cant return preview state + * before time-out, success, call camerasrc_autofocus_stop + * - If focused, focus status will be locked at preview state + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @param[in] af_mode Auto focusing operation mode see ::camerasrc_af_mode + * @param[in] interval interval time in millisecond + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note State transition : [CAMERASRC_STATE_PREVIEW] => [CAMERASRC_STATE_AF_IN_PROGRESS] + * Phase description : Running phase + */ +int camerasrc_start_autofocusing(camsrc_handle_t handle); + +/** + * Stop auto focusing + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note State transition : [CAMERASRC_STATE_AF_IN_PROGRESS] => [CAMERASRC_STATE_PREVIEW] + * Phase description : Running phase + */ +int camerasrc_stop_autofocusing(camsrc_handle_t handle); + +/** + * Release auto focusing + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note State transition : [CAMERASRC_STATE_AF_IN_PROGRESS] => [CAMERASRC_STATE_PREVIEW] + * Phase description : Running phase + */ +int camerasrc_release_autofocusing(camsrc_handle_t handle); + +/** + * Initialize auto focusing mode to specified focal length + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[in] af_mode ::camerasrc_af_mode_t Auto focusing mode + * @param[in] af_range ::camerasrc_af_scan_range_t Auto focusing range + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_init_autofocusing_mode(camsrc_handle_t handle, camerasrc_af_mode_t af_mode, camerasrc_af_scan_range_t af_range); + +/** + * Get current auto focusing mode + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[out] af_mode ::camerasrc_af_mode_t Auto focusing mode + * @param[out] af_range ::camerasrc_af_scan_range_t Auto focusing range + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_get_autofocusing_mode(camsrc_handle_t handle, camerasrc_af_mode_t* af_mode, camerasrc_af_scan_range_t* af_range); + +/** + * Get current auto focusing status + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[out] af_status ::camerasrc_auto_focus_status_t Auto focusing status + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * + */ +int camerasrc_get_autofocusing_status(camsrc_handle_t handle, camerasrc_auto_focus_status_t* af_status); +/**** M I S C E L L A N E O U S O P E R A T I O N ****/ + +/**** I N P U T ( C A M D E V I C E ) O P E R A T I O N ****/ + +/** + * Get input camera ID just like ioctl (fd, VIDIOC_G_INPUT, &index) + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @param[out] id Camera ID currently set + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note This function is only effective at CAMERASRC_PHASE_RUNNING + */ +int camerasrc_get_input(camsrc_handle_t handle, camerasrc_dev_id_t* camera_id); + +/** + * Set input camera ID just like ioctl (fd, VIDIOC_S_INPUT, &index) + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @param[out] camera_id Camera ID currently set + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note This function is only effective at CAMERASRC_STATE_READY + * If you use set_input(), handle will be initiated. + */ +int camerasrc_set_input(camsrc_handle_t handle, camerasrc_dev_id_t camera_id); + + +/**** E F F E C T C O N T R O L O P E R A T I O N ****/ + +/** + * Check support controls with ::camerasrc_ctrl_t ID + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[in] ctrl_id control ID to be checked + * @param[out] ctrl_info control information to be got + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_query_control(camsrc_handle_t handle, camerasrc_ctrl_t ctrl_id, camerasrc_ctrl_info_t* ctrl_info); + +/** + * Check support controls with ::camerasrc_ctrl_t ID + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[in] ctrl_id control ID to be checked + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_support_control(camsrc_handle_t handle, camerasrc_ctrl_t ctrl_id); + +/** + * Start facedetection + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note State transition : [CAMERASRC_STATE_AF_IN_PROGRESS] => [CAMERASRC_STATE_PREVIEW] + * Phase description : Running phase + */ +int camerasrc_start_facedetection(camsrc_handle_t handle); + +/** + * Stop face detection + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note State transition : [CAMERASRC_STATE_AF_IN_PROGRESS] => [CAMERASRC_STATE_PREVIEW] + * Phase description : Running phase + */ +int camerasrc_stop_facedetection(camsrc_handle_t handle); + +/** + * Get face detection status + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[out] is_detecting whether it is detecting or not + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note State transition : [CAMERASRC_STATE_AF_IN_PROGRESS] => [CAMERASRC_STATE_PREVIEW] + * Phase description : Running phase + */ +int camerasrc_get_facedetection(camsrc_handle_t handle, int* is_detecting); + +/** + * Control miscellaneous settings through ::camerasrc_ctrl_t IDs + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[in] ctrl_id control ID to be checked + * @param[in] value value to be set + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note This function is only effective at CAMERASRC_STATE_READY + */ +int camerasrc_set_control(camsrc_handle_t handle, camerasrc_ctrl_t ctrl_id, int value); + +/** + * Get the value of miscellaneous settings through ::camerasrc_ctrl_t IDs + * + * @param[in] handle ::camsrc_handle_t camerasrc context handle + * @param[in] ctrl_id control ID to be checked + * @param[out] value value to be stored + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note This function is only effective at CAMERASRC_STATE_READY + */ +int camerasrc_get_control(camsrc_handle_t handle, camerasrc_ctrl_t ctrl_id, int* value); + +/**** O U T P U T C O N T R O L O P E R A T I O N ****/ + +/** + * Control frame refresh rate setting + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @param[in] frac ::camsrc_frac_t time per frame + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note This function is only effective at CAMERASRC_STATE_READY + */ +int camerasrc_set_timeperframe(camsrc_handle_t handle, camerasrc_frac_t* frac); + +/** + * Set output format of camera device just like ioctl VIDIOC_S_FMT + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @param[in] fmt ::camerasrc_format_t output format description to be set + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note This function is only effective at CAMERASRC_STATE_READY. + * device dependent function. + */ +int camerasrc_set_format(camsrc_handle_t handle, camerasrc_format_t* fmt); + +/** + * Get output format of camera device just like ioctl VIDIOC_G_FMT + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @param[in] fmt ::camerasrc_format_t output format description to be set + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note This function is only effective at CAMERASRC_PHASE_RUNNING + */ +int camerasrc_get_format(camsrc_handle_t handle, camerasrc_format_t* fmt); + +/** + * Try output format of camera device just like ioctl VIDIOC_TRY_FMT + * In this function, it doesn't change any format, only just try. just test + * the combinations of format setting + * + * @param[in] handle ::camerasrc_handle_t camerasrc context handle + * @param[in] fmt ::camerasrc_format_t output format description to be set + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + * @note This function is only effective at CAMERASRC_STATE_READY. + * device dependent function. + */ +int camerasrc_try_format(camsrc_handle_t handle, camerasrc_format_t* fmt); + +/** + * Get virtual/physical address of data frame + * + * @param[in] handle ::camsrc_handle_t handle + * @param[in] ae_lock ::camerasrc_ae_lock_t Auto exposure lock/unlock + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_get_frame_data(camsrc_handle_t handle, camerasrc_frame_data_t * data); + +/**** S H U T T E R S P E E D & A P E R T U R E M O D U L A T I O N ****/ +/** + * Get exif string to be combined with jpg image from camerasrc + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] exif_string exif information string + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_get_exif_info(camsrc_handle_t handle, camerasrc_exif_t* exif_struct); + +/** + * Check whether camera device is opened + * + * @param[in] handle ::camsrc_handle_t handle + * @return 0 when camera is not opened else return non-zero value. + */ + int camerasrc_device_is_open(camsrc_handle_t handle); + +/** + * Set the camera device file decriptor + + * @param[in] handle ::camsrc_handle_t handle + * @param[in] videofd ::file descriptor of camera device + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_set_videofd(camsrc_handle_t handle,int videofd); + +/** + * Set AF behaviour after capturing + + * @param[in] handle ::camsrc_handle_t handle + * @param[in] hold_af ::whether holding af after capturing + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_set_af_hold_after_capture(camsrc_handle_t handle, int hold_af); + +/** + * Set Sensor mode to camera driver + + * @param[in] handle ::camsrc_handle_t handle + * @param[in] sensor_mode ::int mode + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_set_sensor_mode(camsrc_handle_t handle, int mode); + +/** + * Set vflip to camera driver + + * @param[in] handle ::camsrc_handle_t handle + * @param[in] vflip ::int vflip + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_set_vflip(camsrc_handle_t handle, int vflip); + +/** + * Set hflip to camera driver + + * @param[in] handle ::camsrc_handle_t handle + * @param[in] hflip ::int hflip + * @return Success on CAMERASRC_ERR_NONE or returns with ::camerasrc_error_t error code + */ +int camerasrc_set_hflip(camsrc_handle_t handle, int hflip); + +/* W I L L B E D E P R E C A T E D */ + +/*! @enum camerasrc_colortone_t + * @brief Enumeration type for camera colortone + * + * Colortone entries for camera effects. This can be used with CAMERASRC_CTRL_COLOR_TONE + * This values are defined for utility. It's dependent on the device/camera module. + */ +typedef enum { + CAMERASRC_COLORTONE_NONE = 0, + CAMERASRC_COLORTONE_NEGATIVE, + CAMERASRC_COLORTONE_SOLARIZATION_1, + CAMERASRC_COLORTONE_SOLARIZATION_2, + CAMERASRC_COLORTONE_SOLARIZATION_3, + CAMERASRC_COLORTONE_SOLARIZATION_4, + CAMERASRC_COLORTONE_EMBOSS, + CAMERASRC_COLORTONE_OUTLINE, + CAMERASRC_COLORTONE_AQUA, + CAMERASRC_COLORTONE_SEPHIA, + CAMERASRC_COLORTONE_GRAY, + CAMERASRC_COLORTONE_B_N_W, + CAMERASRC_COLORTONE_RED, + CAMERASRC_COLORTONE_GREEN, + CAMERASRC_COLORTONE_BLUE, + CAMERASRC_COLORTONE_ANTIQUE, + CAMERASRC_COLORTONE_SKETCH1, + CAMERASRC_COLORTONE_SKETCH2, + CAMERASRC_COLORTONE_SKETCH3, + CAMERASRC_COLORTONE_SKETCH4, + CAMERASRC_COLORTONE_NUM, +}camerasrc_colortone_t; + +/*! @enum camerasrc_program_mode_t + * @brief Enumeration type for preset program mode + * + * WRITEME + */ +typedef enum { + CAMERASRC_PROGRAM_MODE_NORMAL = 0, + CAMERASRC_PROGRAM_MODE_PORTRAIT, + CAMERASRC_PROGRAM_MODE_LANDSCAPE, + CAMERASRC_PROGRAM_MODE_SPORTS, + CAMERASRC_PROGRAM_MODE_PARTY_N_INDOOR, + CAMERASRC_PROGRAM_MODE_BEACH_N_INDOOR, + CAMERASRC_PROGRAM_MODE_SUNSET, + CAMERASRC_PROGRAM_MODE_DUSK_N_DAWN, + CAMERASRC_PROGRAM_MODE_FALL_COLOR, + CAMERASRC_PROGRAM_MODE_NIGHT_SCENE, + CAMERASRC_PROGRAM_MODE_FIREWORK, + CAMERASRC_PROGRAM_MODE_TEXT, + CAMERASRC_PROGRAM_MODE_SHOW_WINDOW, + CAMERASRC_PROGRAM_MODE_CANDLE_LIGHT, + CAMERASRC_PROGRAM_MODE_BACK_LIGHT, + CAMERASRC_PROGRAM_MODE_NUM, +}camerasrc_program_mode_t; + +/*! @enum camerasrc_whitebalance_t + * @brief Enumeration type for preset whitebalance + * + * WRITEME + */ +typedef enum { + CAMERASRC_WHITEBALANCE_AUTO = 0, + CAMERASRC_WHITEBALANCE_INCANDESCENT, + CAMERASRC_WHITEBALANCE_FLUORESCENT, + CAMERASRC_WHITEBALANCE_DAYLIGHT, + CAMERASRC_WHITEBALANCE_CLOUDY, + CAMERASRC_WHITEBALANCE_SHADE, + CAMERASRC_WHITEBALANCE_HORIZON, + CAMERASRC_WHITEBALANCE_FLASH, + CAMERASRC_WHITEBALANCE_CUSTOM, + CAMERASRC_WHITEBALANCE_NUM, +}camerasrc_whitebalance_t; + +/** + * Enumerations for flip. + */ +typedef enum { + CAMERASRC_FILP_NONE = 0, /**< Not flipped */ + CAMERASRC_FILP_VERTICAL, /**< Flip vertically */ + CAMERASRC_FILP_HORIZONTAL, /**< Flip horizontally */ + CAMERASRC_FILP_NUM, /**< Number of flip status */ +}camerasrc_flip_t; + +/*! @enum camerasrc_strobo_status_t + * @brief strobo status + * strobo status + */ +typedef enum { + CAMERASRC_STROBO_STATUS_BANNED = 0, /**< strobo off*/ + CAMERASRC_STROBO_STATUS_FORCE_ON, /**< strobo on.*/ + CAMERASRC_STROBO_STATUS_AUTO, /**< control strobo automatically*/ + CAMERASRC_STROBO_STATUS_MOVIE_ON, /**< control strobo automatically*/ + CAMERASRC_STROBO_STATUS_NUM, /**< Number of AF status*/ +}camerasrc_strobo_status_t; +/*! @enum camerasrc_strobe_mode_t + * @brief strobe mode + * strobe mode + */ +typedef enum { + CAMERASRC_STROBE_MODE_OFF = 1, /**< off */ + CAMERASRC_STROBE_MODE_AUTO, /**< auto */ + CAMERASRC_STROBE_MODE_ON, /**< on */ + CAMERASRC_STROBE_MODE_PERMANENT, /**< permanent */ + CAMERASRC_STROBE_MODE_NUM, /**< Number of strobe mode */ +}camerasrc_strobe_mode_t; + +/*! @enum camerasrc_ae_mode_t + * @brief Auto exposure mode + * Auto exposure operation mode + */ +typedef enum { + CAMERASRC_AE_MODE_OFF = 0, + CAMERASRC_AE_MODE_ALL, + CAMERASRC_AE_MODE_CENTER_WEIGHTED_AVR_1, + CAMERASRC_AE_MODE_CENTER_WEIGHTED_AVR_2, + CAMERASRC_AE_MODE_CENTER_WEIGHTED_AVR_3, + CAMERASRC_AE_MODE_SPOT_1, + CAMERASRC_AE_MODE_SPOT_2, + CAMERASRC_AE_MODE_CUSTOM_1, + CAMERASRC_AE_MODE_CUSTOM_2, +} camerasrc_ae_mode_t; + +/*! @enum camerasrc_iso_t + * @brief Reserved iso number in definition + * Traditionally predefined ISO values + */ +typedef enum { + CAMERASRC_ISO_AUTO = 0, + CAMERASRC_ISO_50, + CAMERASRC_ISO_100, + CAMERASRC_ISO_200, + CAMERASRC_ISO_400, + CAMERASRC_ISO_800, + CAMERASRC_ISO_1600, + CAMERASRC_ISO_3200, +} camerasrc_iso_t; + +/*! @enum camerasrc_sensor_mode_t + * @brief Sensor mode in driver + * Sensor mode + */ +typedef enum { + CAMERASRC_SENSOR_MODE_CAMERA = 0, + CAMERASRC_SENSOR_MODE_MOVIE, +} camerasrc_sensor_mode_t; + +/*! @def CAMERASRC_SET_SIZE_BY_PRESET_RESOLUTION + * @brief Utility definitions for setting regular size with ::camerasrc_resol_name_t + */ +#define CAMERASRC_SET_SIZE_BY_PRESET_RESOLUTION(format, resolution) { \ + memset(&(format.img_size), 0, sizeof(camerasrc_size_t)); \ + format.img_size.res = resolution; \ +} + +/** + * Set the mode of strobe + * + * @param[in] handle ::camsrc_handle_t handle + * @param[in] mode ::camerasrc_strobe_mode_t mode of strobe + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_set_strobe_mode(camsrc_handle_t handle, camerasrc_strobe_mode_t mode); + +/** + * Get the mode of strobe + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] mode ::camerasrc_strobe_mode_t mode of strobe + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_get_strobe_mode(camsrc_handle_t handle, camerasrc_strobe_mode_t* mode); + +/** + * Set the mode of auto-exposure processing + * + * @param[in] handle ::camsrc_handle_t handle + * @param[in] ae_mode ::camerasrc_ae_mode_t AE mode to be set + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_set_exposure_mode(camsrc_handle_t handle, camerasrc_ae_mode_t ae_mode); + +/** + * Get the mode of auto-exposure processing + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] ae_mode ::camerasrc_ae_mode_t AE mode to be got + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_get_exposure_mode(camsrc_handle_t handle, camerasrc_ae_mode_t* ae_mode); + +/** + * Set the shutter speed + * + * @param[in] handle ::camsrc_handle_t handle + * @param[in] frac ::camerasrc_frac_t shutter speed to be set + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_set_shutter_speed(camsrc_handle_t handle, camerasrc_frac_t frac); + +/** + * Get the shutter speed + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] frac ::camerasrc_frac_t shutter speed to be got + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_get_shutter_speed(camsrc_handle_t handle, camerasrc_frac_t* frac); + +/** + * Set the exposure value + * + * @param[in] handle ::camsrc_handle_t handle + * @param[in] frac ::camerasrc_frac_t exposure value to be set + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_set_exposure_value(camsrc_handle_t handle, camerasrc_frac_t frac); + +/** + * Get the exposure value + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] frac ::camerasrc_frac_t exposure value to be got + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_get_exposure_value(camsrc_handle_t handle, camerasrc_frac_t* frac); + +/** + * Check whether supports exif embed in jpg or not + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] support_exif ::if supports, returns 1 or 0 + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_support_embed_exif(camsrc_handle_t handle, int* support_exif); + +/** + * Check whether supports jpeg encoding inside camera driver + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] support_jpeg_encoding ::if supports, returns 1. If not, return 0 + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_support_jpeg_encoding(camsrc_handle_t handle, int* support_jpeg_encoding); + + + +/* For Query functionalities */ +/** + * Query basic device info of device + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] camerasrc_caps_info_t device information structure + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_read_basic_dev_info(camerasrc_dev_id_t dev_id, camerasrc_caps_info_t* caps_info); + +/** + * Query miscellaneous device info(effect, WB, preset values, etc.) of device + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] camerasrc_ctrl_list_info_t device capabilities structure + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_read_misc_dev_info(camerasrc_dev_id_t dev_id, camerasrc_ctrl_list_info_t* ctrl_info); + +/** + * Query extra device info(face detection, strobe, etc.) of device + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] camerasrc_extra_info_t device capabilities structure + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_read_extra_dev_info(camerasrc_dev_id_t dev_id, camerasrc_extra_info_t* extra_info); + +/** + * Record miscellaneous device info(effect, WB, preset values, etc.) of device + * + * @param[in] handle ::camsrc_handle_t handle + * @param[in] camerasrc_ctrl_list_info_t device capabilities structure + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_write_basic_dev_info(camsrc_handle_t handle, camerasrc_caps_info_t* caps_info); + +/** + * Record miscellaneous device info(effect, WB, preset values, etc.) of device + * + * @param[in] handle ::camsrc_handle_t handle + * @param[in] camerasrc_ctrl_list_info_t device capabilities structure + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_write_misc_dev_info(camsrc_handle_t handle, camerasrc_ctrl_list_info_t* ctrl_info); + +/** + * Record extra device info(face detection, strobe, etc.) of device + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] camerasrc_extra_info_t device capabilities structure + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_write_extra_dev_info(camsrc_handle_t handle, camerasrc_extra_info_t* extra_info); + +/** + * Query to device driver about basic device info + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] camerasrc_ctrl_list_info_t device capabilities structure + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_query_basic_dev_info(camsrc_handle_t handle, camerasrc_caps_info_t* caps_info); + +/** + * Query to device driver about miscellaneous device info(effect, WB, preset values, etc.) of device + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] camerasrc_ctrl_list_info_t device capabilities structure + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_query_misc_dev_info(camsrc_handle_t handle, camerasrc_ctrl_list_info_t* ctrl_list_info); +int camerasrc_query_misc_dev_ctrl_info(camsrc_handle_t handle, camerasrc_ctrl_t ctrl_id, camerasrc_ctrl_info_t* ctrl_info); + +/** + * Query to device driver about extra device info(face detection, strobe, etc.) of device + * + * @param[in] handle ::camsrc_handle_t handle + * @param[out] camerasrc_extra_info_t device capabilities structure + * @return Success(Support) on CAMERASRC_ERR_NONE or returns with ::camerasrc_error error code + */ +int camerasrc_query_extra_dev_info(camsrc_handle_t handle, camerasrc_extra_info_t* extra_info); + +/** + * Dump functions for debugging + */ +int camerasrc_dump_basic_dev_info(camsrc_handle_t handle, camerasrc_caps_info_t* caps_info); +int camerasrc_dump_misc_dev_info(camsrc_handle_t handle, camerasrc_ctrl_list_info_t* ctrl_list_info); +int camerasrc_dump_extra_dev_info(camsrc_handle_t handle, camerasrc_extra_info_t* extra_info); +/* END For Query functionalities */ + +#ifdef __cplusplus +} +#endif + +#endif /*__CAMERASRC_H__*/ diff --git a/camerasrc/src/include/gstcamerasrc.h b/camerasrc/src/include/gstcamerasrc.h new file mode 100644 index 0000000..61b3c28 --- /dev/null +++ b/camerasrc/src/include/gstcamerasrc.h @@ -0,0 +1,175 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __GSTCAMERASRC_H__ +#define __GSTCAMERASRC_H__ + +#include <gst/gst.h> +#include <gst/base/gstpushsrc.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <gst/interfaces/colorbalance.h> +#include <gst/interfaces/cameracontrol.h> + +#include "camerasrc.h" + +#ifndef _SPEED_UP_RAW_CAPTURE +/* Disabled now +#define _SPEED_UP_RAW_CAPTURE +*/ +#endif /* _SPEED_UP_RAW_CAPTURE */ + +G_BEGIN_DECLS +#define GST_TYPE_CAMERA_SRC (gst_camerasrc_get_type()) +#define GST_CAMERA_SRC(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_CAMERA_SRC,GstCameraSrc)) +#define GST_CAMERA_SRC_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_CAMERA_SRC,GstCameraSrcClass)) +#define GST_IS_CAMERA_SRC(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_CAMERA_SRC)) +#define GST_IS_CAMERA_SRC_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_CAMERA_SRC)) + + +/** + * Quality. + */ +typedef enum { + GST_CAMERASRC_QUALITY_LOW, /**< Low quality */ + GST_CAMERASRC_QUALITY_HIGH, /**< High quality */ +} GstCameraSrcQuality; + +#define MAX_USR_BUFFER_NUM 12 + +typedef struct _GstCameraSrc GstCameraSrc; +typedef struct _GstCameraSrcClass GstCameraSrcClass; +typedef struct _GstCameraBuffer GstCameraBuffer; + +/* global info */ +struct _GstCameraBuffer { + GstBuffer buffer; + int v4l2_buffer_index; + GstCameraSrc *camerasrc; + gboolean is_alloc_data; +}; + +struct _GstCameraSrc +{ + GstPushSrc element; + + /*private*/ + void *v4l2_handle; /**< video4linux2 handle */ + int mode; + int sensor_mode; /**< sensor mode - camera or movie(camcorder) */ + gboolean vflip; /**< flip camera input vertically */ + gboolean hflip; /**< flip camera input horizontally */ + gboolean firsttime; + + int main_buf_sz; + int cap_count_current; /**< current capture count */ + int cap_count_reverse; /**< current capture count (reverse counting) */ + unsigned long cap_next_time; /**< next shot time for capture */ + GQueue *command_list; /**< Command list(Start capture, Stop capture) queue */ + + GCond *cond; + GMutex *mutex; + + /*camera property*/ + int width; /**< Width */ + int height; /**< Height */ + int fps; /**< Video source fps */ + + guint32 fourcc; /**< Four CC */ + int pix_format; /**< Image format of video source */ + int colorspace; /**< Colorspace of video source */ + int high_speed_fps; /**< Video source fps for high speed recording */ + gboolean fps_auto; /**< Auto Video source fps */ + + gboolean req_negotiation; /**< Video source fps for high speed recording */ + int camera_id; + int rotate; /**< Video source rotate */ + gboolean use_rotate_caps; /**< Use or not rotate value in caps */ + int external_videofd; /**< video FD from external module */ + + GCond *buffer_cond; + GMutex *buffer_lock; + gboolean buffer_running; /* with lock */ + gint num_live_buffers; /* with lock */ + guint buffer_count; + gboolean bfirst; /* temp */ +#ifdef _SPEED_UP_RAW_CAPTURE + gboolean cap_stream_diff; /* whether preview and capture streams are different each other */ +#endif + GQueue *pad_alloc_list; + GMutex *pad_alloc_mutex; + gint current_buffer_data_index; + gboolean first_invokation; + + GstBuffer *usr_buffer[MAX_USR_BUFFER_NUM]; + gboolean use_pad_alloc; + guint num_alloc_buf; + + /* Colorbalance , CameraControl interface */ + GList *colors; + GList *camera_controls; + + + /*capture property*/ + guint32 cap_fourcc; /**< gstreamer fourcc value(GST_MAKE_FOURCC format) for raw capturing */ + GstCameraSrcQuality cap_quality; /**< Capture quality */ + int cap_width; /**< Capture width */ + int cap_height; /**< Capture height */ + int cap_interval; /**< Capture interval */ + int cap_count; /**< Capture count */ + int cap_jpg_quality; /**< Capture quality for jpg compress ratio */ + gboolean cap_provide_exif; /**< Is exif provided? */ + + /*etc property*/ + gboolean signal_still_capture; /**< enable still capture signal */ + gboolean hold_af_after_capturing; /**< Whether to hold af after capturing */ + gboolean create_jpeg; + GMutex *jpg_mutex; +}; + +struct _GstCameraSrcClass { + GstPushSrcClass parent_class; + /* signals */ + void (*still_capture) (GstElement *element, GstBuffer *main, GstBuffer *sub, GstBuffer *scrnl); + void (*nego_complete) (GstElement *element); + void (*register_trouble) (GstElement *element); +}; + +typedef enum { + INTERFACE_NONE, + INTERFACE_COLOR_BALANCE, + INTERFACE_CAMERA_CONTROL, +} GstInterfaceType; + + +void gst_camerasrc_set_capture_command(GstCameraSrc* camerasrc, GstCameraControlCaptureCommand cmd); + + +GType gst_camerasrc_get_type(void); + +G_END_DECLS + +#endif /* __GSTCAMERASRC_H__ */ diff --git a/camerasrc/src/include/gstcamerasrccolorbalance.h b/camerasrc/src/include/gstcamerasrccolorbalance.h new file mode 100644 index 0000000..0edeb46 --- /dev/null +++ b/camerasrc/src/include/gstcamerasrccolorbalance.h @@ -0,0 +1,93 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __GST_CAMERASRC_COLOR_BALANCE_H__ +#define __GST_CAMERASRC_COLOR_BALANCE_H__ + +#include <gst/gst.h> +#include <gst/interfaces/colorbalance.h> +#include "gstcamerasrc.h" + +G_BEGIN_DECLS + +#define GST_TYPE_CAMERASRC_COLOR_BALANCE_CHANNEL (gst_camerasrc_color_balance_channel_get_type ()) +#define GST_CAMERASRC_COLOR_BALANCE_CHANNEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_CAMERASRC_COLOR_BALANCE_CHANNEL, GstCameraSrcColorBalanceChannel)) +#define GST_CAMERASRC_COLOR_BALANCE_CHANNEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_CAMERASRC_COLOR_BALANCE_CHANNEL, GstCameraSrcColorBalanceChannelClass)) +#define GST_IS_CAMERASRC_COLOR_BALANCE_CHANNEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_CAMERASRC_COLOR_BALANCE_CHANNEL)) +#define GST_IS_CAMERASRC_COLOR_BALANCE_CHANNEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_CAMERASRC_COLOR_BALANCE_CHANNEL)) + +typedef struct _GstCameraSrcColorBalanceChannel { + GstColorBalanceChannel parent; + + guint32 id; +} GstCameraSrcColorBalanceChannel; + +typedef struct _GstCameraSrcColorBalanceChannelClass { + GstColorBalanceChannelClass parent; +} GstCameraSrcColorBalanceChannelClass; + +GType gst_camerasrc_color_balance_channel_get_type( void ); + +const GList *gst_camerasrc_color_balance_list_channels( GstCameraSrc* camerasrc ); +void gst_camerasrc_color_balance_set_value( GstCameraSrc* camerasrc, GstColorBalanceChannel* color_channel, gint value ); +gint gst_camerasrc_color_balance_get_value( GstCameraSrc* camerasrc, GstColorBalanceChannel* color_channel ); + +#define GST_IMPLEMENT_CAMERASRC_COLOR_BALANCE_METHODS( Type, interface_as_function ) \ + \ +static const GList* \ +interface_as_function ## _color_balance_list_channels( GstColorBalance* balance ) \ +{ \ + Type *this = (Type*) balance; \ + return gst_camerasrc_color_balance_list_channels( this ); \ +} \ + \ +static void \ +interface_as_function ## _color_balance_set_value( GstColorBalance* balance, \ + GstColorBalanceChannel* color_channel, \ + gint value ) \ +{ \ + Type *this = (Type*) balance; \ + return gst_camerasrc_color_balance_set_value( this, color_channel, value ); \ +} \ + \ +static gint \ +interface_as_function ## _color_balance_get_value( GstColorBalance* balance, \ + GstColorBalanceChannel* color_channel )\ +{ \ + Type *this = (Type*) balance; \ + return gst_camerasrc_color_balance_get_value( this, color_channel ); \ +} \ + \ +void \ +interface_as_function ## _color_balance_interface_init( GstColorBalanceClass* klass ) \ +{ \ + GST_COLOR_BALANCE_TYPE( klass ) = GST_COLOR_BALANCE_HARDWARE; \ + \ + /* default virtual functions */ \ + klass->list_channels = interface_as_function ## _color_balance_list_channels; \ + klass->set_value = interface_as_function ## _color_balance_set_value; \ + klass->get_value = interface_as_function ## _color_balance_get_value; \ +} + +#endif /* __GST_CAMERASRC_COLOR_BALANCE_H__ */ + diff --git a/camerasrc/src/include/gstcamerasrccontrol.h b/camerasrc/src/include/gstcamerasrccontrol.h new file mode 100644 index 0000000..c4ce913 --- /dev/null +++ b/camerasrc/src/include/gstcamerasrccontrol.h @@ -0,0 +1,384 @@ +/* + * camerasrc + * + * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: Jeongmo Yang <jm80.yang@samsung.com> + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation; either version 2.1 of the License, or (at your option) + * any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __GST_CAMERASRC_CONTROL_H__ +#define __GST_CAMERASRC_CONTROL_H__ + +#include <gst/gst.h> +#include <gst/interfaces/cameracontrol.h> +#include "gstcamerasrc.h" + +G_BEGIN_DECLS + +#define GST_TYPE_CAMERASRC_CONTROL_CHANNEL (gst_camerasrc_control_channel_get_type ()) +#define GST_CAMERASRC_CONTROL_CHANNEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_CAMERASRC_CONTROL_CHANNEL, GstCamerasrcControlChannel)) +#define GST_CAMERASRC_CONTROL_CHANNEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_CAMERASRC_CONTROL_CHANNEL, GstCamerasrcControlChannelClass)) +#define GST_IS_CAMERASRC_CONTROL_CHANNEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_CAMERASRC_CONTROL_CHANNEL)) +#define GST_IS_CAMERASRC_CONTROL_CHANNEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_CAMERASRC_CONTROL_CHANNEL)) + +typedef struct _GstCamerasrcControlChannel { +GstCameraControlChannel parent; +guint32 id; +} GstCamerasrcControlChannel; + +typedef struct _GstCamerasrcControlChannelClass { +GstCameraControlChannelClass parent; +} GstCamerasrcControlChannelClass; + +GType gst_camerasrc_control_channel_get_type( void ); + +const GList*gst_camerasrc_control_list_channels( GstCameraSrc* camera_src ); + +gboolean gst_camerasrc_control_set_value ( GstCameraSrc* camera_src, GstCameraControlChannel* control_channel, gint value ); +gboolean gst_camerasrc_control_get_value ( GstCameraSrc* camera_src, GstCameraControlChannel* control_channel, gint* value ); +gboolean gst_camerasrc_control_set_exposure ( GstCameraSrc* camera_src, gint type, gint value1, gint value2 ); +gboolean gst_camerasrc_control_get_exposure ( GstCameraSrc* camera_src, gint type, gint* value1, gint* value2 ); +gboolean gst_camerasrc_control_set_capture_mode ( GstCameraSrc* camera_src, gint type, gint value ); +gboolean gst_camerasrc_control_get_capture_mode ( GstCameraSrc* camera_src, gint type, gint* value ); +gboolean gst_camerasrc_control_set_strobe ( GstCameraSrc* camera_src, gint type, gint value ); +gboolean gst_camerasrc_control_get_strobe ( GstCameraSrc* camera_src, gint type, gint *value ); +gboolean gst_camerasrc_control_set_detect ( GstCameraSrc* camera_src, gint type, gint value ); +gboolean gst_camerasrc_control_get_detect ( GstCameraSrc* camera_src, gint type, gint* value ); +gboolean gst_camerasrc_control_set_zoom ( GstCameraSrc* camera_src, gint type, gint value ); +gboolean gst_camerasrc_control_get_zoom ( GstCameraSrc* camera_src, gint type, gint* value ); +gboolean gst_camerasrc_control_set_focus ( GstCameraSrc* camera_src, gint mode, gint range ); +gboolean gst_camerasrc_control_get_focus ( GstCameraSrc* camera_src, gint* mode, gint* range ); +gboolean gst_camerasrc_control_start_auto_focus ( GstCameraSrc* camera_src ); +gboolean gst_camerasrc_control_stop_auto_focus ( GstCameraSrc* camera_src ); +gboolean gst_camerasrc_control_set_focus_level ( GstCameraSrc* camera_src, gint manual_level ); +gboolean gst_camerasrc_control_get_focus_level ( GstCameraSrc* camera_src, gint* manual_level ); +gboolean gst_camerasrc_control_set_auto_focus_area( GstCameraSrc* camera_src, GstCameraControlRectType rect ); +gboolean gst_camerasrc_control_get_auto_focus_area( GstCameraSrc* camera_src, GstCameraControlRectType* rect ); +gboolean gst_camerasrc_control_set_wdr ( GstCameraSrc* camera_src, gint value ); +gboolean gst_camerasrc_control_get_wdr ( GstCameraSrc* camera_src, gint* value ); +gboolean gst_camerasrc_control_set_ahs ( GstCameraSrc* camera_src, gint value ); +gboolean gst_camerasrc_control_get_ahs ( GstCameraSrc* camera_src, gint* value ); +gboolean gst_camerasrc_control_set_part_color ( GstCameraSrc* camera_src, gint type, gint value ); +gboolean gst_camerasrc_control_get_part_color ( GstCameraSrc* camera_src, gint type, gint* value ); +gboolean gst_camerasrc_control_get_exif_info ( GstCameraSrc* camera_src, GstCameraControlExifInfo* info ); +gboolean gst_camerasrc_control_get_basic_dev_info ( GstCameraSrc* camera_src, gint dev_id, GstCameraControlCapsInfoType* info ); +gboolean gst_camerasrc_control_get_misc_dev_info ( GstCameraSrc* camera_src, gint dev_id, GstCameraControlCtrlListInfoType* info ); +gboolean gst_camerasrc_control_get_extra_dev_info ( GstCameraSrc* camera_src, gint dev_id, GstCameraControlExtraInfoType* info ); +void gst_camerasrc_control_set_capture_command( GstCameraSrc* camera_src, GstCameraControlCaptureCommand cmd ); + +#define GST_IMPLEMENT_CAMERASRC_CONTROL_METHODS(Type, interface_as_function) \ + \ +static const GList* \ +interface_as_function ## _control_list_channels( GstCameraControl* control ) \ +{ \ + Type* this = (Type*) control; \ + return gst_camerasrc_control_list_channels( this ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_value( GstCameraControl* control, \ + GstCameraControlChannel* control_channel, gint value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_value( this, control_channel, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_value( GstCameraControl* control, \ + GstCameraControlChannel* control_channel, gint* value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_value( this, control_channel, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_exposure( GstCameraControl* control, \ + gint type, gint value1, gint value2 ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_exposure( this, type, value1, value2 ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_exposure( GstCameraControl* control, \ + gint type, gint* value1, gint* value2 ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_exposure( this, type, value1, value2 ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_capture_mode( GstCameraControl* control, \ + gint type, gint value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_capture_mode( this, type, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_capture_mode( GstCameraControl* control, \ + gint type, gint* value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_capture_mode( this, type, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_strobe( GstCameraControl* control, \ + gint type, gint value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_strobe( this, type, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_strobe( GstCameraControl* control, \ + gint type, gint* value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_strobe( this, type, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_detect( GstCameraControl* control, \ + gint type, gint value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_detect( this, type, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_detect( GstCameraControl* control, \ + gint type, gint* value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_detect( this, type, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_zoom( GstCameraControl* control, \ + gint type, gint value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_zoom( this, type, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_zoom( GstCameraControl* control, \ + gint type, gint* value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_zoom( this, type, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_focus( GstCameraControl* control, \ + gint focus_mode, gint focus_range ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_focus( this, focus_mode, focus_range ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_focus( GstCameraControl* control, \ + gint* focus_mode, gint* focus_range ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_focus( this, focus_mode, focus_range ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_start_auto_focus( GstCameraControl* control) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_start_auto_focus( this ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_stop_auto_focus( GstCameraControl* control) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_stop_auto_focus( this ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_focus_level( GstCameraControl* control, \ + gint focus_level ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_focus_level( this, focus_level ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_focus_level( GstCameraControl* control, \ + gint* focus_level ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_focus_level( this, focus_level ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_auto_focus_area( GstCameraControl* control, \ + GstCameraControlRectType rect ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_auto_focus_area( this, rect ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_auto_focus_area( GstCameraControl* control, \ + GstCameraControlRectType* rect ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_auto_focus_area( this, rect ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_wdr( GstCameraControl* control, \ + gint value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_wdr( this, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_wdr( GstCameraControl* control, \ + gint* value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_wdr( this, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_ahs( GstCameraControl* control, \ + gint value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_ahs( this, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_ahs( GstCameraControl* control, \ + gint* value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_ahs( this, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_set_part_color( GstCameraControl* control, \ + gint type, gint value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_set_part_color( this, type, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_part_color( GstCameraControl* control, \ + gint type, gint* value ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_part_color( this, type, value ); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_exif_info( GstCameraControl* control, \ + GstCameraControlExifInfo* info ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_exif_info( this, info); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_basic_dev_info( GstCameraControl* control, \ + gint dev_id, \ + GstCameraControlCapsInfoType* info ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_basic_dev_info( this, dev_id, info); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_misc_dev_info( GstCameraControl* control, \ + gint dev_id, \ + GstCameraControlCtrlListInfoType* info ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_misc_dev_info( this, dev_id, info); \ +} \ + \ +static gboolean \ +interface_as_function ## _control_get_extra_dev_info( GstCameraControl* control, \ + gint dev_id, \ + GstCameraControlExtraInfoType* info ) \ +{ \ + Type* this = (Type*)control; \ + return gst_camerasrc_control_get_extra_dev_info( this, dev_id, info); \ +} \ + \ +static void \ +interface_as_function ## _control_set_capture_command( GstCameraControl* control, \ + GstCameraControlCaptureCommand cmd ) \ +{ \ + Type* this = (Type*)control; \ + gst_camerasrc_control_set_capture_command( this, cmd ); \ + return; \ +} \ + \ +void \ +interface_as_function ## _control_interface_init( GstCameraControlClass *klass ) \ +{ \ + GST_CAMERA_CONTROL_TYPE( klass ) = GST_CAMERA_CONTROL_HARDWARE; \ + \ + /* default virtual functions */ \ + klass->list_channels = interface_as_function ## _control_list_channels; \ + klass->set_value = interface_as_function ## _control_set_value; \ + klass->get_value = interface_as_function ## _control_get_value; \ + klass->set_exposure = interface_as_function ## _control_set_exposure; \ + klass->get_exposure = interface_as_function ## _control_get_exposure; \ + klass->set_capture_mode = interface_as_function ## _control_set_capture_mode; \ + klass->get_capture_mode = interface_as_function ## _control_get_capture_mode; \ + klass->set_strobe = interface_as_function ## _control_set_strobe; \ + klass->get_strobe = interface_as_function ## _control_get_strobe; \ + klass->set_detect = interface_as_function ## _control_set_detect; \ + klass->get_detect = interface_as_function ## _control_get_detect; \ + klass->set_zoom = interface_as_function ## _control_set_zoom; \ + klass->get_zoom = interface_as_function ## _control_get_zoom; \ + klass->set_focus = interface_as_function ## _control_set_focus; \ + klass->get_focus = interface_as_function ## _control_get_focus; \ + klass->start_auto_focus = interface_as_function ## _control_start_auto_focus; \ + klass->stop_auto_focus = interface_as_function ## _control_stop_auto_focus; \ + klass->set_focus_level = interface_as_function ## _control_set_focus_level; \ + klass->get_focus_level = interface_as_function ## _control_get_focus_level; \ + klass->set_auto_focus_area = interface_as_function ## _control_set_auto_focus_area; \ + klass->get_auto_focus_area = interface_as_function ## _control_get_auto_focus_area; \ + klass->set_wdr = interface_as_function ## _control_set_wdr; \ + klass->get_wdr = interface_as_function ## _control_get_wdr; \ + klass->set_ahs = interface_as_function ## _control_set_ahs; \ + klass->get_ahs = interface_as_function ## _control_get_ahs; \ + klass->set_part_color = interface_as_function ## _control_set_part_color; \ + klass->get_part_color = interface_as_function ## _control_get_part_color; \ + klass->get_exif_info = interface_as_function ## _control_get_exif_info; \ + klass->get_basic_dev_info = interface_as_function ## _control_get_basic_dev_info; \ + klass->get_misc_dev_info = interface_as_function ## _control_get_misc_dev_info; \ + klass->get_extra_dev_info = interface_as_function ## _control_get_extra_dev_info; \ + klass->set_capture_command = interface_as_function ## _control_set_capture_command; \ + \ +} + +#endif /* __GST_CAMERASRC_CONTROL_H__ */ |