Imported Upstream version 0.10.36
[profile/ivi/gstreamer.git] / tests / examples / manual / decodebin.c
1
2 /*** block a  from ../../../docs/manual/highlevel-components.xml ***/
3 #include <gst/gst.h>
4
5 /*** block b  from ../../../docs/manual/highlevel-components.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       /* unhandled message */
32       break;
33   }
34
35   /* remove message from the queue */
36   return TRUE;
37 }
38
39 /*** block c  from ../../../docs/manual/highlevel-components.xml ***/
40 GstElement *pipeline, *audio;
41
42 static void
43 cb_newpad (GstElement *decodebin,
44            GstPad     *pad,
45            gboolean    last,
46            gpointer    data)
47 {
48   GstCaps *caps;
49   GstStructure *str;
50   GstPad *audiopad;
51
52   /* only link once */
53   audiopad = gst_element_get_static_pad (audio, "sink");
54   if (GST_PAD_IS_LINKED (audiopad)) {
55     g_object_unref (audiopad);
56     return;
57   }
58
59   /* check media type */
60   caps = gst_pad_get_caps (pad);
61   str = gst_caps_get_structure (caps, 0);
62   if (!g_strrstr (gst_structure_get_name (str), "audio")) {
63     gst_caps_unref (caps);
64     gst_object_unref (audiopad);
65     return;
66   }
67   gst_caps_unref (caps);
68
69   /* link'n'play */
70   gst_pad_link (pad, audiopad);
71
72   g_object_unref (audiopad);
73 }
74
75 gint
76 main (gint   argc,
77       gchar *argv[])
78 {
79   GMainLoop *loop;
80   GstElement *src, *dec, *conv, *sink;
81   GstPad *audiopad;
82   GstBus *bus;
83
84   /* init GStreamer */
85   gst_init (&argc, &argv);
86   loop = g_main_loop_new (NULL, FALSE);
87
88   /* make sure we have input */
89   if (argc != 2) {
90     g_print ("Usage: %s <filename>\n", argv[0]);
91     return -1;
92   }
93
94   /* setup */
95   pipeline = gst_pipeline_new ("pipeline");
96
97   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
98   gst_bus_add_watch (bus, my_bus_callback, loop);
99   gst_object_unref (bus);
100
101   src = gst_element_factory_make ("filesrc", "source");
102   g_object_set (G_OBJECT (src), "location", argv[1], NULL);
103   dec = gst_element_factory_make ("decodebin", "decoder");
104   g_signal_connect (dec, "new-decoded-pad", G_CALLBACK (cb_newpad), NULL);
105   gst_bin_add_many (GST_BIN (pipeline), src, dec, NULL);
106   gst_element_link (src, dec);
107
108   /* create audio output */
109   audio = gst_bin_new ("audiobin");
110   conv = gst_element_factory_make ("audioconvert", "aconv");
111   audiopad = gst_element_get_static_pad (conv, "sink");
112   sink = gst_element_factory_make ("alsasink", "sink");
113   gst_bin_add_many (GST_BIN (audio), conv, sink, NULL);
114   gst_element_link (conv, sink);
115   gst_element_add_pad (audio,
116       gst_ghost_pad_new ("sink", audiopad));
117   gst_object_unref (audiopad);
118   gst_bin_add (GST_BIN (pipeline), audio);
119
120   /* run */
121   gst_element_set_state (pipeline, GST_STATE_PLAYING);
122   g_main_loop_run (loop);
123
124   /* cleanup */
125   gst_element_set_state (pipeline, GST_STATE_NULL);
126   gst_object_unref (GST_OBJECT (pipeline));
127
128   return 0;
129 }