2 /*** block from ../../../docs/manual/basics-helloworld.xml ***/
12 GMainLoop *loop = (GMainLoop *) data;
14 switch (GST_MESSAGE_TYPE (msg)) {
17 g_print ("End of stream\n");
18 g_main_loop_quit (loop);
21 case GST_MESSAGE_ERROR: {
25 gst_message_parse_error (msg, &error, &debug);
28 g_printerr ("Error: %s\n", error->message);
31 g_main_loop_quit (loop);
43 on_pad_added (GstElement *element,
48 GstElement *decoder = (GstElement *) data;
50 /* We can now link this pad with the vorbis-decoder sink pad */
51 g_print ("Dynamic pad created, linking demuxer/decoder\n");
53 sinkpad = gst_element_get_static_pad (decoder, "sink");
55 gst_pad_link (pad, sinkpad);
57 gst_object_unref (sinkpad);
68 GstElement *pipeline, *source, *demuxer, *decoder, *conv, *sink;
72 gst_init (&argc, &argv);
74 loop = g_main_loop_new (NULL, FALSE);
77 /* Check input arguments */
79 g_printerr ("Usage: %s <Ogg/Vorbis filename>\n", argv[0]);
84 /* Create gstreamer elements */
85 pipeline = gst_pipeline_new ("audio-player");
86 source = gst_element_factory_make ("filesrc", "file-source");
87 demuxer = gst_element_factory_make ("oggdemux", "ogg-demuxer");
88 decoder = gst_element_factory_make ("vorbisdec", "vorbis-decoder");
89 conv = gst_element_factory_make ("audioconvert", "converter");
90 sink = gst_element_factory_make ("autoaudiosink", "audio-output");
92 if (!pipeline || !source || !demuxer || !decoder || !conv || !sink) {
93 g_printerr ("One element could not be created. Exiting.\n");
97 /* Set up the pipeline */
99 /* we set the input filename to the source element */
100 g_object_set (G_OBJECT (source), "location", argv[1], NULL);
102 /* we add a message handler */
103 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
104 gst_bus_add_watch (bus, bus_call, loop);
105 gst_object_unref (bus);
107 /* we add all elements into the pipeline */
108 /* file-source | ogg-demuxer | vorbis-decoder | converter | alsa-output */
109 gst_bin_add_many (GST_BIN (pipeline),
110 source, demuxer, decoder, conv, sink, NULL);
112 /* we link the elements together */
113 /* file-source -> ogg-demuxer ~> vorbis-decoder -> converter -> alsa-output */
114 gst_element_link (source, demuxer);
115 gst_element_link_many (decoder, conv, sink, NULL);
116 g_signal_connect (demuxer, "pad-added", G_CALLBACK (on_pad_added), decoder);
118 /* note that the demuxer will be linked to the decoder dynamically.
119 The reason is that Ogg may contain various streams (for example
120 audio and video). The source pad(s) will be created at run time,
121 by the demuxer when it detects the amount and nature of streams.
122 Therefore we connect a callback function which will be executed
123 when the "pad-added" is emitted.*/
126 /* Set the pipeline to "playing" state*/
127 g_print ("Now playing: %s\n", argv[1]);
128 gst_element_set_state (pipeline, GST_STATE_PLAYING);
132 g_print ("Running...\n");
133 g_main_loop_run (loop);
136 /* Out of the main loop, clean up nicely */
137 g_print ("Returned, stopping playback\n");
138 gst_element_set_state (pipeline, GST_STATE_NULL);
140 g_print ("Deleting pipeline\n");
141 gst_object_unref (GST_OBJECT (pipeline));