Remove dummy plugin_init. Remove some undefined entries from doc- section file. Add...
[platform/upstream/gstreamer.git] / gst / debug / progressreport.c
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>
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:element-progressreport
25  * @short_description: Reports progress
26  * @see_also:
27  *
28  * <refsect2>
29  * <para>
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.
34  * </para>
35  * <para>
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).
38  * </para>
39  * <para>
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.
43  * </para>
44  * <para>
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).
49  * </para>
50  * <para>
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
60  * a whole).
61  * </para>
62  * <title>Example launch line</title>
63  * <para>
64  * <programlisting>
65  * gst-launch -m filesrc location=foo.ogg ! decodebin ! progressreport update-freq=1 ! audioconvert ! audioresample ! autoaudiosink
66  * </programlisting>
67  * This shows a progress query where a duration is available.
68  * </para>
69  * <para>
70  * <programlisting>
71  * gst-launch -m audiotestsrc ! progressreport update-freq=1 ! audioconvert ! autoaudiosink
72  * </programlisting>
73  * This shows a progress query where no duration is available.
74  * </para>
75  * </refsect2>
76  */
77
78 #ifdef HAVE_CONFIG_H
79 #include "config.h"
80 #endif
81
82 #include <gst/gst.h>
83 #include <string.h>
84 #include <math.h>
85 #include <time.h>
86
87 #include "progressreport.h"
88
89
90 enum
91 {
92   ARG_0,
93   ARG_UPDATE_FREQ,
94   ARG_SILENT,
95   ARG_FORMAT
96 };
97
98 GstStaticPadTemplate progress_report_src_template =
99 GST_STATIC_PAD_TEMPLATE ("src",
100     GST_PAD_SRC,
101     GST_PAD_ALWAYS,
102     GST_STATIC_CAPS_ANY);
103
104 GstStaticPadTemplate progress_report_sink_template =
105 GST_STATIC_PAD_TEMPLATE ("sink",
106     GST_PAD_SINK,
107     GST_PAD_ALWAYS,
108     GST_STATIC_CAPS_ANY);
109
110 static const GstElementDetails progress_report_details =
111 GST_ELEMENT_DETAILS ("Progress report",
112     "Testing",
113     "Periodically query and report on processing progress",
114     "Jan Schmidt <thaytan@mad.scientist.com>");
115
116 #define DEFAULT_UPDATE_FREQ  5
117 #define DEFAULT_SILENT       FALSE
118 #define DEFAULT_FORMAT       "auto"
119
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);
124
125 static gboolean gst_progress_report_event (GstBaseTransform * trans,
126     GstEvent * event);
127 static GstFlowReturn gst_progress_report_transform_ip (GstBaseTransform * trans,
128     GstBuffer * buf);
129
130 static gboolean gst_progress_report_start (GstBaseTransform * trans);
131 static gboolean gst_progress_report_stop (GstBaseTransform * trans);
132
133 GST_BOILERPLATE (GstProgressReport, gst_progress_report, GstBaseTransform,
134     GST_TYPE_BASE_TRANSFORM);
135
136 static void
137 gst_progress_report_base_init (gpointer g_class)
138 {
139   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
140
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));
145
146   gst_element_class_set_details (element_class, &progress_report_details);
147 }
148
149 static void
150 gst_progress_report_finalize (GObject * obj)
151 {
152   GstProgressReport *filter = GST_PROGRESS_REPORT (obj);
153
154   g_free (filter->format);
155   filter->format = NULL;
156
157   G_OBJECT_CLASS (parent_class)->finalize (obj);
158 }
159
160 static void
161 gst_progress_report_class_init (GstProgressReportClass * g_class)
162 {
163   GstBaseTransformClass *gstbasetrans_class;
164   GObjectClass *gobject_class;
165
166   gobject_class = G_OBJECT_CLASS (g_class);
167   gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (g_class);
168
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;
172
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));
177
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));
182
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));
186
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);
192 }
193
194 static void
195 gst_progress_report_init (GstProgressReport * report,
196     GstProgressReportClass * g_class)
197 {
198   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (report), TRUE);
199
200   report->update_freq = DEFAULT_UPDATE_FREQ;
201   report->silent = DEFAULT_SILENT;
202   report->format = g_strdup (DEFAULT_FORMAT);
203 }
204
205 static void
206 gst_progress_report_post_progress (GstProgressReport * filter,
207     GstFormat format, gint64 current, gint64 total)
208 {
209   GstStructure *s = NULL;
210
211   if (current >= 0 && total > 0) {
212     gdouble perc;
213
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);
217
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);
226   }
227
228   if (s) {
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);
233   }
234 }
235
236 static gboolean
237 gst_progress_report_do_query (GstProgressReport * filter, GstFormat format,
238     gint hh, gint mm, gint ss)
239 {
240   const gchar *format_name = NULL;
241   GstPad *sink_pad;
242   gint64 cur, total;
243
244   sink_pad = GST_BASE_TRANSFORM (filter)->sinkpad;
245
246   GST_LOG_OBJECT (filter, "querying using format %d (%s)", format,
247       gst_format_get_name (format));
248
249   if (!gst_pad_query_peer_position (sink_pad, &format, &cur) ||
250       !gst_pad_query_peer_duration (sink_pad, &format, &total)) {
251     return FALSE;
252   }
253
254   switch (format) {
255     case GST_FORMAT_BYTES:
256       format_name = "bytes";
257       break;
258     case GST_FORMAT_BUFFERS:
259       format_name = "buffers";
260       break;
261     case GST_FORMAT_PERCENT:
262       format_name = "percent";
263       break;
264     case GST_FORMAT_TIME:
265       format_name = "seconds";
266       cur /= GST_SECOND;
267       total /= GST_SECOND;
268       break;
269     case GST_FORMAT_DEFAULT:{
270       GstCaps *caps;
271
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);
277
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";
283         }
284       }
285       break;
286     }
287     default:{
288       const GstFormatDefinition *details;
289
290       details = gst_format_get_details (format);
291       if (details) {
292         format_name = details->nick;
293       } else {
294         format_name = "unknown";
295       }
296       break;
297     }
298   }
299
300   if (!filter->silent) {
301     if (total > 0) {
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);
305     } else {
306       g_print ("%s (%02d:%02d:%02d): %" G_GINT64_FORMAT " %s\n",
307           GST_OBJECT_NAME (filter), hh, mm, ss, cur, format_name);
308     }
309   }
310
311   gst_progress_report_post_progress (filter, format, cur, total);
312   return TRUE;
313 }
314
315 static void
316 gst_progress_report_report (GstProgressReport * filter, GTimeVal cur_time)
317 {
318   GstFormat try_formats[] = { GST_FORMAT_TIME, GST_FORMAT_BYTES,
319     GST_FORMAT_PERCENT, GST_FORMAT_BUFFERS,
320     GST_FORMAT_DEFAULT
321   };
322   GstMessage *msg;
323   GstFormat format = GST_FORMAT_UNDEFINED;
324   gboolean done = FALSE;
325   glong run_time;
326   gint hh, mm, ss;
327
328   run_time = cur_time.tv_sec - filter->start_time.tv_sec;
329
330   hh = (run_time / 3600) % 100;
331   mm = (run_time / 60) % 60;
332   ss = (run_time % 60);
333
334   GST_OBJECT_LOCK (filter);
335
336   if (filter->format != NULL && strcmp (filter->format, "auto") != 0) {
337     format = gst_format_get_by_nick (filter->format);
338   }
339
340   if (format != GST_FORMAT_UNDEFINED) {
341     done = gst_progress_report_do_query (filter, format, hh, mm, ss);
342   } else {
343     gint i;
344
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);
347       if (done)
348         break;
349     }
350   }
351
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);
355   }
356
357   msg = filter->pending_msg;
358   filter->pending_msg = NULL;
359   GST_OBJECT_UNLOCK (filter);
360
361   if (msg) {
362     gst_element_post_message (GST_ELEMENT_CAST (filter), msg);
363   }
364 }
365
366 static gboolean
367 gst_progress_report_event (GstBaseTransform * trans, GstEvent * event)
368 {
369   GstProgressReport *filter;
370
371   filter = GST_PROGRESS_REPORT (trans);
372
373   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
374     GTimeVal cur_time;
375
376     g_get_current_time (&cur_time);
377     gst_progress_report_report (filter, cur_time);
378   }
379   return GST_BASE_TRANSFORM_CLASS (parent_class)->event (trans, event);
380 }
381
382 static GstFlowReturn
383 gst_progress_report_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
384 {
385   GstProgressReport *filter;
386   gboolean need_update;
387   GTimeVal cur_time;
388
389   g_get_current_time (&cur_time);
390
391   filter = GST_PROGRESS_REPORT (trans);
392
393   /* Check if update_freq seconds have passed since the last update */
394   GST_OBJECT_LOCK (filter);
395   need_update =
396       ((cur_time.tv_sec - filter->last_report.tv_sec) >= filter->update_freq);
397   GST_OBJECT_UNLOCK (filter);
398
399   if (need_update) {
400     gst_progress_report_report (filter, cur_time);
401     GST_OBJECT_LOCK (filter);
402     filter->last_report = cur_time;
403     GST_OBJECT_UNLOCK (filter);
404   }
405
406   return GST_FLOW_OK;
407 }
408
409 static gboolean
410 gst_progress_report_start (GstBaseTransform * trans)
411 {
412   GstProgressReport *filter;
413
414   filter = GST_PROGRESS_REPORT (trans);
415
416   g_get_current_time (&filter->last_report);
417   filter->start_time = filter->last_report;
418
419   return TRUE;
420 }
421
422 static gboolean
423 gst_progress_report_stop (GstBaseTransform * trans)
424 {
425   /* anything we should be doing here? */
426   return TRUE;
427 }
428
429 static void
430 gst_progress_report_set_property (GObject * object, guint prop_id,
431     const GValue * value, GParamSpec * pspec)
432 {
433   GstProgressReport *filter;
434
435   filter = GST_PROGRESS_REPORT (object);
436
437   switch (prop_id) {
438     case ARG_UPDATE_FREQ:
439       GST_OBJECT_LOCK (filter);
440       filter->update_freq = g_value_get_int (value);
441       GST_OBJECT_UNLOCK (filter);
442       break;
443     case ARG_SILENT:
444       GST_OBJECT_LOCK (filter);
445       filter->silent = g_value_get_boolean (value);
446       GST_OBJECT_UNLOCK (filter);
447       break;
448     case ARG_FORMAT:
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);
455       break;
456     default:
457       break;
458   }
459 }
460
461 static void
462 gst_progress_report_get_property (GObject * object, guint prop_id,
463     GValue * value, GParamSpec * pspec)
464 {
465   GstProgressReport *filter;
466
467   filter = GST_PROGRESS_REPORT (object);
468
469   switch (prop_id) {
470     case ARG_UPDATE_FREQ:
471       GST_OBJECT_LOCK (filter);
472       g_value_set_int (value, filter->update_freq);
473       GST_OBJECT_UNLOCK (filter);
474       break;
475     case ARG_SILENT:
476       GST_OBJECT_LOCK (filter);
477       g_value_set_boolean (value, filter->silent);
478       GST_OBJECT_UNLOCK (filter);
479       break;
480     case ARG_FORMAT:
481       GST_OBJECT_LOCK (filter);
482       g_value_set_string (value, filter->format);
483       GST_OBJECT_UNLOCK (filter);
484       break;
485     default:
486       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
487       break;
488   }
489 }