1 /* GStreamer Progress Report Element
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David Schleef <ds@schleef.org>
4 * Copyright (C) <2004> Jan Schmidt <thaytan@mad.scientist.com>
5 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:element-progressreport
25 * @short_description: Reports progress
30 * The progressreport element can be put into a pipeline to report progress,
31 * which is done by doing upstream duration and position queries in regular
32 * (real-time) intervals. Both the interval and the prefered query format
33 * can be specified via the "update-freq" and the "format" property.
36 * Element messages containing a "progress" structure are posted on the bus
37 * whenever progress has been queried (since gst-plugins-good 0.10.6 only).
40 * Since the element was originally designed for debugging purposes, it will
41 * by default also print information about the current progress to the
42 * terminal. This can be prevented by setting the "silent" property to TRUE.
45 * This element is most useful in transcoding pipelines or other situations
46 * where just querying the pipeline might not lead to the wanted result. For
47 * progress in TIME format, the element is best placed in a 'raw stream'
48 * section of the pipeline (or after any demuxers/decoders/parsers).
51 * Three more things should be pointed out: firstly, the element will only
52 * query progress when data flow happens. If data flow is stalled for some
53 * reason, no progress messages will be posted. Secondly, there are other
54 * elements (like qtdemux, for example) that may also post "progress" element
55 * messages on the bus. Applications should check the source of any element
56 * messages they receive, if needed. Finally, applications should not take
57 * action on receiving notification of progress being 100%, they should only
58 * take action when they receive an EOS message (since the progress reported
59 * is in reference to an internal point of a pipeline and not the pipeline as
62 * <title>Example launch line</title>
65 * gst-launch -m filesrc location=foo.ogg ! decodebin ! progressreport update-freq=1 ! audioconvert ! audioresample ! autoaudiosink
67 * This shows a progress query where a duration is available.
71 * gst-launch -m audiotestsrc ! progressreport update-freq=1 ! audioconvert ! autoaudiosink
73 * This shows a progress query where no duration is available.
87 #include "progressreport.h"
98 GstStaticPadTemplate progress_report_src_template =
99 GST_STATIC_PAD_TEMPLATE ("src",
102 GST_STATIC_CAPS_ANY);
104 GstStaticPadTemplate progress_report_sink_template =
105 GST_STATIC_PAD_TEMPLATE ("sink",
108 GST_STATIC_CAPS_ANY);
110 static const GstElementDetails progress_report_details =
111 GST_ELEMENT_DETAILS ("Progress report",
113 "Periodically query and report on processing progress",
114 "Jan Schmidt <thaytan@mad.scientist.com>");
116 #define DEFAULT_UPDATE_FREQ 5
117 #define DEFAULT_SILENT FALSE
118 #define DEFAULT_FORMAT "auto"
120 static void gst_progress_report_set_property (GObject * object, guint prop_id,
121 const GValue * value, GParamSpec * pspec);
122 static void gst_progress_report_get_property (GObject * object, guint prop_id,
123 GValue * value, GParamSpec * pspec);
125 static gboolean gst_progress_report_event (GstBaseTransform * trans,
127 static GstFlowReturn gst_progress_report_transform_ip (GstBaseTransform * trans,
130 static gboolean gst_progress_report_start (GstBaseTransform * trans);
131 static gboolean gst_progress_report_stop (GstBaseTransform * trans);
133 GST_BOILERPLATE (GstProgressReport, gst_progress_report, GstBaseTransform,
134 GST_TYPE_BASE_TRANSFORM);
137 gst_progress_report_base_init (gpointer g_class)
139 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
141 gst_element_class_add_pad_template (element_class,
142 gst_static_pad_template_get (&progress_report_sink_template));
143 gst_element_class_add_pad_template (element_class,
144 gst_static_pad_template_get (&progress_report_src_template));
146 gst_element_class_set_details (element_class, &progress_report_details);
150 gst_progress_report_finalize (GObject * obj)
152 GstProgressReport *filter = GST_PROGRESS_REPORT (obj);
154 g_free (filter->format);
155 filter->format = NULL;
157 G_OBJECT_CLASS (parent_class)->finalize (obj);
161 gst_progress_report_class_init (GstProgressReportClass * g_class)
163 GstBaseTransformClass *gstbasetrans_class;
164 GObjectClass *gobject_class;
166 gobject_class = G_OBJECT_CLASS (g_class);
167 gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (g_class);
169 gobject_class->finalize = gst_progress_report_finalize;
170 gobject_class->set_property = gst_progress_report_set_property;
171 gobject_class->get_property = gst_progress_report_get_property;
173 g_object_class_install_property (gobject_class,
174 ARG_UPDATE_FREQ, g_param_spec_int ("update-freq", "Update Frequency",
175 "Number of seconds between reports when data is flowing", 1, G_MAXINT,
176 DEFAULT_UPDATE_FREQ, G_PARAM_READWRITE));
178 g_object_class_install_property (gobject_class,
179 ARG_SILENT, g_param_spec_boolean ("silent",
180 "Do not print output to stdout", "Do not print output to stdout",
181 DEFAULT_SILENT, G_PARAM_READWRITE));
183 g_object_class_install_property (gobject_class,
184 ARG_FORMAT, g_param_spec_string ("format", "format",
185 "Format to use for the querying", DEFAULT_FORMAT, G_PARAM_READWRITE));
187 gstbasetrans_class->event = GST_DEBUG_FUNCPTR (gst_progress_report_event);
188 gstbasetrans_class->transform_ip =
189 GST_DEBUG_FUNCPTR (gst_progress_report_transform_ip);
190 gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_progress_report_start);
191 gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_progress_report_stop);
195 gst_progress_report_init (GstProgressReport * report,
196 GstProgressReportClass * g_class)
198 gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (report), TRUE);
200 report->update_freq = DEFAULT_UPDATE_FREQ;
201 report->silent = DEFAULT_SILENT;
202 report->format = g_strdup (DEFAULT_FORMAT);
206 gst_progress_report_post_progress (GstProgressReport * filter,
207 GstFormat format, gint64 current, gint64 total)
209 GstStructure *s = NULL;
211 if (current >= 0 && total > 0) {
214 perc = gst_util_guint64_to_gdouble (current) * 100.0 /
215 gst_util_guint64_to_gdouble (total);
216 perc = CLAMP (perc, 0.0, 100.0);
218 /* we provide a "percent" field of integer type to stay compatible
219 * with qtdemux, but add a second "percent-double" field for those who
220 * want more precision and are too lazy to calculate it themselves */
221 s = gst_structure_new ("progress", "percent", G_TYPE_INT, (gint) perc,
222 "percent-double", G_TYPE_DOUBLE, perc, "current", G_TYPE_INT64, current,
223 "total", G_TYPE_INT64, total, NULL);
224 } else if (current >= 0) {
225 s = gst_structure_new ("progress", "current", G_TYPE_INT64, current, NULL);
229 GST_LOG_OBJECT (filter, "posting progress message: %" GST_PTR_FORMAT, s);
230 gst_structure_set (s, "format", GST_TYPE_FORMAT, format, NULL);
231 /* can't post it right here because we're holding the object lock */
232 filter->pending_msg = gst_message_new_element (GST_OBJECT_CAST (filter), s);
237 gst_progress_report_do_query (GstProgressReport * filter, GstFormat format,
238 gint hh, gint mm, gint ss)
240 const gchar *format_name = NULL;
244 sink_pad = GST_BASE_TRANSFORM (filter)->sinkpad;
246 GST_LOG_OBJECT (filter, "querying using format %d (%s)", format,
247 gst_format_get_name (format));
249 if (!gst_pad_query_peer_position (sink_pad, &format, &cur) ||
250 !gst_pad_query_peer_duration (sink_pad, &format, &total)) {
255 case GST_FORMAT_BYTES:
256 format_name = "bytes";
258 case GST_FORMAT_BUFFERS:
259 format_name = "buffers";
261 case GST_FORMAT_PERCENT:
262 format_name = "percent";
264 case GST_FORMAT_TIME:
265 format_name = "seconds";
269 case GST_FORMAT_DEFAULT:{
272 format_name = "bogounits";
273 caps = GST_PAD_CAPS (GST_BASE_TRANSFORM (filter)->sinkpad);
274 if (caps && gst_caps_is_fixed (caps) && !gst_caps_is_any (caps)) {
275 GstStructure *s = gst_caps_get_structure (caps, 0);
276 const gchar *mime_type = gst_structure_get_name (s);
278 if (g_str_has_prefix (mime_type, "video/") ||
279 g_str_has_prefix (mime_type, "image/")) {
280 format_name = "frames";
281 } else if (g_str_has_prefix (mime_type, "audio/")) {
282 format_name = "samples";
288 const GstFormatDefinition *details;
290 details = gst_format_get_details (format);
292 format_name = details->nick;
294 format_name = "unknown";
300 if (!filter->silent) {
302 g_print ("%s (%02d:%02d:%02d): %" G_GINT64_FORMAT " / %"
303 G_GINT64_FORMAT " %s (%4.1f %%)\n", GST_OBJECT_NAME (filter), hh,
304 mm, ss, cur, total, format_name, (gdouble) cur / total * 100.0);
306 g_print ("%s (%02d:%02d:%02d): %" G_GINT64_FORMAT " %s\n",
307 GST_OBJECT_NAME (filter), hh, mm, ss, cur, format_name);
311 gst_progress_report_post_progress (filter, format, cur, total);
316 gst_progress_report_report (GstProgressReport * filter, GTimeVal cur_time)
318 GstFormat try_formats[] = { GST_FORMAT_TIME, GST_FORMAT_BYTES,
319 GST_FORMAT_PERCENT, GST_FORMAT_BUFFERS,
323 GstFormat format = GST_FORMAT_UNDEFINED;
324 gboolean done = FALSE;
328 run_time = cur_time.tv_sec - filter->start_time.tv_sec;
330 hh = (run_time / 3600) % 100;
331 mm = (run_time / 60) % 60;
332 ss = (run_time % 60);
334 GST_OBJECT_LOCK (filter);
336 if (filter->format != NULL && strcmp (filter->format, "auto") != 0) {
337 format = gst_format_get_by_nick (filter->format);
340 if (format != GST_FORMAT_UNDEFINED) {
341 done = gst_progress_report_do_query (filter, format, hh, mm, ss);
345 for (i = 0; i < G_N_ELEMENTS (try_formats); ++i) {
346 done = gst_progress_report_do_query (filter, try_formats[i], hh, mm, ss);
352 if (!done && !filter->silent) {
353 g_print ("%s (%2d:%2d:%2d): Could not query position and/or duration\n",
354 GST_OBJECT_NAME (filter), hh, mm, ss);
357 msg = filter->pending_msg;
358 filter->pending_msg = NULL;
359 GST_OBJECT_UNLOCK (filter);
362 gst_element_post_message (GST_ELEMENT_CAST (filter), msg);
367 gst_progress_report_event (GstBaseTransform * trans, GstEvent * event)
369 GstProgressReport *filter;
371 filter = GST_PROGRESS_REPORT (trans);
373 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
376 g_get_current_time (&cur_time);
377 gst_progress_report_report (filter, cur_time);
379 return GST_BASE_TRANSFORM_CLASS (parent_class)->event (trans, event);
383 gst_progress_report_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
385 GstProgressReport *filter;
386 gboolean need_update;
389 g_get_current_time (&cur_time);
391 filter = GST_PROGRESS_REPORT (trans);
393 /* Check if update_freq seconds have passed since the last update */
394 GST_OBJECT_LOCK (filter);
396 ((cur_time.tv_sec - filter->last_report.tv_sec) >= filter->update_freq);
397 GST_OBJECT_UNLOCK (filter);
400 gst_progress_report_report (filter, cur_time);
401 GST_OBJECT_LOCK (filter);
402 filter->last_report = cur_time;
403 GST_OBJECT_UNLOCK (filter);
410 gst_progress_report_start (GstBaseTransform * trans)
412 GstProgressReport *filter;
414 filter = GST_PROGRESS_REPORT (trans);
416 g_get_current_time (&filter->last_report);
417 filter->start_time = filter->last_report;
423 gst_progress_report_stop (GstBaseTransform * trans)
425 /* anything we should be doing here? */
430 gst_progress_report_set_property (GObject * object, guint prop_id,
431 const GValue * value, GParamSpec * pspec)
433 GstProgressReport *filter;
435 filter = GST_PROGRESS_REPORT (object);
438 case ARG_UPDATE_FREQ:
439 GST_OBJECT_LOCK (filter);
440 filter->update_freq = g_value_get_int (value);
441 GST_OBJECT_UNLOCK (filter);
444 GST_OBJECT_LOCK (filter);
445 filter->silent = g_value_get_boolean (value);
446 GST_OBJECT_UNLOCK (filter);
449 GST_OBJECT_LOCK (filter);
450 g_free (filter->format);
451 filter->format = g_value_dup_string (value);
452 if (filter->format == NULL)
453 filter->format = g_strdup ("auto");
454 GST_OBJECT_UNLOCK (filter);
462 gst_progress_report_get_property (GObject * object, guint prop_id,
463 GValue * value, GParamSpec * pspec)
465 GstProgressReport *filter;
467 filter = GST_PROGRESS_REPORT (object);
470 case ARG_UPDATE_FREQ:
471 GST_OBJECT_LOCK (filter);
472 g_value_set_int (value, filter->update_freq);
473 GST_OBJECT_UNLOCK (filter);
476 GST_OBJECT_LOCK (filter);
477 g_value_set_boolean (value, filter->silent);
478 GST_OBJECT_UNLOCK (filter);
481 GST_OBJECT_LOCK (filter);
482 g_value_set_string (value, filter->format);
483 GST_OBJECT_UNLOCK (filter);
486 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);