Always set internal buffer
[platform/core/multimedia/libmm-camcorder.git] / src / mm_camcorder_gstcommon.c
old mode 100755 (executable)
new mode 100644 (file)
index 6e708b2..306f0db
@@ -156,23 +156,34 @@ static guint32 _mmcamcorder_convert_fourcc_string_to_value(const gchar* format_n
 static bool __mmcamcorder_find_max_resolution(MMHandleType handle, gint *max_width, gint *max_height);
 #endif /* _MMCAMCORDER_PRODUCT_TV */
 
-static gboolean __mmcamcorder_set_stream_data_tbm(MMCamcorderVideoStreamDataType *stream, tbm_surface_info_s *ts_info);
-static gboolean __mmcamcorder_set_stream_data_normal(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMapInfo *map_info);
+static gboolean __mmcamcorder_set_stream_data(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMemory *memory);
+static gboolean __mmcamcorder_set_stream_data_zero_copy(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMemory *memory);
 
 /*=======================================================================================
 |  FUNCTION DEFINITIONS                                                                 |
 =======================================================================================*/
-static gboolean __mmcamcorder_set_stream_data_normal(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMapInfo *map_info)
+static gboolean __mmcamcorder_set_stream_data(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMemory *memory)
 {
+       gboolean ret = TRUE;
+       GstMapInfo map_info;
+
        mmf_return_val_if_fail(buffer, FALSE);
-       mmf_return_val_if_fail(map_info, FALSE);
        mmf_return_val_if_fail(stream, FALSE);
 
+       memset(&map_info, 0x0, sizeof(GstMapInfo));
+
+       if (!gst_memory_map(memory, &map_info, GST_MAP_READWRITE)) {
+               MMCAM_LOG_ERROR("map failed for memory[%p]", memory);
+               return FALSE;
+       }
+
+       stream->length_total = gst_memory_get_sizes(memory, NULL, NULL);
+
        switch (stream->format) {
        case MM_PIXEL_FORMAT_NV12: /* fall through */
        case MM_PIXEL_FORMAT_NV21:
                stream->data_type = MM_CAM_STREAM_DATA_YUV420SP;
-               stream->data.yuv420sp.y = map_info->data;
+               stream->data.yuv420sp.y = map_info.data;
                stream->data.yuv420sp.length_y = stream->width * stream->height;
                stream->data.yuv420sp.uv = stream->data.yuv420sp.y + stream->data.yuv420sp.length_y;
                stream->data.yuv420sp.length_uv = stream->data.yuv420sp.length_y >> 1;
@@ -185,7 +196,7 @@ static gboolean __mmcamcorder_set_stream_data_normal(MMCamcorderVideoStreamDataT
 
        case MM_PIXEL_FORMAT_I420:
                stream->data_type = MM_CAM_STREAM_DATA_YUV420P;
-               stream->data.yuv420p.y = map_info->data;
+               stream->data.yuv420p.y = map_info.data;
                stream->data.yuv420p.length_y = stream->width * stream->height;
                stream->data.yuv420p.u = stream->data.yuv420p.y + stream->data.yuv420p.length_y;
                stream->data.yuv420p.length_u = stream->data.yuv420p.length_y >> 2;
@@ -200,7 +211,7 @@ static gboolean __mmcamcorder_set_stream_data_normal(MMCamcorderVideoStreamDataT
 
        case MM_PIXEL_FORMAT_422P:
                stream->data_type = MM_CAM_STREAM_DATA_YUV422P;
-               stream->data.yuv422p.y = map_info->data;
+               stream->data.yuv422p.y = map_info.data;
                stream->data.yuv422p.length_y = stream->width * stream->height;
                stream->data.yuv422p.u = stream->data.yuv422p.y + stream->data.yuv422p.length_y;
                stream->data.yuv422p.length_u = stream->data.yuv422p.length_y >> 1;
@@ -217,7 +228,7 @@ static gboolean __mmcamcorder_set_stream_data_normal(MMCamcorderVideoStreamDataT
        case MM_PIXEL_FORMAT_UYVY: /* fall through */
        case MM_PIXEL_FORMAT_ITLV_JPEG_UYVY:
                stream->data_type = MM_CAM_STREAM_DATA_YUV422;
-               stream->data.yuv422.yuv = map_info->data;
+               stream->data.yuv422.yuv = map_info.data;
                stream->data.yuv422.length_yuv = stream->length_total;
                stream->stride[0] = stream->width << 1;
                stream->elevation[0] = stream->height;
@@ -229,7 +240,7 @@ static gboolean __mmcamcorder_set_stream_data_normal(MMCamcorderVideoStreamDataT
        case MM_PIXEL_FORMAT_ENCODED_VP8:   /* fall through */
        case MM_PIXEL_FORMAT_ENCODED_VP9:   /* fall through */
                stream->data_type = MM_CAM_STREAM_DATA_ENCODED;
-               stream->data.encoded.data = map_info->data;
+               stream->data.encoded.data = map_info.data;
                stream->data.encoded.length_data = stream->length_total;
                stream->data.encoded.is_delta_frame = GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT);
                stream->num_planes = 1;
@@ -237,7 +248,7 @@ static gboolean __mmcamcorder_set_stream_data_normal(MMCamcorderVideoStreamDataT
 
        case MM_PIXEL_FORMAT_INVZ:
                stream->data_type = MM_CAM_STREAM_DATA_DEPTH;
-               stream->data.depth.data = map_info->data;
+               stream->data.depth.data = map_info.data;
                stream->data.depth.length_data = stream->length_total;
                stream->stride[0] = stream->width << 1;
                stream->elevation[0] = stream->height;
@@ -247,7 +258,7 @@ static gboolean __mmcamcorder_set_stream_data_normal(MMCamcorderVideoStreamDataT
        case MM_PIXEL_FORMAT_RGBA: /* fall through */
        case MM_PIXEL_FORMAT_ARGB:
                stream->data_type = MM_CAM_STREAM_DATA_RGB;
-               stream->data.rgb.data = map_info->data;
+               stream->data.rgb.data = map_info.data;
                stream->data.rgb.length_data = stream->length_total;
                stream->stride[0] = stream->width << 2;
                stream->elevation[0] = stream->height;
@@ -256,38 +267,101 @@ static gboolean __mmcamcorder_set_stream_data_normal(MMCamcorderVideoStreamDataT
 
        default:
                MMCAM_LOG_ERROR("unsupported format[%d]", stream->format);
-               return FALSE;
+               ret = FALSE;
+               break;
        }
 
-       return TRUE;
+       gst_memory_unmap(memory, &map_info);
+
+       return ret;
 }
 
 
-static gboolean __mmcamcorder_set_stream_data_tbm(MMCamcorderVideoStreamDataType *stream, tbm_surface_info_s *ts_info)
+static gboolean __mmcamcorder_set_stream_data_zero_copy(MMCamcorderVideoStreamDataType *stream, GstBuffer *buffer, GstMemory *memory)
 {
-       mmf_return_val_if_fail(ts_info, FALSE);
+       int i = 0;
+       int num_bos = 0;
+       int tbm_ret = TBM_SURFACE_ERROR_NONE;
+       tbm_bo_handle bo_handle = {NULL, };
+       tbm_surface_info_s ts_info;
+
        mmf_return_val_if_fail(stream, FALSE);
+       mmf_return_val_if_fail(buffer, FALSE);
+       mmf_return_val_if_fail(memory, FALSE);
+
+       if (_mmcamcorder_is_encoded_preview_pixel_format(stream->format)) {
+               stream->bo[0] = gst_tizen_memory_get_bos(memory, 0);
+
+               bo_handle = tbm_bo_get_handle(stream->bo[0], TBM_DEVICE_CPU);
+               if (!bo_handle.ptr) {
+                       MMCAM_LOG_ERROR("tbm_bo_get_handle failed[bo:%p,memory:%p]", stream->bo[0], memory);
+                       return FALSE;
+               }
+
+               stream->data_type = MM_CAM_STREAM_DATA_ENCODED;
+               stream->num_planes = 1;
+               stream->stride[0] = stream->width;
+               stream->elevation[0] = stream->height;
+               stream->data.encoded.data = bo_handle.ptr;
+               stream->length_total = gst_memory_get_sizes(memory, NULL, NULL);
+               stream->data.encoded.length_data = stream->length_total;
+               stream->data.encoded.is_delta_frame = GST_BUFFER_FLAG_IS_SET(buffer, GST_BUFFER_FLAG_DELTA_UNIT);
+
+               MMCAM_LOG_VERBOSE("[ENCODED] length[%u], is_delta[%d]",
+                       stream->data.encoded.length_data, stream->data.encoded.is_delta_frame);
+
+               return TRUE;
+       }
+
+       memset(&ts_info, 0x0, sizeof(tbm_surface_info_s));
+
+       tbm_ret = tbm_surface_get_info((tbm_surface_h)gst_tizen_memory_get_surface(memory), &ts_info);
+       if (tbm_ret != TBM_SURFACE_ERROR_NONE) {
+               MMCAM_LOG_ERROR("get tbm surface info failed[0x%x]", tbm_ret);
+               return FALSE;
+       }
+
+       stream->length_total = ts_info.size;
+       for (i = 0 ; i < ts_info.num_planes ; i++) {
+               stream->stride[i] = ts_info.planes[i].stride;
+               stream->elevation[i] = ts_info.planes[i].size / ts_info.planes[i].stride;
+               MMCAM_LOG_VERBOSE("    plane[%d] %dx%d", i, stream->stride[i], stream->elevation[i]);
+       }
+
+       num_bos = gst_tizen_memory_get_num_bos(memory);
+       for (i = 0 ; i < num_bos ; i++)
+               stream->bo[i] = gst_tizen_memory_get_bos(memory, i);
+
 
        switch (stream->format) {
        case MM_PIXEL_FORMAT_NV12: /* fall through */
        case MM_PIXEL_FORMAT_NV21:
                stream->data_type = MM_CAM_STREAM_DATA_YUV420SP;
                stream->num_planes = 2;
-               stream->data.yuv420sp.y = ts_info->planes[0].ptr;
-               stream->data.yuv420sp.length_y = ts_info->planes[0].size;
-               stream->data.yuv420sp.uv = ts_info->planes[1].ptr;
-               stream->data.yuv420sp.length_uv = ts_info->planes[1].size;
+               stream->data.yuv420sp.y = ts_info.planes[0].ptr;
+               stream->data.yuv420sp.length_y = ts_info.planes[0].size;
+               stream->data.yuv420sp.uv = ts_info.planes[1].ptr;
+               stream->data.yuv420sp.length_uv = ts_info.planes[1].size;
+
+               MMCAM_LOG_VERBOSE("[420SP] 0[%p,%u], 1[%p,%u]",
+                       stream->data.yuv420sp.y, stream->data.yuv420sp.length_y,
+                       stream->data.yuv420sp.uv, stream->data.yuv420sp.length_uv);
                break;
 
        case MM_PIXEL_FORMAT_I420:
                stream->data_type = MM_CAM_STREAM_DATA_YUV420P;
                stream->num_planes = 3;
-               stream->data.yuv420p.y = ts_info->planes[0].ptr;
-               stream->data.yuv420p.length_y = ts_info->planes[0].size;
-               stream->data.yuv420p.u = ts_info->planes[1].ptr;
-               stream->data.yuv420p.length_u = ts_info->planes[1].size;
-               stream->data.yuv420p.v = ts_info->planes[2].ptr;
-               stream->data.yuv420p.length_v = ts_info->planes[2].size;
+               stream->data.yuv420p.y = ts_info.planes[0].ptr;
+               stream->data.yuv420p.length_y = ts_info.planes[0].size;
+               stream->data.yuv420p.u = ts_info.planes[1].ptr;
+               stream->data.yuv420p.length_u = ts_info.planes[1].size;
+               stream->data.yuv420p.v = ts_info.planes[2].ptr;
+               stream->data.yuv420p.length_v = ts_info.planes[2].size;
+
+               MMCAM_LOG_VERBOSE("[420P] 0[%p,%u], 1[%p,%u], 2[%p,%u]",
+                       stream->data.yuv420p.y, stream->data.yuv420p.length_y,
+                       stream->data.yuv420p.u, stream->data.yuv420p.length_u,
+                       stream->data.yuv420p.v, stream->data.yuv420p.length_v);
                break;
 
        default:
@@ -302,18 +376,13 @@ static gboolean __mmcamcorder_set_stream_data_tbm(MMCamcorderVideoStreamDataType
 gboolean _mmcamcorder_invoke_video_stream_cb(MMHandleType handle, GstSample *sample, gboolean is_preview, int stream_id)
 {
        int i = 0;
-       int num_bos = 0;
        gboolean ret_cb = TRUE;
        mmf_camcorder_t *hcamcorder = MMF_CAMCORDER(handle);
        _MMCamcorderSubContext *sc = NULL;
        MMCamcorderVideoStreamDataType stream;
 
-       tbm_surface_h t_surface = NULL;
-       tbm_surface_info_s ts_info;
-
        GstBuffer *buffer = NULL;
        GstMemory *memory = NULL;
-       GstMapInfo map_info;
        GstCaps *caps = NULL;
        GstStructure *structure = NULL;
 
@@ -326,8 +395,6 @@ gboolean _mmcamcorder_invoke_video_stream_cb(MMHandleType handle, GstSample *sam
        sc = MMF_CAMCORDER_SUBCONTEXT(hcamcorder);
        mmf_return_val_if_fail(sc, FALSE);
 
-       /* clear data structure */
-       memset(&map_info, 0x0, sizeof(GstMapInfo));
        memset(&stream, 0x0, sizeof(MMCamcorderVideoStreamDataType));
 
        caps = gst_sample_get_caps(sample);
@@ -336,52 +403,36 @@ gboolean _mmcamcorder_invoke_video_stream_cb(MMHandleType handle, GstSample *sam
        structure = gst_caps_get_structure(caps, 0);
        mmf_return_val_if_fail(structure, FALSE);
 
-       /* set format and resolution */
-       gst_structure_get_int(structure, "width", &stream.width);
-       gst_structure_get_int(structure, "height", &stream.height);
-       stream.format = _mmcamcorder_get_pixel_format(caps);
+       stream.format = _mmcamcorder_get_pixel_format(caps, TRUE);
+       gst_structure_get(structure,
+               "width", G_TYPE_INT, &stream.width,
+               "height", G_TYPE_INT, &stream.height,
+               NULL);
 
-       if (_mmcamcorder_is_encoded_preview_pixel_format(stream.format)) {
-               memory = gst_buffer_get_all_memory(buffer);
-               stream.internal_buffer = buffer;
-       } else {
-               memory = gst_buffer_peek_memory(buffer, 0);
-       }
+       memory = gst_buffer_peek_memory(buffer, 0);
        if (!memory) {
                MMCAM_LOG_ERROR("GstMemory get failed from buffer %p", buffer);
                return FALSE;
        }
 
-       /* set zero-copy related information */
-       if (hcamcorder->use_zero_copy_format) {
-               t_surface = (tbm_surface_h)gst_tizen_memory_get_surface(memory);
-
-               if (tbm_surface_get_info(t_surface, &ts_info) != TBM_SURFACE_ERROR_NONE) {
-                       MMCAM_LOG_ERROR("failed to get tbm surface[%p] info", t_surface);
-                       goto _INVOKE_VIDEO_STREAM_CB_DONE;
-               }
-
-               /* set bo, stride and elevation */
-               num_bos = gst_tizen_memory_get_num_bos(memory);
-               for (i = 0 ; i < num_bos ; i++)
-                       stream.bo[i] = gst_tizen_memory_get_bos(memory, i);
-
-               for (i = 0 ; i < ts_info.num_planes ; i++) {
-                       stream.stride[i] = ts_info.planes[i].stride;
-                       stream.elevation[i] = ts_info.planes[i].size / ts_info.planes[i].stride;
-                       MMCAM_LOG_VERBOSE("    plane[%d] %dx%d", i, stream.stride[i], stream.elevation[i]);
-               }
+       structure = gst_mini_object_get_qdata(GST_MINI_OBJECT_CAST(buffer), hcamcorder->buffer_quark);
+       if (structure) {
+               gst_structure_get(structure,
+                       "focus-state", G_TYPE_INT, &stream.focus_state,
+                       "facing-direction", G_TYPE_INT, &stream.facing_direction,
+                       "flip", G_TYPE_INT, &stream.flip,
+                       "rotation", G_TYPE_INT, &stream.rotation,
+                       NULL);
 
-               stream.length_total = ts_info.size;
-               stream.internal_buffer = buffer;
+               MMCAM_LOG_VERBOSE("structure[%p], [fs:%d,fd:%d,f:%d,r:%d]", structure,
+                       stream.focus_state, stream.facing_direction, stream.flip, stream.rotation);
+       }
 
-               if (!__mmcamcorder_set_stream_data_tbm(&stream, &ts_info))
+       if (gst_is_tizen_memory(memory)) {
+               if (!__mmcamcorder_set_stream_data_zero_copy(&stream, buffer, memory))
                        goto _INVOKE_VIDEO_STREAM_CB_DONE;
        } else {
-               stream.length_total = gst_memory_get_sizes(memory, NULL, NULL);
-
-               if (!gst_memory_map(memory, &map_info, GST_MAP_READWRITE) ||
-                       !__mmcamcorder_set_stream_data_normal(&stream, buffer, &map_info))
+               if (!__mmcamcorder_set_stream_data(&stream, buffer, memory))
                        goto _INVOKE_VIDEO_STREAM_CB_DONE;
        }
 
@@ -390,6 +441,7 @@ gboolean _mmcamcorder_invoke_video_stream_cb(MMHandleType handle, GstSample *sam
 
        stream.timestamp = (unsigned int)(GST_BUFFER_PTS(buffer) / 1000000); /* nano sec -> milli sec */
        stream.extra_stream_id = stream_id;
+       stream.internal_buffer = buffer;
 
        /* invoke application callback */
        if (is_preview) {
@@ -410,12 +462,6 @@ _INVOKE_VIDEO_STREAM_CB_DONE:
                tbm_bo_unmap(stream.bo[i]);
        }
 
-       if (map_info.data)
-               gst_memory_unmap(memory, &map_info);
-
-       if (_mmcamcorder_is_encoded_preview_pixel_format(stream.format))
-               gst_memory_unref(memory);
-
        return ret_cb;
 }
 
@@ -549,13 +595,14 @@ int _mmcamcorder_create_preview_elements(MMHandleType handle)
         */
        MMCAMCORDER_G_OBJECT_SET_POINTER(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hal-name", hcamcorder->network_hal_name);
 
-       /* set camera properties */
+       /* camera properties */
        MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "high-speed-fps", 0);
        MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-width", capture_width);
        MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-height", capture_height);
        MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "capture-jpg-quality", capture_jpg_quality);
        MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "hdr-capture", sc->info_image->hdr_capture_mode);
        MMCAMCORDER_G_OBJECT_SET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "extra-preview", extra_preview_enable);
+       MMCAMCORDER_G_OBJECT_GET(sc->element[_MMCAMCORDER_VIDEOSRC_SRC].gst, "buffer-quark", &hcamcorder->buffer_quark);
 
        _MMCAMCORDER_ELEMENT_MAKE(sc, sc->element, _MMCAMCORDER_VIDEOSRC_QUE, "queue", "videosrc_queue", element_list, err);
 
@@ -1511,7 +1558,7 @@ int _mmcamcorder_create_encodesink_bin(MMHandleType handle, MMCamcorderEncodebin
        MMCAM_LOG_INFO("Element add complete");
 
        if (profile == MM_CAMCORDER_ENCBIN_PROFILE_VIDEO) {
-               pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "video");
+               pad = gst_element_request_pad_simple(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "video");
                if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("video_sink0", pad))) {
                        gst_object_unref(pad);
                        pad = NULL;
@@ -1523,7 +1570,7 @@ int _mmcamcorder_create_encodesink_bin(MMHandleType handle, MMCamcorderEncodebin
                pad = NULL;
 
                if (sc->audio_disable == FALSE) {
-                       pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
+                       pad = gst_element_request_pad_simple(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
                        if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad))) {
                                gst_object_unref(pad);
                                pad = NULL;
@@ -1535,7 +1582,7 @@ int _mmcamcorder_create_encodesink_bin(MMHandleType handle, MMCamcorderEncodebin
                        pad = NULL;
                }
        } else if (profile == MM_CAMCORDER_ENCBIN_PROFILE_AUDIO) {
-               pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
+               pad = gst_element_request_pad_simple(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "audio");
                if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("audio_sink0", pad))) {
                        gst_object_unref(pad);
                        pad = NULL;
@@ -1547,7 +1594,7 @@ int _mmcamcorder_create_encodesink_bin(MMHandleType handle, MMCamcorderEncodebin
                pad = NULL;
        } else {
                /* for stillshot */
-               pad = gst_element_get_request_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "image");
+               pad = gst_element_request_pad_simple(sc->encode_element[_MMCAMCORDER_ENCSINK_ENCBIN].gst, "image");
                if (!gst_element_add_pad(sc->encode_element[_MMCAMCORDER_ENCSINK_BIN].gst, gst_ghost_pad_new("image_sink0", pad))) {
                        gst_object_unref(pad);
                        pad = NULL;
@@ -2617,10 +2664,12 @@ bool _mmcamcorder_set_videosrc_caps(MMHandleType handle, unsigned int fourcc, in
                        if (format_string)
                                caps_fourcc = _mmcamcorder_convert_fourcc_string_to_value(format_string);
 
-                       gst_structure_get_int(structure, "width", &caps_width);
-                       gst_structure_get_int(structure, "height", &caps_height);
-                       gst_structure_get_int(structure, "fps", &caps_fps);
-                       gst_structure_get_int(structure, "rotate", &caps_rotate);
+                       gst_structure_get(structure,
+                               "width", G_TYPE_INT, &caps_width,
+                               "height", G_TYPE_INT, &caps_height,
+                               "fps", G_TYPE_INT, &caps_fps,
+                               "rotate", G_TYPE_INT, &caps_rotate,
+                               NULL);
 
 #ifdef _MMCAMCORDER_PRODUCT_TV
                        if (_mmcamcorder_is_encoded_preview_pixel_format(sc->info_image->preview_format)) {