2 #include <gst/audio/audio.h>
5 #define CHUNK_SIZE 1024 /* Amount of bytes we are sending in each buffer */
6 #define SAMPLE_RATE 44100 /* Samples per second we are sending */
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) {
32 gint num_samples = CHUNK_SIZE / 2; /* Because each sample is 16 bits */
35 /* Create a new empty buffer */
36 buffer = gst_buffer_new_and_alloc (CHUNK_SIZE);
38 /* Set its timestamp and duration */
39 GST_BUFFER_TIMESTAMP (buffer) = gst_util_uint64_scale (data->num_samples, GST_SECOND, SAMPLE_RATE);
40 GST_BUFFER_DURATION (buffer) = gst_util_uint64_scale (CHUNK_SIZE, GST_SECOND, SAMPLE_RATE);
42 /* Generate some psychodelic waveforms */
43 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
44 raw = (gint16 *)map.data;
46 data->d -= data->c / 1000;
47 freq = 1100 + 1000 * data->d;
48 for (i = 0; i < num_samples; i++) {
50 data->b -= data->a / freq;
51 raw[i] = (gint16)(500 * data->a);
53 gst_buffer_unmap (buffer, &map);
54 data->num_samples += num_samples;
56 /* Push the buffer into the appsrc */
57 g_signal_emit_by_name (data->app_source, "push-buffer", buffer, &ret);
59 /* Free the buffer now that we are done with it */
60 gst_buffer_unref (buffer);
62 if (ret != GST_FLOW_OK) {
63 /* We got some error, stop sending data */
70 /* This signal callback triggers when appsrc needs data. Here, we add an idle handler
71 * to the mainloop to start pushing data into the appsrc */
72 static void start_feed (GstElement *source, guint size, CustomData *data) {
73 if (data->sourceid == 0) {
74 g_print ("Start feeding\n");
75 data->sourceid = g_idle_add ((GSourceFunc) push_data, data);
79 /* This callback triggers when appsrc has enough data and we can stop sending.
80 * We remove the idle handler from the mainloop */
81 static void stop_feed (GstElement *source, CustomData *data) {
82 if (data->sourceid != 0) {
83 g_print ("Stop feeding\n");
84 g_source_remove (data->sourceid);
89 /* The appsink has received a buffer */
90 static void new_sample (GstElement *sink, CustomData *data) {
93 /* Retrieve the buffer */
94 g_signal_emit_by_name (sink, "pull-sample", &sample);
96 /* The only thing we do in this example is print a * to indicate a received buffer */
98 gst_sample_unref (sample);
102 /* This function is called when an error message is posted on the bus */
103 static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
107 /* Print error details on the screen */
108 gst_message_parse_error (msg, &err, &debug_info);
109 g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
110 g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
111 g_clear_error (&err);
114 g_main_loop_quit (data->main_loop);
117 int main(int argc, char *argv[]) {
119 GstPadTemplate *tee_src_pad_template;
120 GstPad *tee_audio_pad, *tee_video_pad, *tee_app_pad;
121 GstPad *queue_audio_pad, *queue_video_pad, *queue_app_pad;
126 /* Initialize cumstom data structure */
127 memset (&data, 0, sizeof (data));
128 data.b = 1; /* For waveform generation */
131 /* Initialize GStreamer */
132 gst_init (&argc, &argv);
134 /* Create the elements */
135 data.app_source = gst_element_factory_make ("appsrc", "audio_source");
136 data.tee = gst_element_factory_make ("tee", "tee");
137 data.audio_queue = gst_element_factory_make ("queue", "audio_queue");
138 data.audio_convert1 = gst_element_factory_make ("audioconvert", "audio_convert1");
139 data.audio_resample = gst_element_factory_make ("audioresample", "audio_resample");
140 data.audio_sink = gst_element_factory_make ("autoaudiosink", "audio_sink");
141 data.video_queue = gst_element_factory_make ("queue", "video_queue");
142 data.audio_convert2 = gst_element_factory_make ("audioconvert", "audio_convert2");
143 data.visual = gst_element_factory_make ("wavescope", "visual");
144 data.video_convert = gst_element_factory_make ("videoconvert", "video_convert");
145 data.video_sink = gst_element_factory_make ("autovideosink", "video_sink");
146 data.app_queue = gst_element_factory_make ("queue", "app_queue");
147 data.app_sink = gst_element_factory_make ("appsink", "app_sink");
149 /* Create the empty pipeline */
150 data.pipeline = gst_pipeline_new ("test-pipeline");
152 if (!data.pipeline || !data.app_source || !data.tee || !data.audio_queue || !data.audio_convert1 ||
153 !data.audio_resample || !data.audio_sink || !data.video_queue || !data.audio_convert2 || !data.visual ||
154 !data.video_convert || !data.video_sink || !data.app_queue || !data.app_sink) {
155 g_printerr ("Not all elements could be created.\n");
159 /* Configure wavescope */
160 g_object_set (data.visual, "shader", 0, "style", 0, NULL);
162 /* Configure appsrc */
163 gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, SAMPLE_RATE, 1, NULL);
164 audio_caps = gst_audio_info_to_caps (&info);
165 g_object_set (data.app_source, "caps", audio_caps, "format", GST_FORMAT_TIME, NULL);
166 g_signal_connect (data.app_source, "need-data", G_CALLBACK (start_feed), &data);
167 g_signal_connect (data.app_source, "enough-data", G_CALLBACK (stop_feed), &data);
169 /* Configure appsink */
170 g_object_set (data.app_sink, "emit-signals", TRUE, "caps", audio_caps, NULL);
171 g_signal_connect (data.app_sink, "new-sample", G_CALLBACK (new_sample), &data);
172 gst_caps_unref (audio_caps);
174 /* Link all elements that can be automatically linked because they have "Always" pads */
175 gst_bin_add_many (GST_BIN (data.pipeline), data.app_source, data.tee, data.audio_queue, data.audio_convert1, data.audio_resample,
176 data.audio_sink, data.video_queue, data.audio_convert2, data.visual, data.video_convert, data.video_sink, data.app_queue,
177 data.app_sink, NULL);
178 if (gst_element_link_many (data.app_source, data.tee, NULL) != TRUE ||
179 gst_element_link_many (data.audio_queue, data.audio_convert1, data.audio_resample, data.audio_sink, NULL) != TRUE ||
180 gst_element_link_many (data.video_queue, data.audio_convert2, data.visual, data.video_convert, data.video_sink, NULL) != TRUE ||
181 gst_element_link_many (data.app_queue, data.app_sink, NULL) != TRUE) {
182 g_printerr ("Elements could not be linked.\n");
183 gst_object_unref (data.pipeline);
187 /* Manually link the Tee, which has "Request" pads */
188 tee_src_pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (data.tee), "src_%u");
189 tee_audio_pad = gst_element_request_pad (data.tee, tee_src_pad_template, NULL, NULL);
190 g_print ("Obtained request pad %s for audio branch.\n", gst_pad_get_name (tee_audio_pad));
191 queue_audio_pad = gst_element_get_static_pad (data.audio_queue, "sink");
192 tee_video_pad = gst_element_request_pad (data.tee, tee_src_pad_template, NULL, NULL);
193 g_print ("Obtained request pad %s for video branch.\n", gst_pad_get_name (tee_video_pad));
194 queue_video_pad = gst_element_get_static_pad (data.video_queue, "sink");
195 tee_app_pad = gst_element_request_pad (data.tee, tee_src_pad_template, NULL, NULL);
196 g_print ("Obtained request pad %s for app branch.\n", gst_pad_get_name (tee_app_pad));
197 queue_app_pad = gst_element_get_static_pad (data.app_queue, "sink");
198 if (gst_pad_link (tee_audio_pad, queue_audio_pad) != GST_PAD_LINK_OK ||
199 gst_pad_link (tee_video_pad, queue_video_pad) != GST_PAD_LINK_OK ||
200 gst_pad_link (tee_app_pad, queue_app_pad) != GST_PAD_LINK_OK) {
201 g_printerr ("Tee could not be linked\n");
202 gst_object_unref (data.pipeline);
205 gst_object_unref (queue_audio_pad);
206 gst_object_unref (queue_video_pad);
207 gst_object_unref (queue_app_pad);
209 /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
210 bus = gst_element_get_bus (data.pipeline);
211 gst_bus_add_signal_watch (bus);
212 g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, &data);
213 gst_object_unref (bus);
215 /* Start playing the pipeline */
216 gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
218 /* Create a GLib Main Loop and set it to run */
219 data.main_loop = g_main_loop_new (NULL, FALSE);
220 g_main_loop_run (data.main_loop);
222 /* Release the request pads from the Tee, and unref them */
223 gst_element_release_request_pad (data.tee, tee_audio_pad);
224 gst_element_release_request_pad (data.tee, tee_video_pad);
225 gst_element_release_request_pad (data.tee, tee_app_pad);
226 gst_object_unref (tee_audio_pad);
227 gst_object_unref (tee_video_pad);
228 gst_object_unref (tee_app_pad);
231 gst_element_set_state (data.pipeline, GST_STATE_NULL);
232 gst_object_unref (data.pipeline);