further i18n: call setlocale()
[platform/upstream/gstreamer.git] / tools / gst-md5sum.c
1 #ifdef HAVE_CONFIG_H
2 #  include "config.h"
3 #endif
4
5 #include <string.h>
6 #include <stdlib.h>
7 #include <gst/gst.h>
8 #include <locale.h>
9
10 static guint64 iterations = 0;
11 static guint64 sum = 0;
12 static guint64 min = G_MAXINT64;
13 static guint64 max = 0;
14 static GstClock *s_clock;
15
16 gboolean
17 idle_func (gpointer data)
18 {
19   gboolean busy;
20   GTimeVal tfthen, tfnow;
21   GstClockTimeDiff diff;
22
23   if (s_clock) {
24     //g_print ("%lld\n", gst_clock_get_time (s_clock));
25   }
26
27   g_get_current_time (&tfthen);
28   busy = gst_bin_iterate (GST_BIN (data));
29   iterations++;
30   g_get_current_time (&tfnow);
31
32   diff = GST_TIMEVAL_TO_TIME (tfnow) -
33          GST_TIMEVAL_TO_TIME (tfthen);
34
35   sum += diff; 
36   min = MIN (min, diff);
37   max = MAX (max, diff);
38
39   if (!busy) {
40     gst_main_quit ();
41     /*
42     g_print ("execution ended after %llu iterations (sum %llu ns, average %llu ns, min %llu ns, max %llu ns)\n", 
43                     iterations, sum, sum/iterations, min, max);
44                     */
45   }
46
47   return busy;
48 }
49
50 int
51 main (int argc, char *argv[])
52 {
53   /* options */
54   gboolean verbose = FALSE;
55   gchar *exclude_args = NULL;
56   struct poptOption options[] = {
57     {"verbose", 'v',  POPT_ARG_NONE|POPT_ARGFLAG_STRIP,   &verbose,   0,
58      "do not output status information", NULL},
59     POPT_TABLEEND
60   };
61
62   GstElement *pipeline;
63   gchar **argvn;
64   GError *error = NULL;
65   GstElement *md5sink;
66   gchar *md5string = g_malloc0 (33);
67
68   free (malloc (8)); /* -lefence */
69
70   setlocale (LC_ALL, "");
71
72   gst_init_with_popt_table (&argc, &argv, options);
73   
74   /* make a null-terminated version of argv with ! md5sink appended 
75    * ! is stored in argvn[argc - 1], md5sink in argvn[argc],
76    * NULL pointer in argvn[argc + 1] */
77   argvn = g_new0 (char *, argc + 2);
78   memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
79   argvn[argc - 1] = g_strdup_printf ("!");
80   argvn[argc] = g_strdup_printf ("md5sink");
81
82   pipeline = (GstElement*) gst_parse_launchv ((const gchar**) argvn, &error);
83
84   if (!pipeline) {
85     if (error)
86     {
87       g_warning ("pipeline could not be constructed: %s\n", error->message);
88       g_error_free (error);
89     }
90     else
91       g_warning ("pipeline could not be constructed\n");
92     return 1;
93   }
94   
95   if (verbose)
96   {
97     gchar **exclude_list = exclude_args ? g_strsplit (exclude_args, ",", 0) 
98                                         : NULL;
99     g_signal_connect (pipeline, "deep_notify", 
100                       G_CALLBACK (gst_element_default_deep_notify), 
101                       exclude_list);
102   }
103   g_signal_connect (pipeline, "error", 
104                     G_CALLBACK (gst_element_default_error), NULL);
105
106   if (gst_element_set_state (pipeline, GST_STATE_PLAYING) != GST_STATE_SUCCESS)
107   {
108     g_warning ("pipeline doesn't want to play\n");
109     return 0;
110   }
111   
112   if (!GST_FLAG_IS_SET (GST_OBJECT (pipeline), GST_BIN_SELF_SCHEDULABLE)) {
113     g_idle_add (idle_func, pipeline);
114     gst_main ();
115   } else { 
116     gst_element_wait_state_change (pipeline);
117   }
118
119   gst_element_set_state (pipeline, GST_STATE_NULL);
120
121   /* print out md5sink here */
122   md5sink = gst_bin_get_by_name (GST_BIN (pipeline), "md5sink0");
123   g_assert (md5sink);
124   g_object_get (G_OBJECT (md5sink), "md5", &md5string, NULL);
125   printf ("%s\n", md5string);
126
127   gst_object_unref (GST_OBJECT (pipeline));
128
129   return 0;
130 }