backports and fixes
[platform/upstream/gstreamer.git] / tools / gst-launch.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <gst/gst.h>
4
5 static guint64 iterations = 0;
6 static guint64 sum = 0;
7 static guint64 min = G_MAXINT64;
8 static guint64 max = 0;
9 static GstClock *s_clock;
10
11 gboolean
12 idle_func (gpointer data)
13 {
14   gboolean busy;
15   GTimeVal tfthen, tfnow;
16   GstClockTimeDiff diff;
17
18   if (s_clock) {
19     //g_print ("%lld\n", gst_clock_get_time (s_clock));
20   }
21
22   g_get_current_time (&tfthen);
23   busy = gst_bin_iterate (GST_BIN (data));
24   iterations++;
25   g_get_current_time (&tfnow);
26
27   diff = GST_TIMEVAL_TO_TIME (tfnow) -
28          GST_TIMEVAL_TO_TIME (tfthen);
29
30   sum += diff; 
31   min = MIN (min, diff);
32   max = MAX (max, diff);
33
34   if (!busy) {
35     gst_main_quit ();
36     g_print ("execution ended after %llu iterations (sum %llu ns, average %llu ns, min %llu ns, max %llu ns)\n", 
37                     iterations, sum, sum/iterations, min, max);
38   }
39
40   return busy;
41 }
42
43 static GstElement*
44 xmllaunch_parse_cmdline (const gchar **argv) 
45 {
46   GstElement *pipeline = NULL, *e;
47   GstXML *xml;
48   gboolean err;
49   const gchar *arg;
50   gchar *element, *property, *value;
51   GList *l;
52   gint i = 0;
53   
54   if (!(arg = argv[0])) {
55     g_print ("usage: gst-xmllaunch <file.xml> [ element.property=value ... ]\n");
56     exit (1);
57   }
58   
59   xml = gst_xml_new ();
60   err = gst_xml_parse_file(xml, arg, NULL);
61   
62   if (err != TRUE) {
63     fprintf (stderr, "ERROR: parse of xml file '%s' failed\n", arg);
64     exit (1);
65   }
66   
67   l = gst_xml_get_topelements (xml);
68   if (!l) {
69     fprintf (stderr, "ERROR: no toplevel pipeline element in file '%s'\n", arg);
70     exit (1);
71   }
72     
73   if (l->next)
74     g_warning ("only one toplevel element is supported at this time");
75   
76   pipeline = GST_ELEMENT (l->data);
77   
78   while ((arg = argv[++i])) {
79     element = g_strdup (arg);
80     property = strchr (element, '.');
81     value = strchr (element, '=');
82     
83     if (!(element < property && property < value)) {
84       fprintf (stderr, "ERROR: could not parse command line argument %d: %s", i, element);
85       g_free (element);
86       exit (1);
87     }
88     
89     *property++ = '\0';
90     *value++ = '\0';
91     
92     e = gst_bin_get_by_name (GST_BIN (pipeline), element);
93     if (!e) {
94       g_warning ("element named '%s' not found", element);
95     } else {
96       gst_util_set_object_arg (G_OBJECT (e), property, value);
97     }
98     g_free (element);
99   }
100   
101   if (!l)
102     return NULL;
103   else
104     return l->data;
105 }
106
107 int
108 main(int argc, char *argv[])
109 {
110   /* options */
111   gboolean silent = FALSE;
112   gchar *savefile = NULL;
113   gchar *exclude_args = NULL;
114   struct poptOption options[] = {
115     {"silent",  's',  POPT_ARG_NONE|POPT_ARGFLAG_STRIP,   &silent,   0,
116      "do not output status information", NULL},
117     {"exclude", 'X',  POPT_ARG_STRING|POPT_ARGFLAG_STRIP, &exclude_args,  0,
118      "do not output status information of TYPE", "TYPE1,TYPE2,..."},
119     {"output",  'o',  POPT_ARG_STRING|POPT_ARGFLAG_STRIP, &savefile, 0,
120      "save xml representation of pipeline to FILE and exit", "FILE"},
121     POPT_TABLEEND
122   };
123
124   GstElement *pipeline;
125   gchar **argvn;
126   GError *error = NULL;
127
128   free (malloc (8)); /* -lefence */
129
130   gst_init_with_popt_table (&argc, &argv, options);
131   
132   /* make a null-terminated version of argv */
133   argvn = g_new0 (char*, argc);
134   memcpy (argvn, argv+1, sizeof (char*) * (argc-1));
135   if (strstr (argv[0], "gst-xmllaunch")) {
136     pipeline = xmllaunch_parse_cmdline ((const gchar**)argvn);
137   } else {
138     pipeline = (GstElement*) gst_parse_launchv ((const gchar**)argvn, &error);
139   }
140   g_free (argvn);
141   g_free (argvn);
142
143   if (!pipeline) {
144     if (error)
145       fprintf(stderr, "ERROR: pipeline could not be constructed: %s\n", error->message);
146     else
147       fprintf(stderr, "ERROR: pipeline could not be constructed\n");
148     exit(1);
149   }
150   
151   if (!silent)
152   {
153     gchar **exclude_list = exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
154     g_signal_connect (pipeline, "deep_notify", G_CALLBACK (gst_element_default_deep_notify), exclude_list);
155   }
156   g_signal_connect (pipeline, "error", G_CALLBACK (gst_element_default_error), NULL);
157   
158 #ifndef GST_DISABLE_LOADSAVE
159   if (savefile) {
160     gst_xml_write_file (GST_ELEMENT (pipeline), fopen (savefile, "w"));
161   }
162 #endif
163   
164   if (!savefile) {
165     gst_buffer_print_stats();
166     gst_event_print_stats();
167
168     fprintf(stderr,"RUNNING pipeline\n");
169     if (gst_element_set_state (pipeline, GST_STATE_PLAYING) != GST_STATE_SUCCESS) {
170       fprintf(stderr,"pipeline doesn't want to play\n");
171       exit (-1);
172     }
173
174     s_clock = gst_bin_get_clock (GST_BIN (pipeline));
175
176     if (!GST_FLAG_IS_SET (GST_OBJECT (pipeline), GST_BIN_SELF_SCHEDULABLE)) {
177         g_idle_add (idle_func, pipeline);
178         gst_main ();
179     } else {
180         g_print ("waiting for the state change...\n");
181         gst_element_wait_state_change (pipeline);
182         g_print ("got the state change...\n");
183     }
184
185     gst_element_set_state (pipeline, GST_STATE_NULL);
186     gst_buffer_print_stats();
187     gst_event_print_stats();
188
189   }
190   gst_object_unref (GST_OBJECT (pipeline));
191
192   return 0;
193 }