Add preview callback information for debug
[platform/core/api/camera.git] / src / camera.c
index f53286b..a0a07e9 100644 (file)
                        return CAMERA_ERROR_NOT_SUPPORTED;\
                }\
        } while (0)
-#define CAMERA_CHECK_HANDLE_RETURN(pc) \
-       do {\
-               if (!pc || !pc->cb_info) {\
-                       CAM_LOG_ERROR("NULL handle[%p]", pc);\
-                       return;\
-               }\
-       } while (0)
-#define CAMERA_CHECK_HANDLE_RETURN_VAL(pc, val) \
-       do {\
-               if (!pc || !pc->cb_info) {\
-                       CAM_LOG_ERROR("NULL handle[%p]", pc);\
-                       return (val);\
-               }\
-       } while (0)
+
 
 /* for camera device manager */
 typedef struct _cdm_symbol_table {
@@ -287,6 +274,88 @@ _DONE:
 }
 
 
+static void __camera_preview_cb_monitoring_info_sub_reset(monitoring_info_sub_s *info_sub)
+{
+       if (!info_sub) {
+               CAM_LOG_ERROR("NULL info_sub");
+               return;
+       }
+
+       info_sub->frame_count = 0;
+       info_sub->elapsed_sec_accum = 0.0;
+       info_sub->elapsed_sec_peak = 0.0;
+}
+
+
+static void __camera_preview_cb_monitoring_info_reset(camera_preview_cb_monitoring_info_s *info)
+{
+       if (!info) {
+               CAM_LOG_ERROR("NULL info");
+               return;
+       }
+
+       __camera_preview_cb_monitoring_info_sub_reset(info->total);
+       __camera_preview_cb_monitoring_info_sub_reset(info->interval);
+}
+
+
+static void __camera_preview_cb_monitoring_info_start(camera_preview_cb_monitoring_info_s *info)
+{
+       if (!info) {
+               CAM_LOG_ERROR("NULL info");
+               return;
+       }
+
+       g_timer_start(info->timer_calling);
+
+       if (info->interval->frame_count == 0)
+               g_timer_start(info->timer_interval);
+}
+
+
+static void __camera_preview_cb_monitoring_info_sub_update(monitoring_info_sub_s *info_sub, gdouble elapsed_sec)
+{
+       if (!info_sub) {
+               CAM_LOG_ERROR("NULL info_sub");
+               return;
+       }
+
+       info_sub->frame_count++;
+       info_sub->elapsed_sec_accum += elapsed_sec;
+
+       if (info_sub->elapsed_sec_peak < elapsed_sec)
+               info_sub->elapsed_sec_peak = elapsed_sec;
+}
+
+
+static void __camera_preview_cb_monitoring_info_end(camera_preview_cb_monitoring_info_s *info)
+{
+       gdouble elapsed_sec = 0.0;
+       gdouble elapsed_sec_interval = 0.0;
+
+       if (!info) {
+               CAM_LOG_ERROR("NULL info");
+               return;
+       }
+
+       elapsed_sec = g_timer_elapsed(info->timer_calling, NULL);
+       elapsed_sec_interval = g_timer_elapsed(info->timer_interval, NULL);
+
+       __camera_preview_cb_monitoring_info_sub_update(info->total, elapsed_sec);
+       __camera_preview_cb_monitoring_info_sub_update(info->interval, elapsed_sec);
+
+       if (elapsed_sec_interval >= 1.0) {
+               CAM_LOG_INFO("id[%d], frame count[t:%llu,i:%llu], elapsed - avg[t:%dms,i:%dms], peak[t:%dms,i:%dms]",
+                       info->stream_id, info->total->frame_count, info->interval->frame_count,
+                       (int)((info->total->elapsed_sec_accum / (gdouble)info->total->frame_count) * 1000),
+                       (int)((info->interval->elapsed_sec_accum / (gdouble)info->interval->frame_count) * 1000),
+                       (int)(info->total->elapsed_sec_peak * 1000), (int)(info->interval->elapsed_sec_peak * 1000));
+
+               __camera_preview_cb_monitoring_info_sub_reset(info->interval);
+       }
+}
+
+
 static void __camera_event_handler_preview(camera_cb_info_s *cb_info, char *recv_msg, int *tfd)
 {
        int i = 0;
@@ -308,6 +377,7 @@ static void __camera_event_handler_preview(camera_cb_info_s *cb_info, char *recv
        MMCamcorderVideoStreamDataType *stream = NULL;
        camera_media_packet_data *mp_data = NULL;
        media_packet_h pkt = NULL;
+       camera_preview_cb_monitoring_info_s *monitoring_info = NULL;
 
        /* tfd[0]: MMCamcorderVideoStreamDataType
           tfd[1]: data_bo or zero copy bo[0]
@@ -334,6 +404,11 @@ static void __camera_event_handler_preview(camera_cb_info_s *cb_info, char *recv
        ret_fd = preview_fd;
        CAMERA_MSG_PARAM_SET(param, INT, ret_fd);
 
+       if (cb_info->invoke_preview_cb == FALSE) {
+               CAM_LOG_WARNING("Skip preview callback for fd[%d] due to preview stop.", ret_fd);
+               goto _PREVIEW_CB_HANDLER_DONE;
+       }
+
        if (num_buffer_fd < 0 || num_buffer_fd > BUFFER_MAX_PLANE_NUM) {
                CAM_LOG_ERROR("invalid num buffer fd %d", num_buffer_fd);
                goto _PREVIEW_CB_HANDLER_DONE;
@@ -381,15 +456,23 @@ static void __camera_event_handler_preview(camera_cb_info_s *cb_info, char *recv
                cb_info->stream_data = stream;
 
                if (stream->extra_stream_id < 0) {
+                       monitoring_info = cb_info->monitoring_info_preview;
+                       __camera_preview_cb_monitoring_info_start(monitoring_info);
+
                        if (cb_info->user_cb[MUSE_CAMERA_EVENT_TYPE_PREVIEW])
                                ((camera_preview_cb)cb_info->user_cb[MUSE_CAMERA_EVENT_TYPE_PREVIEW])(&frame,
                                        cb_info->user_data[MUSE_CAMERA_EVENT_TYPE_PREVIEW]);
                } else {
+                       monitoring_info = cb_info->monitoring_info_preview_ex[stream->extra_stream_id];
+                       __camera_preview_cb_monitoring_info_start(monitoring_info);
+
                        if (cb_info->user_cb[MUSE_CAMERA_EVENT_TYPE_EXTRA_PREVIEW])
                                ((camera_extra_preview_cb)cb_info->user_cb[MUSE_CAMERA_EVENT_TYPE_EXTRA_PREVIEW])(&frame,
                                        stream->extra_stream_id, cb_info->user_data[MUSE_CAMERA_EVENT_TYPE_EXTRA_PREVIEW]);
                }
 
+               __camera_preview_cb_monitoring_info_end(monitoring_info);
+
                cb_info->stream_data = NULL;
        }
 
@@ -422,8 +505,14 @@ static void __camera_event_handler_preview(camera_cb_info_s *cb_info, char *recv
        /* 1. media packet preview callback */
        if (cb_info->user_cb[MUSE_CAMERA_EVENT_TYPE_MEDIA_PACKET_PREVIEW]) {
                media_packet_ref(pkt);
+
+               monitoring_info = cb_info->monitoring_info_preview_mp;
+               __camera_preview_cb_monitoring_info_start(monitoring_info);
+
                ((camera_media_packet_preview_cb)cb_info->user_cb[MUSE_CAMERA_EVENT_TYPE_MEDIA_PACKET_PREVIEW])(pkt,
                        cb_info->user_data[MUSE_CAMERA_EVENT_TYPE_MEDIA_PACKET_PREVIEW]);
+
+               __camera_preview_cb_monitoring_info_end(monitoring_info);
        }
 
        /* 2. call evas renderer */
@@ -760,7 +849,7 @@ static int __set_enable(camera_h camera, muse_camera_api_e api, int set_enable)
 }
 
 
-static void __send_message_get_return(camera_cb_info_s *cb_info, muse_camera_api_e api, char *msg, int time_out, int *ret)
+void _camera_send_message_get_return(camera_cb_info_s *cb_info, muse_camera_api_e api, char *msg, int time_out, int *ret)
 {
        int send_ret = 0;
 
@@ -772,7 +861,10 @@ static void __send_message_get_return(camera_cb_info_s *cb_info, muse_camera_api
                return;
        }
 
-       CAM_LOG_INFO("api[%d], message[%s]", api, msg);
+       if (api != MUSE_CAMERA_API_RETURN_BUFFER && api != MUSE_CAMERA_API_PREVIEW_CB_RETURN)
+               CAM_LOG_INFO("api[%d], message[%s]", api, msg);
+       else
+               CAM_LOG_DEBUG("api[%d], message[%s]", api, msg);
 
        if (cb_info->is_server_connected) {
                _camera_update_api_waiting(cb_info, api, 1);
@@ -795,7 +887,10 @@ static void __send_message_get_return(camera_cb_info_s *cb_info, muse_camera_api
 
        muse_core_msg_free(msg);
 
-       CAM_LOG_INFO("api[%d] ret[0x%x]", api, ret ? *ret : 0x0);
+       if (api != MUSE_CAMERA_API_RETURN_BUFFER && api != MUSE_CAMERA_API_PREVIEW_CB_RETURN)
+               CAM_LOG_INFO("api[%d] ret[0x%x]", api, ret ? *ret : 0x0);
+       else
+               CAM_LOG_DEBUG("api[%d] ret[0x%x]", api, ret ? *ret : 0x0);
 }
 
 
@@ -863,7 +958,7 @@ void _camera_msg_send(int api, int *fds, camera_cb_info_s *cb_info,
 
        msg = muse_core_msg_new(api, NULL);
 
-       __send_message_get_return(cb_info, api, msg, timeout, ret);
+       _camera_send_message_get_return(cb_info, api, msg, timeout, ret);
 }
 
 
@@ -908,7 +1003,7 @@ void _camera_msg_send_param1(int api, camera_cb_info_s *cb_info,
                break;
        }
 
-       __send_message_get_return(cb_info, api, msg, timeout, ret);
+       _camera_send_message_get_return(cb_info, api, msg, timeout, ret);
 }
 
 
@@ -937,7 +1032,7 @@ void _camera_msg_send_param2_int(int api, camera_cb_info_s *cb_info,
                param1->type, param1->name, param1->value.value_INT,
                NULL);
 
-       __send_message_get_return(cb_info, api, msg, timeout, ret);
+       _camera_send_message_get_return(cb_info, api, msg, timeout, ret);
 }
 
 
@@ -1288,7 +1383,7 @@ static int __camera_update_media_packet_format(camera_cb_info_s *cb_info, MMCamc
                media_format_get_video_info(cb_info->pkt_fmt,
                        &pkt_fmt_mimetype, &pkt_fmt_width, &pkt_fmt_height, NULL, NULL);
 
-               CAM_LOG_INFO("pkt_fmt %dx%d - stream %dx%d",
+               CAM_LOG_DEBUG("pkt_fmt %dx%d - stream %dx%d",
                        pkt_fmt_width, pkt_fmt_height, stream->width, stream->height);
 
                if (pkt_fmt_mimetype != mimetype ||
@@ -1351,6 +1446,16 @@ static int __camera_create_media_packet(camera_cb_info_s *cb_info, MMCamcorderVi
                        stream->data.encoded.data, stream->data.encoded.length_data,
                        (media_packet_dispose_cb)_camera_media_packet_dispose, (void *)cb_info,
                        &pkt);
+
+               if (pkt) {
+                       if (!stream->data.encoded.is_delta_frame)
+                               media_packet_set_flags(pkt, MEDIA_PACKET_SYNC_FRAME);
+
+                       if (stream->data.encoded.is_header_included) {
+                               media_packet_set_flags(pkt, MEDIA_PACKET_CODEC_CONFIG);
+                               CAM_LOG_INFO("Codec config in buffer");
+                       }
+               }
        } else {
                tsurf = __camera_get_tbm_surface(stream, mp_data);
                if (!tsurf) {
@@ -1375,8 +1480,7 @@ static int __camera_create_media_packet(camera_cb_info_s *cb_info, MMCamcorderVi
                goto _PACKET_CREATE_FAILED;
        }
 
-       /* set timestamp : msec -> nsec */
-       if (media_packet_set_pts(pkt, (uint64_t)(stream->timestamp) * 1000000) != MEDIA_PACKET_ERROR_NONE)
+       if (media_packet_set_pts(pkt, stream->timestamp_nsec) != MEDIA_PACKET_ERROR_NONE)
                CAM_LOG_WARNING("media_packet_set_pts failed");
 
        if (media_packet_set_rotate_method(pkt, (media_packet_rotate_method_e)stream->rotation) != MEDIA_PACKET_ERROR_NONE)
@@ -2277,10 +2381,42 @@ static void __camera_mutex_cond_clear(camera_cb_info_s *cb_info)
 }
 
 
+static camera_preview_cb_monitoring_info_s *__camera_preview_cb_monitoring_info_create(int stream_id)
+{
+       camera_preview_cb_monitoring_info_s *new_info = g_new0(camera_preview_cb_monitoring_info_s, 1);
+
+       new_info->stream_id = stream_id;
+       new_info->total = g_new0(monitoring_info_sub_s, 1);
+       new_info->interval = g_new0(monitoring_info_sub_s, 1);
+       new_info->timer_calling = g_timer_new();
+       new_info->timer_interval = g_timer_new();
+
+       CAM_LOG_INFO("create preview cb monitoring info[%p]", new_info);
+
+       return new_info;
+}
+
+
+static void __camera_preview_cb_monitoring_info_destroy(camera_preview_cb_monitoring_info_s *info)
+{
+       if (!info)
+               return;
+
+       g_timer_destroy(info->timer_calling);
+       g_timer_destroy(info->timer_interval);
+       g_free(info->total);
+       g_free(info->interval);
+
+       CAM_LOG_INFO("destroy preview cb monitoring info[%p]", info);
+
+       g_free(info);
+}
+
+
 static camera_cb_info_s *__camera_client_callback_new(gint sockfd)
 {
+       int i = 0;
        camera_cb_info_s *cb_info = NULL;
-       gint i = 0;
 
        g_return_val_if_fail(sockfd > 0, NULL);
 
@@ -2325,6 +2461,11 @@ static camera_cb_info_s *__camera_client_callback_new(gint sockfd)
 
        cb_info->is_server_connected = TRUE;
 
+       cb_info->monitoring_info_preview = __camera_preview_cb_monitoring_info_create(CAMERA_MONITORING_INFO_STREAM_ID_PREVIEW);
+       cb_info->monitoring_info_preview_mp = __camera_preview_cb_monitoring_info_create(CAMERA_MONITORING_INFO_STREAM_ID_PREVIEW_MP);
+       for (i = 0 ; i < MM_CAMCORDER_EXTRA_PREVIEW_STREAM_MAX ; i++)
+               cb_info->monitoring_info_preview_ex[i] = __camera_preview_cb_monitoring_info_create(i);
+
        return cb_info;
 //LCOV_EXCL_START
 ErrorExit:
@@ -2345,6 +2486,8 @@ ErrorExit:
 
 static void __camera_client_callback_destroy(camera_cb_info_s *cb_info)
 {
+       int i = 0;
+
        if (!cb_info) {
                CAM_LOG_ERROR("NULL cb_info");
                return;
@@ -2383,6 +2526,11 @@ static void __camera_client_callback_destroy(camera_cb_info_s *cb_info)
                cb_info->dp_interface = NULL;
        }
 
+       __camera_preview_cb_monitoring_info_destroy(cb_info->monitoring_info_preview);
+       __camera_preview_cb_monitoring_info_destroy(cb_info->monitoring_info_preview_mp);
+       for (i = 0 ; i < MM_CAMCORDER_EXTRA_PREVIEW_STREAM_MAX ; i++)
+               __camera_preview_cb_monitoring_info_destroy(cb_info->monitoring_info_preview_ex[i]);
+
        g_free(cb_info);
 }
 
@@ -2736,6 +2884,7 @@ int camera_destroy(camera_h camera)
 
 int camera_start_preview(camera_h camera)
 {
+       int i = 0;
        int ret = CAMERA_ERROR_NONE;
        int *fds = NULL;
        muse_camera_api_e api = MUSE_CAMERA_API_START_PREVIEW;
@@ -2761,6 +2910,11 @@ int camera_start_preview(camera_h camera)
        }
 
        if (current_state == CAMERA_STATE_CREATED) {
+               __camera_preview_cb_monitoring_info_reset(pc->cb_info->monitoring_info_preview);
+               __camera_preview_cb_monitoring_info_reset(pc->cb_info->monitoring_info_preview_mp);
+               for (i = 0 ; i < MM_CAMCORDER_EXTRA_PREVIEW_STREAM_MAX ; i++)
+                       __camera_preview_cb_monitoring_info_reset(pc->cb_info->monitoring_info_preview_ex[i]);
+
                if (!__create_msg_handler_thread(&pc->cb_info->preview_cb_info,
                                CAMERA_MESSAGE_HANDLER_TYPE_PREVIEW_CB, "cam:preview_cb", pc->cb_info)) {
                        CAM_LOG_ERROR("preview_cb_info failed");
@@ -2777,11 +2931,13 @@ int camera_start_preview(camera_h camera)
                }
        }
 
+       pc->cb_info->invoke_preview_cb = TRUE;
+
        _camera_msg_send(api, fds, pc->cb_info, &ret,
 #ifdef TIZEN_FEATURE_NO_TIMEOUT_FOR_PREVIEW
                CAMERA_CB_NO_TIMEOUT);
 #else
-               (pc->cb_info->is_network ? CAMERA_CB_NETWORK_PREVIEW_TIMEOUT : CAMERA_CB_TIMEOUT));
+               CAMERA_CB_TIMEOUT_FOR_PREVIEW);
 #endif
        if (ret != CAMERA_ERROR_NONE)
                goto _START_FAILED;
@@ -2839,7 +2995,9 @@ int camera_stop_preview(camera_h camera)
                }
        }
 //LCOV_EXCL_STOP
-       /* send stop preview message */
+
+       pc->cb_info->invoke_preview_cb = FALSE;
+
        _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
 
        if (ret == CAMERA_ERROR_NONE) {
@@ -4786,7 +4944,7 @@ int camera_attr_set_geotag(camera_h camera, double latitude, double longitude, d
                MUSE_TYPE_ARRAY, "set_geotag", length, (int *)set_geotag,
                NULL);
 
-       __send_message_get_return(pc->cb_info, api, msg, CAMERA_CB_TIMEOUT, &ret);
+       _camera_send_message_get_return(pc->cb_info, api, msg, CAMERA_CB_TIMEOUT, &ret);
 
        return ret;
 }
@@ -6195,7 +6353,7 @@ int camera_attr_set_display_roi_area(camera_h camera, int x, int y, int width, i
                MUSE_TYPE_ARRAY, "set_display_roi_area", length, (int *)set_display_roi_area,
                NULL);
 
-       __send_message_get_return(pc->cb_info, api, msg, CAMERA_CB_TIMEOUT, &ret);
+       _camera_send_message_get_return(pc->cb_info, api, msg, CAMERA_CB_TIMEOUT, &ret);
 
        return ret;
 }
@@ -6483,7 +6641,7 @@ int camera_device_manager_initialize(camera_device_manager_h *manager)
        unsigned int i = 0;
        int ret = CAMERA_ERROR_NONE;
        void *dl_handle = NULL;
-       camera_device_manager *new_manager = NULL;
+       g_autofree camera_device_manager *new_manager = g_new0(camera_device_manager, 1);
        cdm_symbol_table sym_table[] = {
                {(void **)&new_manager->initialize, "cdm_initialize"},
                {(void **)&new_manager->deinitialize, "cdm_deinitialize"},
@@ -6494,8 +6652,6 @@ int camera_device_manager_initialize(camera_device_manager_h *manager)
 
        CAMERA_CHECK_DEVICE_MANAGER;
 
-       new_manager = g_new0(camera_device_manager, 1);
-
        if (!manager) {
                CAM_LOG_ERROR("NULL manager");
                ret = CAMERA_ERROR_INVALID_PARAMETER;
@@ -6526,17 +6682,17 @@ int camera_device_manager_initialize(camera_device_manager_h *manager)
        }
 
        new_manager->dl_handle = dl_handle;
-       *manager = (camera_device_manager_h)new_manager;
+       *manager = (camera_device_manager_h)g_steal_pointer(&new_manager);
 
        CAM_LOG_INFO("camera device manager[%p](dl handle[%p]) initialized",
-               new_manager, dl_handle);
+               *manager, dl_handle);
 
        return CAMERA_ERROR_NONE;
 
 _INITIALIZE_FAILED:
        if (dl_handle)
                dlclose(dl_handle);
-       g_free(new_manager);
+
        return ret;
 }
 
@@ -6759,7 +6915,7 @@ int camera_set_extra_preview_stream_format(camera_h camera, int stream_id, camer
                MUSE_TYPE_ARRAY, "stream_format", 4, stream_format,
                NULL);
 
-       __send_message_get_return(pc->cb_info, api, msg, CAMERA_CB_TIMEOUT, &ret);
+       _camera_send_message_get_return(pc->cb_info, api, msg, CAMERA_CB_TIMEOUT, &ret);
 
        return ret;
 }