33f7d8eb6f77bd45f5addc7f105ef65fb81651cc
[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 /* blocking */
11 static gboolean
12 event_loop (GstElement * pipeline)
13 {
14   GstBus *bus;
15   GstMessageType revent;
16   GstMessage *message = NULL;
17
18   bus = gst_element_get_bus (GST_ELEMENT (pipeline));
19
20   while (TRUE) {
21     revent = gst_bus_poll (bus, GST_MESSAGE_ANY, -1);
22
23     message = gst_bus_pop (bus);
24     g_return_val_if_fail (message != NULL, TRUE);
25
26     switch (revent) {
27       case GST_MESSAGE_EOS:
28         gst_message_unref (message);
29         return FALSE;
30       case GST_MESSAGE_WARNING:
31       case GST_MESSAGE_ERROR:{
32         GError *gerror;
33         gchar *debug;
34
35         gst_message_parse_error (message, &gerror, &debug);
36         gst_message_unref (message);
37         gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
38         g_error_free (gerror);
39         g_free (debug);
40         return TRUE;
41       }
42       default:
43         gst_message_unref (message);
44         break;
45     }
46   }
47
48   g_assert_not_reached ();
49   return TRUE;
50 }
51
52 int
53 main (int argc, char *argv[])
54 {
55   GstElement *pipeline = NULL;
56   GError *error = NULL;
57   GstElement *md5sink;
58   gchar **argvn;
59   gchar *md5string = g_malloc0 (33);
60
61   free (malloc (8));            /* -lefence */
62
63   setlocale (LC_ALL, "");
64
65   gst_init (&argc, &argv);
66
67   argvn = g_new0 (char *, argc);
68   memcpy (argvn, argv + 1, sizeof (char *) * (argc - 1));
69   pipeline = (GstElement *) gst_parse_launchv ((const gchar **) argvn, &error);
70   if (!pipeline) {
71     if (error) {
72       g_warning ("pipeline could not be constructed: %s\n", error->message);
73       g_error_free (error);
74     } else
75       g_warning ("pipeline could not be constructed\n");
76     return 1;
77   }
78
79   md5sink = gst_bin_get_by_name (GST_BIN (pipeline), "md5sink0");
80   if (md5sink == NULL) {
81     g_print ("ERROR: pipeline has no element named md5sink0.\n");
82     g_print ("Did you forget to put an md5sink in the pipeline?\n");
83     return 1;
84   }
85
86   if (!pipeline) {
87     if (error) {
88       g_warning ("pipeline could not be constructed: %s\n", error->message);
89       g_error_free (error);
90     } else
91       g_warning ("pipeline could not be constructed\n");
92     return 1;
93   }
94
95   if (gst_element_set_state (pipeline, GST_STATE_PLAYING) != GST_STATE_SUCCESS) {
96     g_warning ("pipeline doesn't want to play\n");
97     return 1;
98   }
99
100   event_loop (pipeline);
101
102   gst_element_set_state (pipeline, GST_STATE_NULL);
103
104   /* print out md5sink here */
105   md5sink = gst_bin_get_by_name (GST_BIN (pipeline), "md5sink0");
106   g_assert (md5sink);
107   g_object_get (G_OBJECT (md5sink), "md5", &md5string, NULL);
108   printf ("%s\n", md5string);
109
110   gst_object_unref (GST_OBJECT (pipeline));
111
112   return 0;
113 }