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 {
11 GstElement *app_source;
13 guint64 num_samples; /* Number of samples generated so far (for timestamp generation) */
14 gfloat a, b, c, d; /* For waveform generation */
16 guint sourceid; /* To control the GSource */
18 GMainLoop *main_loop; /* GLib's Main Loop */
21 /* This method is called by the idle GSource in the mainloop, to feed CHUNK_SIZE bytes into appsrc.
22 * The ide handler is added to the mainloop when appsrc requests us to start sending data (need-data signal)
23 * and is removed when appsrc has enough data (enough-data signal).
25 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 gst_buffer_map (buffer, &map, GST_MAP_WRITE);
43 raw = (gint16 *)map.data;
45 data->d -= data->c / 1000;
46 freq = 1100 + 1000 * data->d;
47 for (i = 0; i < num_samples; i++) {
49 data->b -= data->a / freq;
50 raw[i] = (gint16)(500 * data->a);
52 gst_buffer_unmap (buffer, &map);
53 data->num_samples += num_samples;
55 /* Push the buffer into the appsrc */
56 g_signal_emit_by_name (data->app_source, "push-buffer", buffer, &ret);
58 /* Free the buffer now that we are done with it */
59 gst_buffer_unref (buffer);
61 if (ret != GST_FLOW_OK) {
62 /* We got some error, stop sending data */
69 /* This signal callback triggers when appsrc needs data. Here, we add an idle handler
70 * to the mainloop to start pushing data into the appsrc */
71 static void start_feed (GstElement *source, guint size, CustomData *data) {
72 if (data->sourceid == 0) {
73 g_print ("Start feeding\n");
74 data->sourceid = g_idle_add ((GSourceFunc) push_data, data);
78 /* This callback triggers when appsrc has enough data and we can stop sending.
79 * We remove the idle handler from the mainloop */
80 static void stop_feed (GstElement *source, CustomData *data) {
81 if (data->sourceid != 0) {
82 g_print ("Stop feeding\n");
83 g_source_remove (data->sourceid);
88 /* This function is called when an error message is posted on the bus */
89 static void error_cb (GstBus *bus, GstMessage *msg, CustomData *data) {
93 /* Print error details on the screen */
94 gst_message_parse_error (msg, &err, &debug_info);
95 g_printerr ("Error received from element %s: %s\n", GST_OBJECT_NAME (msg->src), err->message);
96 g_printerr ("Debugging information: %s\n", debug_info ? debug_info : "none");
100 g_main_loop_quit (data->main_loop);
103 /* This function is called when playbin has created the appsrc element, so we have
104 * a chance to configure it. */
105 static void source_setup (GstElement *pipeline, GstElement *source, CustomData *data) {
109 g_print ("Source has been created. Configuring.\n");
110 data->app_source = source;
112 /* Configure appsrc */
113 gst_audio_info_set_format (&info, GST_AUDIO_FORMAT_S16, SAMPLE_RATE, 1, NULL);
114 audio_caps = gst_audio_info_to_caps (&info);
115 g_object_set (source, "caps", audio_caps, "format", GST_FORMAT_TIME, NULL);
116 g_signal_connect (source, "need-data", G_CALLBACK (start_feed), data);
117 g_signal_connect (source, "enough-data", G_CALLBACK (stop_feed), data);
118 gst_caps_unref (audio_caps);
121 int main(int argc, char *argv[]) {
125 /* Initialize cumstom data structure */
126 memset (&data, 0, sizeof (data));
127 data.b = 1; /* For waveform generation */
130 /* Initialize GStreamer */
131 gst_init (&argc, &argv);
133 /* Create the playbin element */
134 data.pipeline = gst_parse_launch ("playbin uri=appsrc://", NULL);
135 g_signal_connect (data.pipeline, "source-setup", G_CALLBACK (source_setup), &data);
137 /* Instruct the bus to emit signals for each received message, and connect to the interesting signals */
138 bus = gst_element_get_bus (data.pipeline);
139 gst_bus_add_signal_watch (bus);
140 g_signal_connect (G_OBJECT (bus), "message::error", (GCallback)error_cb, &data);
141 gst_object_unref (bus);
143 /* Start playing the pipeline */
144 gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
146 /* Create a GLib Main Loop and set it to run */
147 data.main_loop = g_main_loop_new (NULL, FALSE);
148 g_main_loop_run (data.main_loop);
151 gst_element_set_state (data.pipeline, GST_STATE_NULL);
152 gst_object_unref (data.pipeline);