tools/gst-launch.1.in: Give example for network streaming (#351998)
[platform/upstream/gstreamer.git] / tools / gst-launch.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *               2000 Wim Taymans <wtay@chello.be>
4  *               2004 Thomas Vander Stichele <thomas@apestaart.org>
5  *
6  * gst-launch.c: tool to launch GStreamer pipelines from the command line
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 /* FIXME: hack alert */
29 #ifdef WIN32
30 #define DISABLE_FAULT_HANDLER
31 #endif
32
33 #include <string.h>
34 #include <signal.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #ifndef DISABLE_FAULT_HANDLER
39 #include <sys/wait.h>
40 #endif
41 #include <locale.h>             /* for LC_ALL */
42 #include "tools.h"
43
44 /* FIXME: This is just a temporary hack.  We should have a better
45  * check for siginfo handling. */
46 #ifdef SA_SIGINFO
47 #define USE_SIGINFO
48 #endif
49
50 extern volatile gboolean glib_on_error_halt;
51
52 #ifndef DISABLE_FAULT_HANDLER
53 static void fault_restore (void);
54 static void fault_spin (void);
55 static void sigint_restore (void);
56 static gboolean caught_intr = FALSE;
57 #endif
58
59 static GstElement *pipeline;
60 static gboolean caught_error = FALSE;
61 static gboolean tags = FALSE;
62 static gboolean messages = FALSE;
63
64
65 #ifndef GST_DISABLE_LOADSAVE
66 static GstElement *
67 xmllaunch_parse_cmdline (const gchar ** argv)
68 {
69   GstElement *pipeline = NULL, *e;
70   GstXML *xml;
71   gboolean err;
72   const gchar *arg;
73   gchar *element, *property, *value;
74   GList *l;
75   gint i = 0;
76
77   if (!(arg = argv[0])) {
78     g_print (_
79         ("Usage: gst-xmllaunch <file.xml> [ element.property=value ... ]\n"));
80     exit (1);
81   }
82
83   xml = gst_xml_new ();
84   /* FIXME guchar from gstxml.c */
85   err = gst_xml_parse_file (xml, (guchar *) arg, NULL);
86
87   if (err != TRUE) {
88     fprintf (stderr, _("ERROR: parse of xml file '%s' failed.\n"), arg);
89     exit (1);
90   }
91
92   l = gst_xml_get_topelements (xml);
93   if (!l) {
94     fprintf (stderr, _("ERROR: no toplevel pipeline element in file '%s'.\n"),
95         arg);
96     exit (1);
97   }
98
99   if (l->next)
100     fprintf (stderr,
101         _("WARNING: only one toplevel element is supported at this time."));
102
103   pipeline = GST_ELEMENT (l->data);
104
105   while ((arg = argv[++i])) {
106     element = g_strdup (arg);
107     property = strchr (element, '.');
108     value = strchr (element, '=');
109
110     if (!(element < property && property < value)) {
111       fprintf (stderr,
112           _("ERROR: could not parse command line argument %d: %s.\n"), i,
113           element);
114       g_free (element);
115       exit (1);
116     }
117
118     *property++ = '\0';
119     *value++ = '\0';
120
121     e = gst_bin_get_by_name (GST_BIN (pipeline), element);
122     if (!e) {
123       fprintf (stderr, _("WARNING: element named '%s' not found.\n"), element);
124     } else {
125       gst_util_set_object_arg (G_OBJECT (e), property, value);
126     }
127     g_free (element);
128   }
129
130   if (!l)
131     return NULL;
132
133   gst_object_ref (pipeline);
134   gst_object_unref (xml);
135   return pipeline;
136 }
137 #endif
138
139 #ifndef DISABLE_FAULT_HANDLER
140 #ifndef USE_SIGINFO
141 static void
142 fault_handler_sighandler (int signum)
143 {
144   fault_restore ();
145
146   /* printf is used instead of g_print(), since it's less likely to
147    * deadlock */
148   switch (signum) {
149     case SIGSEGV:
150       printf ("Caught SIGSEGV\n");
151       break;
152     case SIGQUIT:
153       printf ("Caught SIGQUIT\n");
154       break;
155     default:
156       printf ("signo:  %d\n", signum);
157       break;
158   }
159
160   fault_spin ();
161 }
162
163 #else /* USE_SIGINFO */
164
165 static void
166 fault_handler_sigaction (int signum, siginfo_t * si, void *misc)
167 {
168   fault_restore ();
169
170   /* printf is used instead of g_print(), since it's less likely to
171    * deadlock */
172   switch (si->si_signo) {
173     case SIGSEGV:
174       printf ("Caught SIGSEGV accessing address %p\n", si->si_addr);
175       break;
176     case SIGQUIT:
177       printf ("Caught SIGQUIT\n");
178       break;
179     default:
180       printf ("signo:  %d\n", si->si_signo);
181       printf ("errno:  %d\n", si->si_errno);
182       printf ("code:   %d\n", si->si_code);
183       break;
184   }
185
186   fault_spin ();
187 }
188 #endif /* USE_SIGINFO */
189
190 static void
191 fault_spin (void)
192 {
193   int spinning = TRUE;
194
195   glib_on_error_halt = FALSE;
196   g_on_error_stack_trace ("gst-launch");
197
198   wait (NULL);
199
200   /* FIXME how do we know if we were run by libtool? */
201   printf ("Spinning.  Please run 'gdb gst-launch %d' to continue debugging, "
202       "Ctrl-C to quit, or Ctrl-\\ to dump core.\n", (gint) getpid ());
203   while (spinning)
204     g_usleep (1000000);
205 }
206
207 static void
208 fault_restore (void)
209 {
210   struct sigaction action;
211
212   memset (&action, 0, sizeof (action));
213   action.sa_handler = SIG_DFL;
214
215   sigaction (SIGSEGV, &action, NULL);
216   sigaction (SIGQUIT, &action, NULL);
217 }
218
219 static void
220 fault_setup (void)
221 {
222   struct sigaction action;
223
224   memset (&action, 0, sizeof (action));
225 #ifdef USE_SIGINFO
226   action.sa_sigaction = fault_handler_sigaction;
227   action.sa_flags = SA_SIGINFO;
228 #else
229   action.sa_handler = fault_handler_sighandler;
230 #endif
231
232   sigaction (SIGSEGV, &action, NULL);
233   sigaction (SIGQUIT, &action, NULL);
234 }
235 #endif /* DISABLE_FAULT_HANDLER */
236
237 static void
238 print_tag (const GstTagList * list, const gchar * tag, gpointer unused)
239 {
240   gint i, count;
241
242   count = gst_tag_list_get_tag_size (list, tag);
243
244   for (i = 0; i < count; i++) {
245     gchar *str;
246
247     if (gst_tag_get_type (tag) == G_TYPE_STRING) {
248       if (!gst_tag_list_get_string_index (list, tag, i, &str))
249         g_assert_not_reached ();
250     } else if (gst_tag_get_type (tag) == GST_TYPE_BUFFER) {
251       GstBuffer *img;
252
253       img = gst_value_get_buffer (gst_tag_list_get_value_index (list, tag, i));
254       if (img) {
255         gchar *caps_str;
256
257         caps_str = GST_BUFFER_CAPS (img) ?
258             gst_caps_to_string (GST_BUFFER_CAPS (img)) : g_strdup ("unknown");
259         str = g_strdup_printf ("buffer of %u bytes, type: %s",
260             GST_BUFFER_SIZE (img), caps_str);
261         g_free (caps_str);
262       } else {
263         str = g_strdup ("NULL buffer");
264       }
265     } else {
266       str =
267           g_strdup_value_contents (gst_tag_list_get_value_index (list, tag, i));
268     }
269
270     if (i == 0) {
271       g_print ("%16s: %s\n", gst_tag_get_nick (tag), str);
272     } else {
273       g_print ("%16s: %s\n", "", str);
274     }
275
276     g_free (str);
277   }
278 }
279
280 #ifndef DISABLE_FAULT_HANDLER
281 /* we only use sighandler here because the registers are not important */
282 static void
283 sigint_handler_sighandler (int signum)
284 {
285   g_print ("Caught interrupt -- ");
286
287   sigint_restore ();
288
289   /* we set a flag that is checked by the mainloop, we cannot do much in the
290    * interrupt handler (no mutex or other blocking stuff) */
291   caught_intr = TRUE;
292 }
293
294 /* is called every 50 milliseconds (20 times a second), the interrupt handler
295  * will set a flag for us. We react to this by posting a message. */
296 static gboolean
297 check_intr (GstElement * pipeline)
298 {
299   if (!caught_intr) {
300     return TRUE;
301   } else {
302     caught_intr = FALSE;
303     g_print ("handling interrupt.\n");
304
305     /* post an application specific message */
306     gst_element_post_message (GST_ELEMENT (pipeline),
307         gst_message_new_application (GST_OBJECT (pipeline),
308             gst_structure_new ("GstLaunchInterrupt",
309                 "message", G_TYPE_STRING, "Pipeline interrupted", NULL)));
310
311     /* remove timeout handler */
312     return FALSE;
313   }
314 }
315
316 static void
317 sigint_setup (void)
318 {
319   struct sigaction action;
320
321   memset (&action, 0, sizeof (action));
322   action.sa_handler = sigint_handler_sighandler;
323
324   sigaction (SIGINT, &action, NULL);
325 }
326
327 static void
328 sigint_restore (void)
329 {
330   struct sigaction action;
331
332   memset (&action, 0, sizeof (action));
333   action.sa_handler = SIG_DFL;
334
335   sigaction (SIGINT, &action, NULL);
336 }
337
338 static void
339 play_handler (int signum)
340 {
341   switch (signum) {
342     case SIGUSR1:
343       g_print ("Caught SIGUSR1 - Play request.\n");
344       gst_element_set_state (pipeline, GST_STATE_PLAYING);
345       break;
346     case SIGUSR2:
347       g_print ("Caught SIGUSR2 - Stop request.\n");
348       gst_element_set_state (pipeline, GST_STATE_NULL);
349       break;
350   }
351 }
352
353 static void
354 play_signal_setup (void)
355 {
356   struct sigaction action;
357
358   memset (&action, 0, sizeof (action));
359   action.sa_handler = play_handler;
360   sigaction (SIGUSR1, &action, NULL);
361   sigaction (SIGUSR2, &action, NULL);
362 }
363 #endif /* DISABLE_FAULT_HANDLER */
364
365 /* returns TRUE if there was an error or we caught a keyboard interrupt. */
366 static gboolean
367 event_loop (GstElement * pipeline, gboolean blocking, GstState target_state)
368 {
369   GstBus *bus;
370   GstMessage *message = NULL;
371   gboolean res = FALSE;
372   gboolean buffering = FALSE;
373
374   bus = gst_element_get_bus (GST_ELEMENT (pipeline));
375
376 #ifndef DISABLE_FAULT_HANDLER
377   g_timeout_add (50, (GSourceFunc) check_intr, pipeline);
378 #endif
379
380   while (TRUE) {
381     message = gst_bus_poll (bus, GST_MESSAGE_ANY, blocking ? -1 : 0);
382
383     /* if the poll timed out, only when !blocking */
384     if (message == NULL)
385       goto exit;
386
387     /* check if we need to dump messages to the console */
388     if (messages) {
389       const GstStructure *s;
390
391       s = gst_message_get_structure (message);
392
393       g_print (_("Got Message from element \"%s\" (%s): "),
394           GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
395           gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
396       if (s) {
397         gchar *sstr;
398
399         sstr = gst_structure_to_string (s);
400         g_print ("%s\n", sstr);
401         g_free (sstr);
402       } else {
403         g_print ("no message details\n");
404       }
405     }
406
407     switch (GST_MESSAGE_TYPE (message)) {
408       case GST_MESSAGE_NEW_CLOCK:
409       {
410         GstClock *clock;
411
412         gst_message_parse_new_clock (message, &clock);
413
414         g_print ("New clock: %s\n", GST_OBJECT_NAME (clock));
415         break;
416       }
417       case GST_MESSAGE_EOS:
418         g_print (_
419             ("Got EOS from element \"%s\".\n"),
420             GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
421         goto exit;
422       case GST_MESSAGE_TAG:
423         if (tags) {
424           GstTagList *tags;
425
426           gst_message_parse_tag (message, &tags);
427           g_print (_("FOUND TAG      : found by element \"%s\".\n"),
428               GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))));
429           gst_tag_list_foreach (tags, print_tag, NULL);
430           gst_tag_list_free (tags);
431         }
432         break;
433       case GST_MESSAGE_WARNING:{
434         GError *gerror;
435         gchar *debug;
436
437         gst_message_parse_warning (message, &gerror, &debug);
438         if (debug) {
439           g_print ("WARNING: Element \"%s\" warns: %s\n",
440               GST_STR_NULL (GST_ELEMENT_NAME (GST_MESSAGE_SRC (message))),
441               debug);
442         }
443         if (gerror)
444           g_error_free (gerror);
445         g_free (debug);
446         break;
447       }
448       case GST_MESSAGE_ERROR:{
449         GError *gerror;
450         gchar *debug;
451
452         gst_message_parse_error (message, &gerror, &debug);
453         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
454         if (gerror)
455           g_error_free (gerror);
456         g_free (debug);
457         /* we have an error */
458         res = TRUE;
459         goto exit;
460       }
461       case GST_MESSAGE_STATE_CHANGED:{
462         GstState old, new, pending;
463
464         gst_message_parse_state_changed (message, &old, &new, &pending);
465
466         /* we only care about pipeline state change messages */
467         if (GST_MESSAGE_SRC (message) != GST_OBJECT_CAST (pipeline))
468           break;
469
470         /* ignore when we are buffering since then we mess with the states
471          * ourselves. */
472         if (buffering)
473           break;
474
475         /* if we reached the final target state, exit */
476         if (target_state == GST_STATE_PAUSED && new == target_state)
477           goto exit;
478
479         /* else not an interesting message */
480         break;
481       }
482       case GST_MESSAGE_BUFFERING:{
483         gint percent;
484
485         gst_message_parse_buffering (message, &percent);
486         fprintf (stderr, "buffering... %d\r", percent);
487
488         if (percent == 100) {
489           /* a 100% message means buffering is done */
490           buffering = FALSE;
491           /* if the desired state is playing, go back */
492           if (target_state == GST_STATE_PLAYING) {
493             fprintf (stderr, _("Setting pipeline to PLAYING ...\n"));
494             gst_element_set_state (pipeline, GST_STATE_PLAYING);
495           } else
496             goto exit;
497         } else {
498           /* buffering busy */
499           if (buffering == FALSE && target_state == GST_STATE_PLAYING) {
500             /* we were not buffering but PLAYING, PAUSE  the pipeline. */
501             fprintf (stderr, _("Setting pipeline to PAUSED ...\n"));
502             gst_element_set_state (pipeline, GST_STATE_PAUSED);
503             buffering = TRUE;
504           }
505         }
506         break;
507       }
508       case GST_MESSAGE_APPLICATION:{
509         const GstStructure *s;
510
511         s = gst_message_get_structure (message);
512
513         if (gst_structure_has_name (s, "GstLaunchInterrupt")) {
514           /* this application message is posted when we caught an interrupt and
515            * we need to stop the pipeline. */
516           fprintf (stderr, _("Interrupt: Setting pipeline to PAUSED ...\n"));
517           gst_element_set_state (pipeline, GST_STATE_PAUSED);
518           /* return TRUE when we caught an interrupt */
519           res = TRUE;
520           goto exit;
521         }
522       }
523       default:
524         /* just be quiet by default */
525         break;
526     }
527     if (message)
528       gst_message_unref (message);
529   }
530   g_assert_not_reached ();
531
532 exit:
533   {
534     if (message)
535       gst_message_unref (message);
536     gst_object_unref (bus);
537     return res;
538   }
539 }
540
541 int
542 main (int argc, char *argv[])
543 {
544   gint i, j;
545
546   /* options */
547   gboolean verbose = FALSE;
548   gboolean no_fault = FALSE;
549   gboolean trace = FALSE;
550   gchar *savefile = NULL;
551   gchar *exclude_args = NULL;
552   GOptionEntry options[] = {
553     {"tags", 't', 0, G_OPTION_ARG_NONE, &tags,
554         N_("Output tags (also known as metadata)"), NULL},
555     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
556         N_("Output status information and property notifications"), NULL},
557     {"messages", 'm', 0, G_OPTION_ARG_NONE, &messages,
558         N_("Output messages"), NULL},
559     {"exclude", 'X', 0, G_OPTION_ARG_NONE, &exclude_args,
560         N_("Do not output status information of TYPE"), N_("TYPE1,TYPE2,...")},
561 #ifndef GST_DISABLE_LOADSAVE
562     {"output", 'o', 0, G_OPTION_ARG_STRING, &savefile,
563         N_("Save xml representation of pipeline to FILE and exit"), N_("FILE")},
564 #endif
565     {"no-fault", 'f', 0, G_OPTION_ARG_NONE, &no_fault,
566         N_("Do not install a fault handler"), NULL},
567     {"trace", 'T', 0, G_OPTION_ARG_NONE, &trace,
568         N_("Print alloc trace (if enabled at compile time)"), NULL},
569     GST_TOOLS_GOPTION_VERSION,
570     {NULL}
571   };
572   GOptionContext *ctx;
573   GError *err = NULL;
574   gchar **argvn;
575   GError *error = NULL;
576   gint res = 0;
577
578   free (malloc (8));            /* -lefence */
579
580 #ifdef ENABLE_NLS
581   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
582   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
583   textdomain (GETTEXT_PACKAGE);
584 #endif
585
586   if (!g_thread_supported ())
587     g_thread_init (NULL);
588
589   gst_alloc_trace_set_flags_all (GST_ALLOC_TRACE_LIVE);
590
591   ctx = g_option_context_new ("PIPELINE-DESCRIPTION");
592   g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
593   g_option_context_add_group (ctx, gst_init_get_option_group ());
594   if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
595     if (err)
596       g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
597     else
598       g_print ("Error initializing: Unknown error!\n");
599     exit (1);
600   }
601   g_option_context_free (ctx);
602
603   gst_tools_print_version ("gst-launch");
604
605   /* FIXpopt: strip short args, too. We do it ourselves for now */
606   j = 1;
607   for (i = 1; i < argc; i++) {
608     if (*(argv[i]) == '-') {
609       if (strlen (argv[i]) == 2) {
610         gchar *c = argv[i];
611
612         c++;
613         if (*c == 'X' || *c == 'o') {
614           i++;
615         }
616       }
617     } else {
618       argv[j] = argv[i];
619       j++;
620     }
621   }
622   argc = j;
623
624 #ifndef DISABLE_FAULT_HANDLER
625   if (!no_fault)
626     fault_setup ();
627
628   sigint_setup ();
629   play_signal_setup ();
630 #endif
631
632   if (trace) {
633     if (!gst_alloc_trace_available ()) {
634       g_warning ("Trace not available (recompile with trace enabled).");
635     }
636     gst_alloc_trace_print_live ();
637   }
638
639   /* make a null-terminated version of argv */
640   argvn = g_new0 (char *, argc);
641   memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
642 #ifndef GST_DISABLE_LOADSAVE
643   if (strstr (argv[0], "gst-xmllaunch")) {
644     pipeline = xmllaunch_parse_cmdline ((const gchar **) argvn);
645   } else
646 #endif
647   {
648     pipeline =
649         (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
650   }
651   g_free (argvn);
652
653   if (!pipeline) {
654     if (error) {
655       fprintf (stderr, _("ERROR: pipeline could not be constructed: %s.\n"),
656           GST_STR_NULL (error->message));
657       g_error_free (error);
658     } else {
659       fprintf (stderr, _("ERROR: pipeline could not be constructed.\n"));
660     }
661     return 1;
662   } else if (error) {
663     fprintf (stderr, _("WARNING: erroneous pipeline: %s\n"),
664         GST_STR_NULL (error->message));
665     g_error_free (error);
666     return 1;
667   }
668
669   if (verbose) {
670     gchar **exclude_list =
671         exclude_args ? g_strsplit (exclude_args, ",", 0) : NULL;
672     g_signal_connect (pipeline, "deep_notify",
673         G_CALLBACK (gst_object_default_deep_notify), exclude_list);
674   }
675 #ifndef GST_DISABLE_LOADSAVE
676   if (savefile) {
677     gst_xml_write_file (GST_ELEMENT (pipeline), fopen (savefile, "w"));
678   }
679 #endif
680
681   if (!savefile) {
682     GstState state, pending;
683     GstStateChangeReturn ret;
684
685     /* If the top-level object is not a pipeline, place it in a pipeline. */
686     if (!GST_IS_PIPELINE (pipeline)) {
687       GstElement *real_pipeline = gst_element_factory_make ("pipeline", NULL);
688
689       if (real_pipeline == NULL) {
690         fprintf (stderr, _("ERROR: the 'pipeline' element wasn't found.\n"));
691         return 1;
692       }
693       gst_bin_add (GST_BIN (real_pipeline), pipeline);
694       pipeline = real_pipeline;
695     }
696     fprintf (stderr, _("Setting pipeline to PAUSED ...\n"));
697     ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
698
699     switch (ret) {
700       case GST_STATE_CHANGE_FAILURE:
701         fprintf (stderr, _("ERROR: Pipeline doesn't want to pause.\n"));
702         res = -1;
703         event_loop (pipeline, FALSE, GST_STATE_VOID_PENDING);
704         goto end;
705       case GST_STATE_CHANGE_NO_PREROLL:
706         fprintf (stderr, _("Pipeline is live and does not need PREROLL ...\n"));
707         break;
708       case GST_STATE_CHANGE_ASYNC:
709         fprintf (stderr, _("Pipeline is PREROLLING ...\n"));
710         caught_error = event_loop (pipeline, TRUE, GST_STATE_PAUSED);
711         if (caught_error) {
712           fprintf (stderr, _("ERROR: pipeline doesn't want to preroll.\n"));
713           goto end;
714         }
715         state = GST_STATE_PAUSED;
716         /* fallthrough */
717       case GST_STATE_CHANGE_SUCCESS:
718         fprintf (stderr, _("Pipeline is PREROLLED ...\n"));
719         break;
720     }
721
722     caught_error = event_loop (pipeline, FALSE, GST_STATE_PLAYING);
723
724     if (caught_error) {
725       fprintf (stderr, _("ERROR: pipeline doesn't want to preroll.\n"));
726     } else {
727       GTimeVal tfthen, tfnow;
728       GstClockTimeDiff diff;
729
730       fprintf (stderr, _("Setting pipeline to PLAYING ...\n"));
731       if (gst_element_set_state (pipeline,
732               GST_STATE_PLAYING) == GST_STATE_CHANGE_FAILURE) {
733         fprintf (stderr, _("ERROR: pipeline doesn't want to play.\n"));
734         res = -1;
735         goto end;
736       }
737
738       g_get_current_time (&tfthen);
739       caught_error = event_loop (pipeline, TRUE, GST_STATE_PLAYING);
740       g_get_current_time (&tfnow);
741
742       diff = GST_TIMEVAL_TO_TIME (tfnow) - GST_TIMEVAL_TO_TIME (tfthen);
743
744       g_print (_("Execution ended after %" G_GUINT64_FORMAT " ns.\n"), diff);
745     }
746
747     /* iterate mainloop to process pending stuff */
748     while (g_main_context_iteration (NULL, FALSE));
749
750     fprintf (stderr, _("Setting pipeline to PAUSED ...\n"));
751     gst_element_set_state (pipeline, GST_STATE_PAUSED);
752     gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
753     fprintf (stderr, _("Setting pipeline to READY ...\n"));
754     gst_element_set_state (pipeline, GST_STATE_READY);
755     gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
756
757   end:
758     fprintf (stderr, _("Setting pipeline to NULL ...\n"));
759     gst_element_set_state (pipeline, GST_STATE_NULL);
760     gst_element_get_state (pipeline, &state, &pending, GST_CLOCK_TIME_NONE);
761   }
762
763   fprintf (stderr, _("FREEING pipeline ...\n"));
764   gst_object_unref (pipeline);
765
766   gst_deinit ();
767   if (trace)
768     gst_alloc_trace_print_live ();
769
770   return res;
771 }