5 #include <gst/app/gstappsrc.h>
6 #include <gst/app/gstappsink.h>
8 /* these are the caps we are going to pass through the appsink and appsrc */
9 const gchar *audio_caps =
10 "audio/x-raw-int,channels=1,rate=8000,signed=(boolean)true,width=16,depth=16,endianness=1234";
19 /* called when the appsink notifies us that there is a new buffer ready for
22 on_new_buffer_from_source (GstElement * elt, ProgramData * data)
25 GstBuffer *app_buffer, *buffer;
28 /* get the buffer from appsink */
29 buffer = gst_app_sink_pull_buffer (GST_APP_SINK (elt));
31 /* turn it into an app buffer, it's not really needed, we could simply push
32 * the retrieved buffer from appsink into appsrc just fine. */
33 size = gst_buffer_get_size (buffer);
34 g_print ("Pushing a buffer of size %d\n", size);
35 app_buffer = gst_buffer_new_and_alloc (size);
37 gst_buffer_copy_into (app_buffer, buffer, GST_BUFFER_COPY_MEMORY, 0, size);
39 /* we don't need the appsink buffer anymore */
40 gst_buffer_unref (buffer);
42 /* get source an push new buffer */
43 source = gst_bin_get_by_name (GST_BIN (data->sink), "testsource");
44 gst_app_src_push_buffer (GST_APP_SRC (source), app_buffer);
47 /* called when we get a GstMessage from the source pipeline when we get EOS, we
48 * notify the appsrc of it. */
50 on_source_message (GstBus * bus, GstMessage * message, ProgramData * data)
54 switch (GST_MESSAGE_TYPE (message)) {
56 g_print ("The source got dry\n");
57 source = gst_bin_get_by_name (GST_BIN (data->sink), "testsource");
58 gst_app_src_end_of_stream (GST_APP_SRC (source));
60 case GST_MESSAGE_ERROR:
61 g_print ("Received error\n");
62 g_main_loop_quit (data->loop);
70 /* called when we get a GstMessage from the sink pipeline when we get EOS, we
71 * exit the mainloop and this testapp. */
73 on_sink_message (GstBus * bus, GstMessage * message, ProgramData * data)
76 switch (GST_MESSAGE_TYPE (message)) {
78 g_print ("Finished playback\n");
79 g_main_loop_quit (data->loop);
81 case GST_MESSAGE_ERROR:
82 g_print ("Received error\n");
83 g_main_loop_quit (data->loop);
92 main (int argc, char *argv[])
94 gchar *filename = NULL;
95 ProgramData *data = NULL;
98 GstElement *testsink = NULL;
99 GstElement *testsource = NULL;
101 gst_init (&argc, &argv);
104 filename = g_strdup (argv[1]);
106 filename = g_strdup ("/usr/share/sounds/ekiga/ring.wav");
108 data = g_new0 (ProgramData, 1);
110 data->loop = g_main_loop_new (NULL, FALSE);
112 /* setting up source pipeline, we read from a file and convert to our desired
116 ("filesrc location=\"%s\" ! wavparse ! audioconvert ! audioresample ! appsink caps=\"%s\" name=testsink",
117 filename, audio_caps);
119 data->source = gst_parse_launch (string, NULL);
122 if (data->source == NULL) {
123 g_print ("Bad source\n");
127 /* to be notified of messages from this pipeline, mostly EOS */
128 bus = gst_element_get_bus (data->source);
129 gst_bus_add_watch (bus, (GstBusFunc) on_source_message, data);
130 gst_object_unref (bus);
132 /* we use appsink in push mode, it sends us a signal when data is available
133 * and we pull out the data in the signal callback. We want the appsink to
134 * push as fast as it can, hence the sync=false */
135 testsink = gst_bin_get_by_name (GST_BIN (data->source), "testsink");
136 g_object_set (G_OBJECT (testsink), "emit-signals", TRUE, "sync", FALSE, NULL);
137 g_signal_connect (testsink, "new-buffer",
138 G_CALLBACK (on_new_buffer_from_source), data);
139 gst_object_unref (testsink);
141 /* setting up sink pipeline, we push audio data into this pipeline that will
142 * then play it back using the default audio sink. We have no blocking
143 * behaviour on the src which means that we will push the entire file into
146 g_strdup_printf ("appsrc name=testsource caps=\"%s\" ! autoaudiosink",
148 data->sink = gst_parse_launch (string, NULL);
151 if (data->sink == NULL) {
152 g_print ("Bad sink\n");
156 testsource = gst_bin_get_by_name (GST_BIN (data->sink), "testsource");
157 /* configure for time-based format */
158 g_object_set (testsource, "format", GST_FORMAT_TIME, NULL);
159 /* uncomment the next line to block when appsrc has buffered enough */
160 /* g_object_set (testsource, "block", TRUE, NULL); */
161 gst_object_unref (testsource);
163 bus = gst_element_get_bus (data->sink);
164 gst_bus_add_watch (bus, (GstBusFunc) on_sink_message, data);
165 gst_object_unref (bus);
167 /* launching things */
168 gst_element_set_state (data->sink, GST_STATE_PLAYING);
169 gst_element_set_state (data->source, GST_STATE_PLAYING);
171 /* let's run !, this loop will quit when the sink pipeline goes EOS or when an
172 * error occurs in the source or sink pipelines. */
173 g_print ("Let's run!\n");
174 g_main_loop_run (data->loop);
175 g_print ("Going out\n");
177 gst_element_set_state (data->source, GST_STATE_NULL);
178 gst_element_set_state (data->sink, GST_STATE_NULL);
180 gst_object_unref (data->source);
181 gst_object_unref (data->sink);
182 g_main_loop_unref (data->loop);