1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19 #include <gst/gst.h>
20 #include <string.h>
21
22 static void
warning_cb(GstBus * bus,GstMessage * msg,gpointer foo)23 warning_cb (GstBus * bus, GstMessage * msg, gpointer foo)
24 {
25 GError *err = NULL;
26 gchar *dbg = NULL;
27
28 gst_message_parse_warning (msg, &err, &dbg);
29
30 g_printerr ("WARNING: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
31
32 g_error_free (err);
33 g_free (dbg);
34 }
35
36 static void
error_cb(GstBus * bus,GstMessage * msg,GMainLoop * main_loop)37 error_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
38 {
39 GError *err = NULL;
40 gchar *dbg = NULL;
41
42 gst_message_parse_error (msg, &err, &dbg);
43
44 g_printerr ("ERROR: %s (%s)\n", err->message, (dbg) ? dbg : "no details");
45
46 g_main_loop_quit (main_loop);
47
48 g_error_free (err);
49 g_free (dbg);
50 }
51
52 static void
eos_cb(GstBus * bus,GstMessage * msg,GMainLoop * main_loop)53 eos_cb (GstBus * bus, GstMessage * msg, GMainLoop * main_loop)
54 {
55 g_print ("EOS\n");
56 g_main_loop_quit (main_loop);
57 }
58
59 static void
state_cb(GstBus * bus,GstMessage * msg,GstElement * pipeline)60 state_cb (GstBus * bus, GstMessage * msg, GstElement * pipeline)
61 {
62 if (msg->src == GST_OBJECT (pipeline)) {
63 GstState old_state, new_state, pending_state;
64
65 gst_message_parse_state_changed (msg, &old_state, &new_state,
66 &pending_state);
67 if (new_state == GST_STATE_PLAYING) {
68 g_print ("Decoding ...\n");
69 }
70 }
71 }
72
73 static void
pad_added_cb(GstElement * decodebin,GstPad * pad,GstElement * pipeline)74 pad_added_cb (GstElement * decodebin, GstPad * pad, GstElement * pipeline)
75 {
76 GstPadLinkReturn ret;
77 GstElement *fakesink;
78 GstPad *fakesink_pad;
79
80 fakesink = gst_element_factory_make ("fakesink", NULL);
81 fakesink_pad = gst_element_get_static_pad (fakesink, "sink");
82
83 gst_bin_add (GST_BIN (pipeline), fakesink);
84
85 /* this doesn't really seem right, but it makes things work for me */
86 gst_element_set_state (fakesink, GST_STATE_PLAYING);
87
88 ret = gst_pad_link (pad, fakesink_pad);
89 if (!GST_PAD_LINK_SUCCESSFUL (ret)) {
90 g_printerr ("Failed to link %s:%s to %s:%s (ret = %d)\n",
91 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (fakesink_pad), ret);
92 } else {
93 g_printerr ("Linked %s:%s to %s:%s\n", GST_DEBUG_PAD_NAME (pad),
94 GST_DEBUG_PAD_NAME (fakesink_pad));
95 }
96
97 gst_object_unref (fakesink_pad);
98 }
99
100 gint
main(gint argc,gchar * argv[])101 main (gint argc, gchar * argv[])
102 {
103 GstElement *decoder;
104 GstElement *source;
105 GstElement *pipeline;
106 GstStateChangeReturn res;
107 GMainLoop *loop;
108 GstBus *bus;
109
110 gst_init (&argc, &argv);
111
112 if (argc != 2) {
113 g_printerr ("Decode file from start to end.\n");
114 g_printerr ("Usage: %s URI\n\n", argv[0]);
115 return 1;
116 }
117
118 loop = g_main_loop_new (NULL, TRUE);
119
120 pipeline = gst_pipeline_new ("pipeline");
121
122 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
123 gst_bus_add_signal_watch (bus);
124
125 g_signal_connect (bus, "message::eos", G_CALLBACK (eos_cb), loop);
126 g_signal_connect (bus, "message::error", G_CALLBACK (error_cb), loop);
127 g_signal_connect (bus, "message::warning", G_CALLBACK (warning_cb), NULL);
128 g_signal_connect (bus, "message::state-changed", G_CALLBACK (state_cb),
129 pipeline);
130
131 source = gst_element_factory_make ("giosrc", "source");
132 g_assert (source);
133
134 if (argv[1] && strstr (argv[1], "://") != NULL) {
135 g_object_set (G_OBJECT (source), "location", argv[1], NULL);
136 } else if (argv[1]) {
137 gchar *uri = g_strdup_printf ("file://%s", argv[1]);
138
139 g_object_set (G_OBJECT (source), "location", uri, NULL);
140 g_free (uri);
141 }
142
143 decoder = gst_element_factory_make ("decodebin", "decoder");
144 g_assert (decoder);
145
146 gst_bin_add (GST_BIN (pipeline), source);
147 gst_bin_add (GST_BIN (pipeline), decoder);
148
149 gst_element_link_pads (source, "src", decoder, "sink");
150
151 g_signal_connect (decoder, "pad-added", G_CALLBACK (pad_added_cb), pipeline);
152
153 res = gst_element_set_state (pipeline, GST_STATE_PLAYING);
154 if (res == GST_STATE_CHANGE_FAILURE) {
155 g_print ("could not play\n");
156 return -1;
157 }
158
159 g_main_loop_run (loop);
160
161 /* tidy up */
162 gst_element_set_state (pipeline, GST_STATE_NULL);
163 gst_object_unref (pipeline);
164 gst_object_unref (bus);
165
166 return 0;
167 }
168