4 #define CHUNK_SIZE 1024 /* Amount of bytes we are sending in each buffer */
5 #define SAMPLE_RATE 44100 /* Samples per second we are sending */
6 #define AUDIO_CAPS "audio/x-raw-int,channels=1,rate=%d,signed=(boolean)true,width=16,depth=16,endianness=BYTE_ORDER"
8 /* Structure to contain all our information, so we can pass it to callbacks */
9 typedef struct _CustomData {
10 GstElement *pipeline, *app_source, *tee, *audio_queue, *audio_convert1, *audio_resample, *audio_sink;
11 GstElement *video_queue, *audio_convert2, *visual, *video_convert, *video_sink;
12 GstElement *app_queue, *app_sink;
14 guint64 num_samples; /* Number of samples generated so far (for timestamp generation) */
15 gfloat a, b, c, d; /* For waveform generation */
17 guint sourceid; /* To control the GSource */
19 GMainLoop *main_loop; /* GLib's Main Loop */
22 /* This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
23 * The idle handler is added to the mainloop when appsrc requests us to start sending data (need-data signal)
24 * and is removed when appsrc has enough data (enough-data signal).
26 static gboolean push_data (CustomData *data) {
31 gint num_samples = CHUNK_SIZE / 2; /* Because each sample is 16 bits */
34 /* Create a new empty buffer */
35 buffer = gst_buffer_new_and_alloc (CHUNK_SIZE);
37 /* Set its timestamp and duration */
38 GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (data->num_samples, GST_SECOND, SAMPLE_RATE);
39 GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (CHUNK_SIZE, GST_SECOND, SAMPLE_RATE);
41 /* Generate some psychodelic waveforms */
42 raw = (gint16 *)GST_BUFFER_DATA (buffer);
44 data->d -= data->c / 1000;
45 freq = 1100 + 1000 * data->d;
46 for (i = 0; i < num_samples; i++) {
48 data->b -= data->a / freq;
49 raw[i] = (gint16)(500 * data->a);
51 data->num_samples += num_samples;
53 /* Push the buffer into the appsrc */
54 g_signal_emit_by_name (data->app_source, "push-buffer", buffer, &ret);
56 /* Free the buffer now that we are done with it */
57 gst_buffer_unref (buffer);
59 if (ret != GST_FLOW_OK) {
60 /* We got some error, stop sending data */
67 /* This signal callback triggers when appsrc needs data. Here, we add an idle handler
68 * to the mainloop to start pushing data into the appsrc */
69 static void start_feed (GstElement *source, guint size, CustomData *data) {
70 if (data->sourceid == 0) {
71 g_print ("Start feeding\n");
72 data->sourceid = g_idle_add ((GSourceFunc) push_data, data);
76 /* This callback triggers when appsrc has enough data and we can stop sending.
77 * We remove the idle handler from the mainloop */
78 static void stop_feed (GstElement *source, CustomData *data) {
79 if (data->sourceid != 0) {
80 g_print ("Stop feeding\n");
81 g_source_remove (data->sourceid);
86 /* The appsink has received a buffer */
87 static void new_buffer (GstElement *sink, CustomData *data) {
90 /* Retrieve the buffer */
91 g_signal_emit_by_name (sink, "pull-buffer", &buffer);
93 /* The only thing we do in this example is print a * to indicate a received buffer */
95 gst_buffer_unref (buffer);
99 /* This function is called when an error message is posted on the bus */
100 static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
104 /* Print error details on the screen */
105 gst_message_parse_error (msg, &err, &debug_info);
106 g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
107 g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
108 g_clear_error (&err);
111 g_main_loop_quit (data->main_loop);
114 int main(int argc, char *argv[]) {
116 GstPadTemplate *tee_src_pad_template;
117 GstPad *tee_audio_pad, *tee_video_pad, *tee_app_pad;
118 GstPad *queue_audio_pad, *queue_video_pad, *queue_app_pad;
119 gchar *audio_caps_text;
123 /* Initialize cumstom data structure */
124 memset (&data, 0, sizeof (data));
125 data.b = 1; /* For waveform generation */
128 /* Initialize GStreamer */
129 gst_init (&argc, &argv);
131 /* Create the elements */
132 data.app_source = gst_element_factory_make ("appsrc", "audio_source");
133 data.tee = gst_element_factory_make ("tee", "tee");
134 data.audio_queue = gst_element_factory_make ("queue", "audio_queue");
135 data.audio_convert1 = gst_element_factory_make ("audioconvert", "audio_convert1");
136 data.audio_resample = gst_element_factory_make ("audioresample", "audio_resample");
137 data.audio_sink = gst_element_factory_make ("autoaudiosink", "audio_sink");
138 data.video_queue = gst_element_factory_make ("queue", "video_queue");
139 data.audio_convert2 = gst_element_factory_make ("audioconvert", "audio_convert2");
140 data.visual = gst_element_factory_make ("wavescope", "visual");
141 data.video_convert = gst_element_factory_make ("ffmpegcolorspace", "csp");
142 data.video_sink = gst_element_factory_make ("autovideosink", "video_sink");
143 data.app_queue = gst_element_factory_make ("queue", "app_queue");
144 data.app_sink = gst_element_factory_make ("appsink", "app_sink");
146 /* Create the empty pipeline */
147 data.pipeline = gst_pipeline_new ("test-pipeline");
149 if (!data.pipeline || !data.app_source || !data.tee || !data.audio_queue || !data.audio_convert1 ||
150 !data.audio_resample || !data.audio_sink || !data.video_queue || !data.audio_convert2 || !data.visual ||
151 !data.video_convert || !data.video_sink || !data.app_queue || !data.app_sink) {
152 g_printerr ("Not all elements could be created.\n");
156 /* Configure wavescope */
157 g_object_set (data.visual, "shader", 0, "style", 0, NULL);
159 /* Configure appsrc */
160 audio_caps_text = g_strdup_printf (AUDIO_CAPS, SAMPLE_RATE);
161 audio_caps = gst_caps_from_string (audio_caps_text);
162 g_object_set (data.app_source, "caps", audio_caps, NULL);
163 g_signal_connect (data.app_source, "need-data", G_CALLBACK (start_feed), &data);
164 g_signal_connect (data.app_source, "enough-data", G_CALLBACK (stop_feed), &data);
166 /* Configure appsink */
167 g_object_set (data.app_sink, "emit-signals", TRUE, "caps", audio_caps, NULL);
168 g_signal_connect (data.app_sink, "new-buffer", G_CALLBACK (new_buffer), &data);
169 gst_caps_unref (audio_caps);
170 g_free (audio_caps_text);
172 /* Link all elements that can be automatically linked because they have "Always" pads */
173 gst_bin_add_many (GST_BIN (data.pipeline), data.app_source, data.tee, data.audio_queue, data.audio_convert1, data.audio_resample,
174 data.audio_sink, data.video_queue, data.audio_convert2, data.visual, data.video_convert, data.video_sink, data.app_queue,
175 data.app_sink, NULL);
176 if (gst_element_link_many (data.app_source, data.tee, NULL) != TRUE ||
177 gst_element_link_many (data.audio_queue, data.audio_convert1, data.audio_resample, data.audio_sink, NULL) != TRUE ||
178 gst_element_link_many (data.video_queue, data.audio_convert2, data.visual, data.video_convert, data.video_sink, NULL) != TRUE ||
179 gst_element_link_many (data.app_queue, data.app_sink, NULL) != TRUE) {
180 g_printerr ("Elements could not be linked.\n");
181 gst_object_unref (data.pipeline);
185 /* Manually link the Tee, which has "Request" pads */
186 tee_src_pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (data.tee), "src%d");
187 tee_audio_pad = gst_element_request_pad (data.tee, tee_src_pad_template, NULL, NULL);
188 g_print ("Obtained request pad %s for audio branch.\n", gst_pad_get_name (tee_audio_pad));
189 queue_audio_pad = gst_element_get_static_pad (data.audio_queue, "sink");
190 tee_video_pad = gst_element_request_pad (data.tee, tee_src_pad_template, NULL, NULL);
191 g_print ("Obtained request pad %s for video branch.\n", gst_pad_get_name (tee_video_pad));
192 queue_video_pad = gst_element_get_static_pad (data.video_queue, "sink");
193 tee_app_pad = gst_element_request_pad (data.tee, tee_src_pad_template, NULL, NULL);
194 g_print ("Obtained request pad %s for app branch.\n", gst_pad_get_name (tee_app_pad));
195 queue_app_pad = gst_element_get_static_pad (data.app_queue, "sink");
196 if (gst_pad_link (tee_audio_pad, queue_audio_pad) != GST_PAD_LINK_OK ||
197 gst_pad_link (tee_video_pad, queue_video_pad) != GST_PAD_LINK_OK ||
198 gst_pad_link (tee_app_pad, queue_app_pad) != GST_PAD_LINK_OK) {
199 g_printerr ("Tee could not be linked\n");
200 gst_object_unref (data.pipeline);
203 gst_object_unref (queue_audio_pad);
204 gst_object_unref (queue_video_pad);
205 gst_object_unref (queue_app_pad);
207 /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
208 bus = gst_element_get_bus (data.pipeline);
209 gst_bus_add_signal_watch (bus);
210 g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, &data);
211 gst_object_unref (bus);
213 /* Start playing the pipeline */
214 gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
216 /* Create a GLib Main Loop and set it to run */
217 data.main_loop = g_main_loop_new (NULL, FALSE);
218 g_main_loop_run (data.main_loop);
220 /* Release the request pads from the Tee, and unref them */
221 gst_element_release_request_pad (data.tee, tee_audio_pad);
222 gst_element_release_request_pad (data.tee, tee_video_pad);
223 gst_element_release_request_pad (data.tee, tee_app_pad);
224 gst_object_unref (tee_audio_pad);
225 gst_object_unref (tee_video_pad);
226 gst_object_unref (tee_app_pad);
229 gst_element_set_state (data.pipeline, GST_STATE_NULL);
230 gst_object_unref (data.pipeline);