Fix resource leak when get caps from pad 14/267714/8
authorbackto.kim <backto.kim@samsung.com>
Thu, 9 Dec 2021 06:13:31 +0000 (15:13 +0900)
committerbackto.kim <backto.kim@samsung.com>
Fri, 10 Dec 2021 08:16:56 +0000 (17:16 +0900)
[Version] 0.3.18
[Issue Type] Resource leak

Change-Id: Ia25db3063b47c6f00c59f2cfabf0b8cb0a22c38b

include/webrtc_private.h
packaging/capi-media-webrtc.spec
src/webrtc_private.c
src/webrtc_sink.c
src/webrtc_source.c

index 0d82d3641b864cfaf524789e73354cea085c9c08..9cc3d4da1036dd6e601d60f8eb78feaf800ee8ce 100644 (file)
@@ -689,8 +689,10 @@ int _set_filesrc_looping(webrtc_s *webrtc, unsigned int source_id, bool looping)
 int _get_filesrc_looping(webrtc_s *webrtc, unsigned int source_id, bool *looping);
 int _remove_filesrc_pad_block_probe(webrtc_s *webrtc);
 
+gchar * _get_media_type_from_pad(GstPad *pad);
 bool _is_supported_media_type(const char *media_type);
 bool _is_audio_media_type(const char *media_type);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index cc9a9205e4dd2a48f7491c69c4499bde7835eb91..229a3658b5da0ceeafcc7d4c5399d954f4c9a7ce 100644 (file)
@@ -1,6 +1,6 @@
 Name:       capi-media-webrtc
 Summary:    A WebRTC library in Tizen Native API
-Version:    0.3.17
+Version:    0.3.18
 Release:    0
 Group:      Multimedia/API
 License:    Apache-2.0
index c3442bfefff3fa38c27fba7a6916b14879026b44..31db65c888a90ccd031e73ef4bf082c4ffc06d27 100644 (file)
@@ -1089,33 +1089,22 @@ static void __webrtcbin_transceiver_set_ulpfec_red(webrtc_s *webrtc, unsigned in
 }
 
 //LCOV_EXCL_START
-static int __get_media_type_from_pad(GstPad *pad, bool *is_video)
+gchar * _get_media_type_from_pad(GstPad *pad)
 {
-       GstCaps *caps;
-       const GValue *value;
-       const gchar *media_type;
+       GstCaps *caps = NULL;
+       gchar *media_type = NULL;
 
-       RET_VAL_IF(pad == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "pad is NULL");
-       RET_VAL_IF(is_video == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "is_video is NULL");
+       RET_VAL_IF(pad == NULL, NULL, "pad is NULL");
 
        caps = gst_pad_get_current_caps(pad);
-       value = gst_structure_get_value(gst_caps_get_structure(caps, 0), "media");
-       media_type = g_value_get_string(value);
+       RET_VAL_IF(caps == NULL, NULL, "caps is NULL");
 
-       if (!g_strcmp0(media_type, "video")) {
-               *is_video = true;
-       } else if (!g_strcmp0(media_type, "audio")) {
-               *is_video = false;
-       } else {
-               LOG_ERROR("not supported media_type[%s]", media_type);
-               gst_caps_unref(caps);
-               return WEBRTC_ERROR_INVALID_OPERATION;
-       }
+       media_type = g_strdup(gst_structure_get_string(gst_caps_get_structure(caps, 0), "media"));
+       LOG_DEBUG("media_type [%s]", media_type);
 
-       LOG_DEBUG("pad[%p] is for [%s]", pad, media_type);
        gst_caps_unref(caps);
 
-       return WEBRTC_ERROR_NONE;
+       return media_type;
 }
 //LCOV_EXCL_STOP
 
@@ -1142,7 +1131,8 @@ static void __webrtcbin_pad_added_cb(GstElement *webrtcbin, GstPad *new_pad, gpo
 {
        int ret = WEBRTC_ERROR_NONE;
        webrtc_s *webrtc = (webrtc_s *)user_data;
-       bool is_video;
+       gchar *media_type = NULL;
+       bool is_audio;
 
        RET_IF(webrtc == NULL, "webrtc is NULL");
 
@@ -1153,13 +1143,20 @@ static void __webrtcbin_pad_added_cb(GstElement *webrtcbin, GstPad *new_pad, gpo
 
        LOG_INFO("new pad[%s] is added", GST_PAD_NAME(new_pad));
 
-       ret = __get_media_type_from_pad(new_pad, &is_video);
-       if (ret != WEBRTC_ERROR_NONE)
+       media_type = _get_media_type_from_pad(new_pad);
+       RET_IF(media_type == NULL, "media_type is NULL");
+
+       if(!_is_supported_media_type(media_type)) {
+               g_free(media_type);
                return;
+       }
+
+       is_audio = _is_audio_media_type(media_type);
+       g_free(media_type);
 
-       if ((is_video && webrtc->encoded_video_frame_cb.callback) ||
-               (!is_video && webrtc->encoded_audio_frame_cb.callback)) {
-               ret = _add_forwarding_sink_bin(webrtc, new_pad, is_video);
+       if ((!is_audio && webrtc->encoded_video_frame_cb.callback) ||
+               (is_audio && webrtc->encoded_audio_frame_cb.callback)) {
+               ret = _add_forwarding_sink_bin(webrtc, new_pad, !is_audio);
                RET_IF(ret != WEBRTC_ERROR_NONE, "failed to _add_forwarding_sink_bin()");
        } else {
                ret = _add_rendering_sink_bin(webrtc, new_pad);
index 8745e8f949d43eb4caa0cbfdf304b2fbae37fe86..40a04536a247168f7f33ff316e36192befec0f16 100644 (file)
@@ -849,6 +849,7 @@ int _add_forwarding_sink_bin(webrtc_s *webrtc, GstPad *src_pad, bool is_video)
        GstCaps *sink_caps;
        webrtc_gst_slot_s *sink;
        GstPad *sink_pad = NULL;
+       GstCaps *src_caps;
 
        RET_VAL_IF(webrtc == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "webrtc is NULL");
        RET_VAL_IF(src_pad == NULL, WEBRTC_ERROR_INVALID_PARAMETER, "src_pad is NULL");
@@ -862,7 +863,10 @@ int _add_forwarding_sink_bin(webrtc_s *webrtc, GstPad *src_pad, bool is_video)
 
        g_free(bin_name);
 
-       CREATE_ELEMENT_FROM_REGISTRY(elem_info, GST_KLASS_NAME_DEPAYLOADER_RTP, gst_pad_get_current_caps(src_pad), NULL, NULL, depayloader);
+       src_caps = gst_pad_get_current_caps(src_pad);
+       CREATE_ELEMENT_FROM_REGISTRY(elem_info, GST_KLASS_NAME_DEPAYLOADER_RTP, src_caps, NULL, NULL, depayloader);
+       gst_caps_unref(src_caps);
+
        if (!depayloader)
                goto error_before_insert;
 
index c047325d90d66eea0fd7b340f91056e903fc34a1..f6ad90ed41d4d78aa5641d7c4083bb05a4b11ab9 100644 (file)
@@ -1885,11 +1885,13 @@ static void __filesrc_pipeline_video_stream_handoff_cb(GstElement *object, GstBu
 static GstPadProbeReturn __fakesink_block_probe_cb(GstPad *pad,  GstPadProbeInfo *info, gpointer u_data)
 {
        webrtc_gst_slot_s *source = u_data;
-       gchar *media = NULL;
+       gchar *media_type = NULL;
 
-       gst_structure_get(gst_caps_get_structure(gst_pad_get_current_caps(pad), 0), "media", G_TYPE_STRING, &media, NULL);
+       media_type = _get_media_type_from_pad(pad);
+       RET_VAL_IF(media_type == NULL, GST_PAD_PROBE_OK, "media_type is NULL");
 
-       LOG_DEBUG("source[%p, id:%u] fakesink pad[%p] for [%s] is blocked", source, source->id, pad, media);
+       LOG_DEBUG("source[%p, id:%u] fakesink pad[%p] for [%s] is blocked", source, source->id, pad, media_type);
+       g_free(media_type);
 
        return GST_PAD_PROBE_OK;
 }
@@ -1897,26 +1899,31 @@ static GstPadProbeReturn __fakesink_block_probe_cb(GstPad *pad,  GstPadProbeInfo
 static GstPadProbeReturn __fakesink_probe_cb(GstPad *pad,  GstPadProbeInfo *info, gpointer u_data)
 {
        webrtc_gst_slot_s *source = u_data;
-       GstCaps *new_cap = NULL;
+       GstCaps * caps= NULL;
        GstElement *appsrc = NULL;
-       const gchar *media_type = NULL;
+       gchar *media_type = NULL;
        int av_idx;
 
-       gst_structure_get(gst_caps_get_structure(gst_pad_get_current_caps(pad), 0), "media", G_TYPE_STRING, &media_type, NULL);
+       media_type = _get_media_type_from_pad(pad);
+       RET_VAL_IF(media_type == NULL, GST_PAD_PROBE_OK, "media_type is NULL");
 
-       if(!_is_supported_media_type(media_type))
+       if(!_is_supported_media_type(media_type)) {
+               g_free(media_type);
                return GST_PAD_PROBE_OK;
+       }
 
        av_idx = GET_AV_IDX(_is_audio_media_type(media_type));
+       g_free(media_type);
 
        appsrc = gst_bin_get_by_name(source->bin, _av_tbl[av_idx].appsrc_name);
-       RET_VAL_IF(appsrc == NULL, GST_PAD_PROBE_OK, "There is no appsrc for [%s]", media_type);
+       RET_VAL_IF(appsrc == NULL, GST_PAD_PROBE_OK, "There is no appsrc for [%s]", (av_idx == AV_IDX_AUDIO) ? "audio" : "video");
 
-       new_cap = gst_caps_copy(gst_pad_get_current_caps(pad));
-       g_object_set(G_OBJECT(appsrc), "caps", new_cap, NULL);
+       caps = gst_pad_get_current_caps(pad);
+       g_object_set(G_OBJECT(appsrc), "caps", caps, NULL);
 
-       LOG_INFO("setting caps for [%s appsrc] successfully", media_type);
-       PRINT_CAPS(new_cap, "appsrc");
+       LOG_INFO("setting caps for [%s appsrc] successfully", (av_idx == AV_IDX_AUDIO) ? "audio" : "video");
+       PRINT_CAPS(caps, "appsrc");
+       gst_caps_unref(caps);
 
        source->filesrc_av[av_idx].sink_pad = pad;
        source->filesrc_av[av_idx].sink_pad_probe_id = gst_pad_add_probe(pad, GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_BLOCK,
@@ -1928,16 +1935,20 @@ static GstPadProbeReturn __fakesink_probe_cb(GstPad *pad,  GstPadProbeInfo *info
 static GstElement * __create_payloader_for_filesrc_pipeline(GstPad *pad, bool is_audio)
 {
        element_info_s elem_info;
+       GstCaps *caps;
        GstElement *payloader = NULL;
 
        RET_VAL_IF(pad == NULL, NULL, "pad is NULL");
 
+       caps = gst_pad_get_current_caps(pad);
+
        CREATE_ELEMENT_FROM_REGISTRY(elem_info, GST_KLASS_NAME_PAYLOADER_RTP,
-                                                               gst_pad_get_current_caps(pad),
+                                                               caps,
                                                                NULL,
                                                                NULL,
                                                                payloader);
 
+       gst_caps_unref(caps);
        RET_VAL_IF(payloader == NULL, NULL, "payloader is NULL");
 
        gst_element_set_name(payloader, _av_tbl[GET_AV_IDX(is_audio)].payloader_name);