Add error handling for getting gst_caps
[platform/core/multimedia/libmm-imgp-gstcs.git] / gstcs / mm_util_gstcs.c
old mode 100755 (executable)
new mode 100644 (file)
index 215e4c2..f1a0344
@@ -26,9 +26,8 @@
 #define MM_UTIL_ROUND_UP_2(num) (((num)+1)&~1)
 #define MM_UTIL_ROUND_UP_4(num) (((num)+3)&~3)
 #define MM_UTIL_ROUND_UP_8(num) (((num)+7)&~7)
-#define MM_UTIL_ROUND_UP_16(num) (((num)+15)&~15)
 
-#define SAFE_STRCPY(dst, src, n)       g_strlcpy(dst, src, n)
+#define SAFE_UNREF_CAPS(caps)  { if (caps)     { gst_caps_unref(caps); caps = NULL; } }
 
 
 static GstFlowReturn
@@ -46,7 +45,7 @@ _mm_sink_sample(GstElement * appsink, gpointer user_data)
                if (pGstreamer_s->output_buffer != NULL) {
                        GstMapInfo mapinfo = GST_MAP_INFO_INIT;
                        gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
-                       gstcs_debug("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %d", mapinfo.data, mapinfo.size);
+                       gstcs_debug("Create Output Buffer: GST_BUFFER_DATA: %p\t GST_BUFFER_SIZE: %zu", mapinfo.data, mapinfo.size);
                        gst_buffer_unmap(pGstreamer_s->output_buffer, &mapinfo);
                } else {
                        gstcs_error("ERROR -Input Prepare Buffer! Check createoutput buffer function");
@@ -132,32 +131,37 @@ static int _mm_create_pipeline(gstreamer_s* pGstreamer_s)
        pGstreamer_s->pipeline = gst_pipeline_new("pipeline");
        if (!pGstreamer_s->pipeline) {
                gstcs_error("pipeline could not be created. Exiting.\n");
-               ret = GSTCS_ERROR_INVALID_PARAMETER;
+               return GSTCS_ERROR_INVALID_PARAMETER;
        }
        pGstreamer_s->appsrc = gst_element_factory_make("appsrc" , "appsrc");
        if (!pGstreamer_s->appsrc) {
                gstcs_error("appsrc could not be created. Exiting.\n");
-               ret = GSTCS_ERROR_INVALID_PARAMETER;
+               gst_object_unref(pGstreamer_s->pipeline);
+               return GSTCS_ERROR_INVALID_PARAMETER;
        }
        pGstreamer_s->colorspace = gst_element_factory_make("videoconvert" , "convert");
        if (!pGstreamer_s->colorspace) {
                gstcs_error("colorspace could not be created. Exiting.\n");
-               ret = GSTCS_ERROR_INVALID_PARAMETER;
+               gst_object_unref(pGstreamer_s->pipeline);
+               return GSTCS_ERROR_INVALID_PARAMETER;
        }
        pGstreamer_s->videoscale = gst_element_factory_make("videoscale", "scale");
        if (!pGstreamer_s->videoscale) {
                gstcs_error("videoscale could not be created. Exiting.\n");
-               ret = GSTCS_ERROR_INVALID_PARAMETER;
+               gst_object_unref(pGstreamer_s->pipeline);
+               return GSTCS_ERROR_INVALID_PARAMETER;
        }
        pGstreamer_s->videoflip = gst_element_factory_make("videoflip", "flip");
        if (!pGstreamer_s->videoflip) {
                gstcs_error("videoflip could not be created. Exiting.\n");
-               ret = GSTCS_ERROR_INVALID_PARAMETER;
+               gst_object_unref(pGstreamer_s->pipeline);
+               return GSTCS_ERROR_INVALID_PARAMETER;
        }
        pGstreamer_s->appsink = gst_element_factory_make("appsink" , "appsink");
        if (!pGstreamer_s->appsink) {
                gstcs_error("appsink could not be created. Exiting.\n");
-               ret = GSTCS_ERROR_INVALID_PARAMETER;
+               gst_object_unref(pGstreamer_s->pipeline);
+               return GSTCS_ERROR_INVALID_PARAMETER;
        }
        return ret;
 }
@@ -274,6 +278,7 @@ static GstCaps* _mm_get_capabilities(mm_util_color_format_e color_format, unsign
        GstVideoFormat videoFormat = GST_VIDEO_FORMAT_UNKNOWN;
 
        videoFormat = _mm_get_video_format(color_format);
+       gstcs_retvm_if(videoFormat == GST_VIDEO_FORMAT_UNKNOWN, NULL, "Unkown video format (%d)", color_format);
 
        caps = gst_caps_new_simple("video/x-raw",
                        "format", G_TYPE_STRING, gst_video_format_to_string(videoFormat),
@@ -324,9 +329,9 @@ static void _mm_get_round_up_width_height(mm_util_color_format_e color_format, u
        gstcs_debug("color_format[%d] width[%u] height[%u] stride[%u], elevation[%u]", color_format, width, height, *stride, *elevation);
 }
 
-static int _mm_setup_image_size(mm_util_color_format_e color_format, unsigned int width, unsigned int height)
+static size_t _mm_setup_image_size(mm_util_color_format_e color_format, unsigned int width, unsigned int height)
 {
-       unsigned int size = 0;
+       size_t size = 0;
 
        gstcs_debug("color_format [%d] width [%u] height [%u]", color_format, width, height);
 
@@ -367,7 +372,7 @@ static int _mm_setup_image_size(mm_util_color_format_e color_format, unsigned in
        }
 
 
-       gstcs_debug("Image size [%u]", size);
+       gstcs_debug("Image size [%zu]", size);
 
        return size;
 }
@@ -375,7 +380,7 @@ static int _mm_setup_image_size(mm_util_color_format_e color_format, unsigned in
 static int _mm_push_buffer_into_pipeline(imgp_info_s* pImgp_info, unsigned char *src, gstreamer_s * pGstreamer_s)
 {
        int ret = GSTCS_ERROR_NONE;
-       gsize data_size = 0;
+       size_t data_size = 0;
        GstBuffer* gst_buf = NULL;
 
        gstcs_fenter();
@@ -398,7 +403,7 @@ static int _mm_push_buffer_into_pipeline_new(unsigned char *src, gstreamer_s * p
 {
        int ret = GSTCS_ERROR_NONE;
        GstBuffer *gst_buf = NULL;
-       unsigned int src_size = 0;
+       size_t src_size = 0;
        unsigned char *data = NULL;
 
        gstcs_fenter();
@@ -457,8 +462,10 @@ static int _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *s
 
        /*create pipeline*/
        ret = _mm_create_pipeline(pGstreamer_s);
-       if (ret != GSTCS_ERROR_NONE)
+       if (ret != GSTCS_ERROR_NONE) {
                gstcs_error("ERROR - mm_create_pipeline ");
+               return ret;
+       }
 
        /* Make appsink emit the "new-preroll" and "new-sample" signals. This option is by default disabled because signal emission is expensive and unneeded when the application prefers to operate in pull mode. */
        gst_app_sink_set_emit_signals((GstAppSink*)pGstreamer_s->appsink, TRUE);
@@ -472,6 +479,13 @@ static int _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *s
 
        src_caps = _mm_get_capabilities(pImgp_info->src_format, src_stride, src_elevation);
        dst_caps = _mm_get_capabilities(pImgp_info->dst_format, pImgp_info->output_stride, pImgp_info->output_elevation);
+       if (src_caps == NULL || dst_caps == NULL) {
+               gstcs_error("ERROR - _mm_get_capabilities ");
+               SAFE_UNREF_CAPS(src_caps);
+               SAFE_UNREF_CAPS(dst_caps);
+               gst_object_unref(pGstreamer_s->pipeline);
+               return ret;
+       }
 
        gst_app_src_set_caps(GST_APP_SRC(pGstreamer_s->appsrc), src_caps);
        gst_app_sink_set_caps(GST_APP_SINK(pGstreamer_s->appsink), dst_caps);
@@ -484,10 +498,8 @@ static int _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *s
 
        if (ret != GSTCS_ERROR_NONE) {
                gstcs_error("ERROR - mm_push_buffer_into_pipeline ");
-               if (src_caps)
-                       gst_caps_unref(src_caps);
-               if (dst_caps)
-                       gst_caps_unref(dst_caps);
+               SAFE_UNREF_CAPS(src_caps);
+               SAFE_UNREF_CAPS(dst_caps);
                gst_object_unref(pGstreamer_s->pipeline);
                return ret;
        }
@@ -526,10 +538,8 @@ static int _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *s
 
        gstcs_debug("Success gst_element_get_state\n");
 
-       if (src_caps)
-               gst_caps_unref(src_caps);
-       if (dst_caps)
-               gst_caps_unref(dst_caps);
+       SAFE_UNREF_CAPS(src_caps);
+       SAFE_UNREF_CAPS(dst_caps);
 
        if (ret_state == GST_STATE_CHANGE_FAILURE) {
                gstcs_error("GST_STATE_CHANGE_FAILURE");
@@ -537,12 +547,12 @@ static int _mm_imgp_gstcs_processing(gstreamer_s* pGstreamer_s, unsigned char *s
                if (pGstreamer_s->output_buffer != NULL) {
                        GstMapInfo mapinfo = GST_MAP_INFO_INIT;
                        gst_buffer_map(pGstreamer_s->output_buffer, &mapinfo, GST_MAP_READ);
-                       int buffer_size = mapinfo.size;
-                       int calc_buffer_size = 0;
+                       size_t buffer_size = mapinfo.size;
+                       size_t calc_buffer_size = 0;
 
                        calc_buffer_size = _mm_setup_image_size(pImgp_info->dst_format, pImgp_info->output_stride, pImgp_info->output_elevation);
 
-                       gstcs_debug("buffer size: %d, calc: %d\n", buffer_size, calc_buffer_size);
+                       gstcs_debug("buffer size[%zu], calc[%zu]", buffer_size, calc_buffer_size);
                        if (buffer_size != calc_buffer_size) {
                                gstcs_debug("Buffer size is different \n");
                                gstcs_debug("unref output buffer");