Initialize Tizen 2.3
[framework/multimedia/gstreamer0.10.git] / wearable / tests / examples / manual / typefind.c
1
2 /*** block a  from ../../../docs/manual/advanced-autoplugging.xml ***/
3 #include <gst/gst.h>
4
5 /*** block b  from ../../../docs/manual/advanced-autoplugging.xml ***/
6 static gboolean
7 my_bus_callback (GstBus     *bus,
8                  GstMessage *message,
9                  gpointer    data)
10 {
11   GMainLoop *loop = data;
12
13   switch (GST_MESSAGE_TYPE (message)) {
14     case GST_MESSAGE_ERROR: {
15       GError *err;
16       gchar *debug;
17
18       gst_message_parse_error (message, &err, &debug);
19       g_print ("Error: %s\n", err->message);
20       g_error_free (err);
21       g_free (debug);
22
23       g_main_loop_quit (loop);
24       break;
25     }
26     case GST_MESSAGE_EOS:
27       /* end-of-stream */
28       g_main_loop_quit (loop);
29       break;
30     default:
31       break;
32   }
33
34   /* remove from queue */
35   return TRUE;
36 }
37
38 /*** block c  from ../../../docs/manual/advanced-autoplugging.xml ***/
39 static gboolean
40 idle_exit_loop (gpointer data)
41 {
42   g_main_loop_quit ((GMainLoop *) data);
43
44   /* once */
45   return FALSE;
46 }
47
48 static void
49 cb_typefound (GstElement *typefind,
50               guint       probability,
51               GstCaps    *caps,
52               gpointer    data)
53 {
54   GMainLoop *loop = data;
55   gchar *type;
56
57   type = gst_caps_to_string (caps);
58   g_print ("Media type %s found, probability %d%%\n", type, probability);
59   g_free (type);
60
61   /* since we connect to a signal in the pipeline thread context, we need
62    * to set an idle handler to exit the main loop in the mainloop context.
63    * Normally, your app should not need to worry about such things. */
64   g_idle_add (idle_exit_loop, loop);
65 }
66
67 gint 
68 main (gint   argc,
69       gchar *argv[])
70 {
71   GMainLoop *loop;
72   GstElement *pipeline, *filesrc, *typefind, *fakesink;
73   GstBus *bus;
74
75   /* init GStreamer */
76   gst_init (&argc, &argv);
77   loop = g_main_loop_new (NULL, FALSE);
78
79   /* check args */
80   if (argc != 2) {
81     g_print ("Usage: %s <filename>\n", argv[0]);
82     return -1;
83   }
84
85   /* create a new pipeline to hold the elements */
86   pipeline = gst_pipeline_new ("pipe");
87
88   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
89   gst_bus_add_watch (bus, my_bus_callback, NULL);
90   gst_object_unref (bus);
91
92   /* create file source and typefind element */
93   filesrc = gst_element_factory_make ("filesrc", "source");
94   g_object_set (G_OBJECT (filesrc), "location", argv[1], NULL);
95   typefind = gst_element_factory_make ("typefind", "typefinder");
96   g_signal_connect (typefind, "have-type", G_CALLBACK (cb_typefound), loop);
97   fakesink = gst_element_factory_make ("fakesink", "sink");
98
99   /* setup */
100   gst_bin_add_many (GST_BIN (pipeline), filesrc, typefind, fakesink, NULL);
101   gst_element_link_many (filesrc, typefind, fakesink, NULL);
102   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
103   g_main_loop_run (loop);
104
105   /* unset */
106   gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_NULL);
107   gst_object_unref (GST_OBJECT (pipeline));
108
109   return 0;
110 }