4 * Copyright (c) 2011 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, ByungWook Jang <bw.jang@samsung.com>,
7 * Maksym Ukhanov <m.ukhanov@samsung.com>, Hyunjun Ko <zzoon.ko@samsung.com>
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 #include "mm_wfd_sink_util.h"
26 #define DUMP_TS_DATA_PATH "/var/tmp/"
28 static GstPadProbeReturn
29 _mm_wfd_sink_util_dump(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
34 char buf[256] = {0, };
35 char path[256] = {0, };
37 snprintf(path, sizeof(path), "%s%s_%s.ts", DUMP_TS_DATA_PATH,
38 gst_element_get_name(gst_pad_get_parent_element(pad)), gst_pad_get_name(pad));
40 if (info && info->type & GST_PAD_PROBE_TYPE_BUFFER) {
42 GstBuffer *buffer = gst_pad_probe_info_get_buffer(info);
44 gst_buffer_map(buffer, &buf_info, GST_MAP_READ);
46 wfd_sink_debug("got buffer %p with size %d", buffer, buf_info.size);
47 data = (gint8 *)(buf_info.data);
51 strerror_r(errno, buf, sizeof(buf));
52 wfd_sink_error("failed to fopen! : %s", buf);
53 return GST_PAD_PROBE_OK;
55 fwrite(data, size, 1, f);
57 gst_buffer_unmap(buffer, &buf_info);
60 return GST_PAD_PROBE_OK;
63 static GstPadProbeReturn
64 _mm_wfd_sink_util_pad_probe_cb(GstPad *pad, GstPadProbeInfo *info, gpointer u_data)
66 GstElement *parent = NULL;
68 wfd_sink_return_val_if_fail(info &&
69 info->type != GST_PAD_PROBE_TYPE_INVALID,
71 wfd_sink_return_val_if_fail(pad, GST_PAD_PROBE_DROP);
73 parent = (GstElement *)gst_object_get_parent(GST_OBJECT(pad));
75 wfd_sink_error("failed to get parent of pad");
76 return GST_PAD_PROBE_DROP;
79 if (info->type & GST_PAD_PROBE_TYPE_BUFFER) {
80 GstBuffer *buffer = gst_pad_probe_info_get_buffer(info);
81 /* show name and timestamp */
82 wfd_sink_debug("BUFFER PROBE : %s:%s : %u:%02u:%02u.%09u (%"G_GSSIZE_FORMAT" bytes)\n",
83 GST_STR_NULL(GST_ELEMENT_NAME(parent)),
84 GST_STR_NULL(GST_PAD_NAME(pad)),
85 GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)),
86 gst_buffer_get_size(buffer));
87 } else if (info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM ||
88 info->type & GST_PAD_PROBE_TYPE_EVENT_UPSTREAM ||
89 info->type & GST_PAD_PROBE_TYPE_EVENT_FLUSH ||
90 info->type & GST_PAD_PROBE_TYPE_EVENT_BOTH) {
91 GstEvent *event = gst_pad_probe_info_get_event(info);
93 /* show name and event type */
94 wfd_sink_debug("EVENT PROBE : %s:%s : %s\n",
95 GST_STR_NULL(GST_ELEMENT_NAME(parent)),
96 GST_STR_NULL(GST_PAD_NAME(pad)),
97 GST_EVENT_TYPE_NAME(event));
99 if (GST_EVENT_TYPE(event) == GST_EVENT_SEGMENT) {
100 const GstSegment *segment = NULL;
101 gst_event_parse_segment(event, &segment);
103 wfd_sink_debug("NEWSEGMENT : %" G_GINT64_FORMAT
104 " -- %" G_GINT64_FORMAT ", time %" G_GINT64_FORMAT " \n",
105 segment->start, segment->stop, segment->time);
110 gst_object_unref(parent);
112 return GST_PAD_PROBE_OK;
116 mm_wfd_sink_util_add_pad_probe(GstPad *pad, GstElement *element, const gchar *pad_name)
118 GstPad *probe_pad = NULL;
121 if (element && pad_name)
122 probe_pad = gst_element_get_static_pad(element, pad_name);
125 gst_object_ref(probe_pad);
129 wfd_sink_debug("add pad(%s) probe", GST_STR_NULL(GST_PAD_NAME(probe_pad)));
130 gst_pad_add_probe(probe_pad, GST_PAD_PROBE_TYPE_DATA_BOTH,
131 _mm_wfd_sink_util_pad_probe_cb, (gpointer)NULL, NULL);
132 gst_object_unref(probe_pad);
137 mm_wfd_sink_util_add_pad_probe_for_data_dump(GstElement *element, const gchar *pad_name)
139 GstPad *probe_pad = NULL;
141 if (element && pad_name)
142 probe_pad = gst_element_get_static_pad(element, pad_name);
145 wfd_sink_debug("add pad(%s) probe", GST_STR_NULL(GST_PAD_NAME(probe_pad)));
146 gst_pad_add_probe(probe_pad, GST_PAD_PROBE_TYPE_BUFFER, _mm_wfd_sink_util_dump, (gpointer)NULL, NULL);
147 gst_object_unref(probe_pad);
151 static GstPadProbeReturn
152 _mm_wfd_sink_util_check_first_buffer_cb(GstPad *pad, GstPadProbeInfo *info, gpointer user_data)
154 GstElement *parent = NULL;
155 GstBuffer *buffer = NULL;
156 guint *probe_id = (guint *)user_data;
158 wfd_sink_return_val_if_fail(pad, GST_PAD_PROBE_DROP);
159 wfd_sink_return_val_if_fail(info, GST_PAD_PROBE_DROP);
161 parent = GST_ELEMENT_CAST(gst_object_get_parent(GST_OBJECT(pad)));
162 if (parent == NULL) {
163 wfd_sink_error("The parent of pad is NULL.");
164 return GST_PAD_PROBE_DROP;
167 buffer = gst_pad_probe_info_get_buffer(info);
169 wfd_sink_debug("FIRST BUFFER PROBE : %s:%s : %u:%02u:%02u.%09u (%"G_GSSIZE_FORMAT" bytes)\n",
170 GST_STR_NULL(GST_ELEMENT_NAME(parent)), GST_STR_NULL(GST_PAD_NAME(pad)),
171 GST_TIME_ARGS(GST_BUFFER_TIMESTAMP(buffer)), gst_buffer_get_size(buffer));
173 if (probe_id && *probe_id > 0) {
174 wfd_sink_debug("remove buffer probe[%d]\n", *probe_id);
175 gst_pad_remove_probe(pad, *probe_id);
177 MMWFDSINK_FREEIF(probe_id);
181 gst_object_unref(parent);
183 return GST_PAD_PROBE_REMOVE;
187 mm_wfd_sink_util_add_pad_probe_for_checking_first_buffer(GstPad *pad, GstElement *element, const gchar *pad_name)
189 GstPad *probe_pad = NULL;
190 guint *probe_id = NULL;
193 if (element && pad_name)
194 probe_pad = gst_element_get_static_pad(element, pad_name);
197 gst_object_ref(probe_pad);
201 probe_id = g_malloc0(sizeof(guint));
203 wfd_sink_error("failed to allocate memory for probe id\n");
204 gst_object_unref(probe_pad);
208 *probe_id = gst_pad_add_probe(probe_pad, GST_PAD_PROBE_TYPE_BUFFER, _mm_wfd_sink_util_check_first_buffer_cb, (gpointer)probe_id, NULL);
209 wfd_sink_debug("add pad(%s) probe, %d",
210 GST_STR_NULL(GST_PAD_NAME(probe_pad)), *probe_id);
212 gst_object_unref(probe_pad);