Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / gst / debugutils / 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  *
26  * The progressreport element can be put into a pipeline to report progress,
27  * which is done by doing upstream duration and position queries in regular
28  * (real-time) intervals. Both the interval and the prefered query format
29  * can be specified via the #GstProgressReport:update-freq and the
30  * #GstProgressReport:format property.
31  *
32  * Element messages containing a "progress" structure are posted on the bus
33  * whenever progress has been queried (since gst-plugins-good 0.10.6 only).
34  *
35  * Since the element was originally designed for debugging purposes, it will
36  * by default also print information about the current progress to the
37  * terminal. This can be prevented by setting the #GstProgressReport:silent
38  * property to %TRUE.
39  *
40  * This element is most useful in transcoding pipelines or other situations
41  * where just querying the pipeline might not lead to the wanted result. For
42  * progress in TIME format, the element is best placed in a 'raw stream'
43  * section of the pipeline (or after any demuxers/decoders/parsers).
44  *
45  * Three more things should be pointed out: firstly, the element will only
46  * query progress when data flow happens. If data flow is stalled for some
47  * reason, no progress messages will be posted. Secondly, there are other
48  * elements (like qtdemux, for example) that may also post "progress" element
49  * messages on the bus. Applications should check the source of any element
50  * messages they receive, if needed. Finally, applications should not take
51  * action on receiving notification of progress being 100%, they should only
52  * take action when they receive an EOS message (since the progress reported
53  * is in reference to an internal point of a pipeline and not the pipeline as
54  * a whole).
55  *
56  * <refsect2>
57  * <title>Example launch line</title>
58  * |[
59  * gst-launch -m filesrc location=foo.ogg ! decodebin ! progressreport update-freq=1 ! audioconvert ! audioresample ! autoaudiosink
60  * ]| This shows a progress query where a duration is available.
61  * |[
62  * gst-launch -m audiotestsrc ! progressreport update-freq=1 ! audioconvert ! autoaudiosink
63  * ]| This shows a progress query where no duration is available.
64  * </refsect2>
65  */
66
67 #ifdef HAVE_CONFIG_H
68 #include "config.h"
69 #endif
70
71 #include <gst/gst.h>
72 #include <string.h>
73 #include <math.h>
74 #include <time.h>
75
76 #include "progressreport.h"
77
78
79 enum
80 {
81   ARG_0,
82   ARG_UPDATE_FREQ,
83   ARG_SILENT,
84   ARG_DO_QUERY,
85   ARG_FORMAT
86 };
87
88 GstStaticPadTemplate progress_report_src_template =
89 GST_STATIC_PAD_TEMPLATE ("src",
90     GST_PAD_SRC,
91     GST_PAD_ALWAYS,
92     GST_STATIC_CAPS_ANY);
93
94 GstStaticPadTemplate progress_report_sink_template =
95 GST_STATIC_PAD_TEMPLATE ("sink",
96     GST_PAD_SINK,
97     GST_PAD_ALWAYS,
98     GST_STATIC_CAPS_ANY);
99
100 #define DEFAULT_UPDATE_FREQ  5
101 #define DEFAULT_SILENT       FALSE
102 #define DEFAULT_DO_QUERY     TRUE
103 #define DEFAULT_FORMAT       "auto"
104
105 static void gst_progress_report_set_property (GObject * object, guint prop_id,
106     const GValue * value, GParamSpec * pspec);
107 static void gst_progress_report_get_property (GObject * object, guint prop_id,
108     GValue * value, GParamSpec * pspec);
109
110 static gboolean gst_progress_report_event (GstBaseTransform * trans,
111     GstEvent * event);
112 static GstFlowReturn gst_progress_report_transform_ip (GstBaseTransform * trans,
113     GstBuffer * buf);
114
115 static gboolean gst_progress_report_start (GstBaseTransform * trans);
116 static gboolean gst_progress_report_stop (GstBaseTransform * trans);
117
118 GST_BOILERPLATE (GstProgressReport, gst_progress_report, GstBaseTransform,
119     GST_TYPE_BASE_TRANSFORM);
120
121 static void
122 gst_progress_report_base_init (gpointer g_class)
123 {
124   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
125
126   gst_element_class_add_static_pad_template (element_class,
127       &progress_report_sink_template);
128   gst_element_class_add_static_pad_template (element_class,
129       &progress_report_src_template);
130
131   gst_element_class_set_details_simple (element_class, "Progress report",
132       "Testing",
133       "Periodically query and report on processing progress",
134       "Jan Schmidt <thaytan@mad.scientist.com>");
135 }
136
137 static void
138 gst_progress_report_finalize (GObject * obj)
139 {
140   GstProgressReport *filter = GST_PROGRESS_REPORT (obj);
141
142   g_free (filter->format);
143   filter->format = NULL;
144
145   G_OBJECT_CLASS (parent_class)->finalize (obj);
146 }
147
148 static void
149 gst_progress_report_class_init (GstProgressReportClass * g_class)
150 {
151   GstBaseTransformClass *gstbasetrans_class;
152   GObjectClass *gobject_class;
153
154   gobject_class = G_OBJECT_CLASS (g_class);
155   gstbasetrans_class = GST_BASE_TRANSFORM_CLASS (g_class);
156
157   gobject_class->finalize = gst_progress_report_finalize;
158   gobject_class->set_property = gst_progress_report_set_property;
159   gobject_class->get_property = gst_progress_report_get_property;
160
161   g_object_class_install_property (gobject_class,
162       ARG_UPDATE_FREQ, g_param_spec_int ("update-freq", "Update Frequency",
163           "Number of seconds between reports when data is flowing", 1, G_MAXINT,
164           DEFAULT_UPDATE_FREQ, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165
166   g_object_class_install_property (gobject_class,
167       ARG_SILENT, g_param_spec_boolean ("silent",
168           "Do not print output to stdout", "Do not print output to stdout",
169           DEFAULT_SILENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170
171   g_object_class_install_property (gobject_class,
172       ARG_DO_QUERY, g_param_spec_boolean ("do-query",
173           "Use a query instead of buffer metadata to determine stream position",
174           "Use a query instead of buffer metadata to determine stream position",
175           DEFAULT_DO_QUERY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176
177   g_object_class_install_property (gobject_class,
178       ARG_FORMAT, g_param_spec_string ("format", "format",
179           "Format to use for the querying", DEFAULT_FORMAT,
180           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181
182   gstbasetrans_class->event = GST_DEBUG_FUNCPTR (gst_progress_report_event);
183   gstbasetrans_class->transform_ip =
184       GST_DEBUG_FUNCPTR (gst_progress_report_transform_ip);
185   gstbasetrans_class->start = GST_DEBUG_FUNCPTR (gst_progress_report_start);
186   gstbasetrans_class->stop = GST_DEBUG_FUNCPTR (gst_progress_report_stop);
187 }
188
189 static void
190 gst_progress_report_init (GstProgressReport * report,
191     GstProgressReportClass * g_class)
192 {
193   gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (report), TRUE);
194
195   report->update_freq = DEFAULT_UPDATE_FREQ;
196   report->silent = DEFAULT_SILENT;
197   report->do_query = DEFAULT_DO_QUERY;
198   report->format = g_strdup (DEFAULT_FORMAT);
199 }
200
201 static void
202 gst_progress_report_post_progress (GstProgressReport * filter,
203     GstFormat format, gint64 current, gint64 total)
204 {
205   GstStructure *s = NULL;
206
207   if (current >= 0 && total > 0) {
208     gdouble perc;
209
210     perc = gst_util_guint64_to_gdouble (current) * 100.0 /
211         gst_util_guint64_to_gdouble (total);
212     perc = CLAMP (perc, 0.0, 100.0);
213
214     /* we provide a "percent" field of integer type to stay compatible
215      * with qtdemux, but add a second "percent-double" field for those who
216      * want more precision and are too lazy to calculate it themselves */
217     s = gst_structure_new ("progress", "percent", G_TYPE_INT, (gint) perc,
218         "percent-double", G_TYPE_DOUBLE, perc, "current", G_TYPE_INT64, current,
219         "total", G_TYPE_INT64, total, NULL);
220   } else if (current >= 0) {
221     s = gst_structure_new ("progress", "current", G_TYPE_INT64, current, NULL);
222   }
223
224   if (s) {
225     GST_LOG_OBJECT (filter, "posting progress message: %" GST_PTR_FORMAT, s);
226     gst_structure_set (s, "format", GST_TYPE_FORMAT, format, NULL);
227     /* can't post it right here because we're holding the object lock */
228     filter->pending_msg = gst_message_new_element (GST_OBJECT_CAST (filter), s);
229   }
230 }
231
232 static gboolean
233 gst_progress_report_do_query (GstProgressReport * filter, GstFormat format,
234     gint hh, gint mm, gint ss, GstBuffer * buf)
235 {
236   const gchar *format_name = NULL;
237   GstPad *sink_pad;
238   gint64 cur, total;
239
240   sink_pad = GST_BASE_TRANSFORM (filter)->sinkpad;
241
242   GST_LOG_OBJECT (filter, "querying using format %d (%s)", format,
243       gst_format_get_name (format));
244
245   if (filter->do_query || !buf) {
246     GST_LOG_OBJECT (filter, "using upstream query");
247     if (!gst_pad_query_peer_position (sink_pad, &format, &cur) ||
248         !gst_pad_query_peer_duration (sink_pad, &format, &total)) {
249       return FALSE;
250     }
251   } else {
252     GstBaseTransform *base = GST_BASE_TRANSFORM (filter);
253
254     GST_LOG_OBJECT (filter, "using buffer metadata");
255     if (format == GST_FORMAT_TIME && base->have_newsegment &&
256         base->segment.format == GST_FORMAT_TIME) {
257       cur = gst_segment_to_stream_time (&base->segment, format,
258           GST_BUFFER_TIMESTAMP (buf));
259       total = base->segment.duration;
260     } else {
261       return FALSE;
262     }
263   }
264
265   switch (format) {
266     case GST_FORMAT_BYTES:
267       format_name = "bytes";
268       break;
269     case GST_FORMAT_BUFFERS:
270       format_name = "buffers";
271       break;
272     case GST_FORMAT_PERCENT:
273       format_name = "percent";
274       break;
275     case GST_FORMAT_TIME:
276       format_name = "seconds";
277       cur /= GST_SECOND;
278       total /= GST_SECOND;
279       break;
280     case GST_FORMAT_DEFAULT:{
281       GstCaps *caps;
282
283       format_name = "bogounits";
284       caps = GST_PAD_CAPS (GST_BASE_TRANSFORM (filter)->sinkpad);
285       if (caps && gst_caps_is_fixed (caps) && !gst_caps_is_any (caps)) {
286         GstStructure *s = gst_caps_get_structure (caps, 0);
287         const gchar *mime_type = gst_structure_get_name (s);
288
289         if (g_str_has_prefix (mime_type, "video/") ||
290             g_str_has_prefix (mime_type, "image/")) {
291           format_name = "frames";
292         } else if (g_str_has_prefix (mime_type, "audio/")) {
293           format_name = "samples";
294         }
295       }
296       break;
297     }
298     default:{
299       const GstFormatDefinition *details;
300
301       details = gst_format_get_details (format);
302       if (details) {
303         format_name = details->nick;
304       } else {
305         format_name = "unknown";
306       }
307       break;
308     }
309   }
310
311   if (!filter->silent) {
312     if (total > 0) {
313       g_print ("%s (%02d:%02d:%02d): %" G_GINT64_FORMAT " / %"
314           G_GINT64_FORMAT " %s (%4.1f %%)\n", GST_OBJECT_NAME (filter), hh,
315           mm, ss, cur, total, format_name, (gdouble) cur / total * 100.0);
316     } else {
317       g_print ("%s (%02d:%02d:%02d): %" G_GINT64_FORMAT " %s\n",
318           GST_OBJECT_NAME (filter), hh, mm, ss, cur, format_name);
319     }
320   }
321
322   gst_progress_report_post_progress (filter, format, cur, total);
323   return TRUE;
324 }
325
326 static void
327 gst_progress_report_report (GstProgressReport * filter, GTimeVal cur_time,
328     GstBuffer * buf)
329 {
330   GstFormat try_formats[] = { GST_FORMAT_TIME, GST_FORMAT_BYTES,
331     GST_FORMAT_PERCENT, GST_FORMAT_BUFFERS,
332     GST_FORMAT_DEFAULT
333   };
334   GstMessage *msg;
335   GstFormat format = GST_FORMAT_UNDEFINED;
336   gboolean done = FALSE;
337   glong run_time;
338   gint hh, mm, ss;
339
340   run_time = cur_time.tv_sec - filter->start_time.tv_sec;
341
342   hh = (run_time / 3600) % 100;
343   mm = (run_time / 60) % 60;
344   ss = (run_time % 60);
345
346   GST_OBJECT_LOCK (filter);
347
348   if (filter->format != NULL && strcmp (filter->format, "auto") != 0) {
349     format = gst_format_get_by_nick (filter->format);
350   }
351
352   if (format != GST_FORMAT_UNDEFINED) {
353     done = gst_progress_report_do_query (filter, format, hh, mm, ss, buf);
354   } else {
355     gint i;
356
357     for (i = 0; i < G_N_ELEMENTS (try_formats); ++i) {
358       done = gst_progress_report_do_query (filter, try_formats[i], hh, mm, ss,
359           buf);
360       if (done)
361         break;
362     }
363   }
364
365   if (!done && !filter->silent) {
366     g_print ("%s (%2d:%2d:%2d): Could not query position and/or duration\n",
367         GST_OBJECT_NAME (filter), hh, mm, ss);
368   }
369
370   msg = filter->pending_msg;
371   filter->pending_msg = NULL;
372   GST_OBJECT_UNLOCK (filter);
373
374   if (msg) {
375     gst_element_post_message (GST_ELEMENT_CAST (filter), msg);
376   }
377 }
378
379 static gboolean
380 gst_progress_report_event (GstBaseTransform * trans, GstEvent * event)
381 {
382   GstProgressReport *filter;
383
384   filter = GST_PROGRESS_REPORT (trans);
385
386   if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
387     GTimeVal cur_time;
388
389     g_get_current_time (&cur_time);
390     gst_progress_report_report (filter, cur_time, NULL);
391   }
392   return GST_BASE_TRANSFORM_CLASS (parent_class)->event (trans, event);
393 }
394
395 static GstFlowReturn
396 gst_progress_report_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
397 {
398   GstProgressReport *filter;
399   gboolean need_update;
400   GTimeVal cur_time;
401
402   g_get_current_time (&cur_time);
403
404   filter = GST_PROGRESS_REPORT (trans);
405
406   /* Check if update_freq seconds have passed since the last update */
407   GST_OBJECT_LOCK (filter);
408   need_update =
409       ((cur_time.tv_sec - filter->last_report.tv_sec) >= filter->update_freq);
410   GST_OBJECT_UNLOCK (filter);
411
412   if (need_update) {
413     gst_progress_report_report (filter, cur_time, buf);
414     GST_OBJECT_LOCK (filter);
415     filter->last_report = cur_time;
416     GST_OBJECT_UNLOCK (filter);
417   }
418
419   return GST_FLOW_OK;
420 }
421
422 static gboolean
423 gst_progress_report_start (GstBaseTransform * trans)
424 {
425   GstProgressReport *filter;
426
427   filter = GST_PROGRESS_REPORT (trans);
428
429   g_get_current_time (&filter->last_report);
430   filter->start_time = filter->last_report;
431
432   return TRUE;
433 }
434
435 static gboolean
436 gst_progress_report_stop (GstBaseTransform * trans)
437 {
438   /* anything we should be doing here? */
439   return TRUE;
440 }
441
442 static void
443 gst_progress_report_set_property (GObject * object, guint prop_id,
444     const GValue * value, GParamSpec * pspec)
445 {
446   GstProgressReport *filter;
447
448   filter = GST_PROGRESS_REPORT (object);
449
450   switch (prop_id) {
451     case ARG_UPDATE_FREQ:
452       GST_OBJECT_LOCK (filter);
453       filter->update_freq = g_value_get_int (value);
454       GST_OBJECT_UNLOCK (filter);
455       break;
456     case ARG_SILENT:
457       GST_OBJECT_LOCK (filter);
458       filter->silent = g_value_get_boolean (value);
459       GST_OBJECT_UNLOCK (filter);
460       break;
461     case ARG_DO_QUERY:
462       GST_OBJECT_LOCK (filter);
463       filter->do_query = g_value_get_boolean (value);
464       GST_OBJECT_UNLOCK (filter);
465       break;
466     case ARG_FORMAT:
467       GST_OBJECT_LOCK (filter);
468       g_free (filter->format);
469       filter->format = g_value_dup_string (value);
470       if (filter->format == NULL)
471         filter->format = g_strdup ("auto");
472       GST_OBJECT_UNLOCK (filter);
473       break;
474     default:
475       break;
476   }
477 }
478
479 static void
480 gst_progress_report_get_property (GObject * object, guint prop_id,
481     GValue * value, GParamSpec * pspec)
482 {
483   GstProgressReport *filter;
484
485   filter = GST_PROGRESS_REPORT (object);
486
487   switch (prop_id) {
488     case ARG_UPDATE_FREQ:
489       GST_OBJECT_LOCK (filter);
490       g_value_set_int (value, filter->update_freq);
491       GST_OBJECT_UNLOCK (filter);
492       break;
493     case ARG_SILENT:
494       GST_OBJECT_LOCK (filter);
495       g_value_set_boolean (value, filter->silent);
496       GST_OBJECT_UNLOCK (filter);
497       break;
498     case ARG_DO_QUERY:
499       GST_OBJECT_LOCK (filter);
500       g_value_set_boolean (value, filter->do_query);
501       GST_OBJECT_UNLOCK (filter);
502       break;
503     case ARG_FORMAT:
504       GST_OBJECT_LOCK (filter);
505       g_value_set_string (value, filter->format);
506       GST_OBJECT_UNLOCK (filter);
507       break;
508     default:
509       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
510       break;
511   }
512 }