4f8b8099e3a3eae89311e80bdb93f43cdd66f10c
[platform/upstream/gstreamer.git] / tutorials / playback-tutorial-3.c
1 #include <gst/gst.h>
2 #include <gst/audio/audio.h>
3 #include <string.h>
4   
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 */
7   
8 /* Structure to contain all our information, so we can pass it to callbacks */
9 typedef struct _CustomData {
10   GstElement *pipeline;
11   GstElement *app_source;
12   
13   guint64 num_samples;   /* Number of samples generated so far (for timestamp generation) */
14   gfloat a, b, c, d;     /* For waveform generation */
15   
16   guint sourceid;        /* To control the GSource */
17   
18   GMainLoop *main_loop;  /* GLib's Main Loop */
19 } CustomData;
20   
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).
24  */
25 static gboolean push_data (CustomData *data) {
26   GstBuffer *buffer;
27   GstFlowReturn ret;
28   int i;
29   GstMapInfo map;
30   gint16 *raw;
31   gint num_samples = CHUNK_SIZE / 2; /* Because each sample is 16 bits */
32   gfloat freq;
33   
34   /* Create a new empty buffer */
35   buffer = gst_buffer_new_and_alloc (CHUNK_SIZE);
36   
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);
40   
41   /* Generate some psychodelic waveforms */
42   gst_buffer_map (buffer, &map, GST_MAP_WRITE);
43   raw = (gint16 *)map.data;
44   data->c += data->d;
45   data->d -= data->c / 1000;
46   freq = 1100 + 1000 * data->d;
47   for (i = 0; i < num_samples; i++) {
48     data->a += data->b;
49     data->b -= data->a / freq;
50     raw[i] = (gint16)(500 * data->a);
51   }
52   gst_buffer_unmap (buffer, &map);
53   data->num_samples += num_samples;
54   
55   /* Push the buffer into the appsrc */
56   g_signal_emit_by_name (data->app_source, "push-buffer", buffer, &ret);
57   
58   /* Free the buffer now that we are done with it */
59   gst_buffer_unref (buffer);
60   
61   if (ret != GST_FLOW_OK) {
62     /* We got some error, stop sending data */
63     return FALSE;
64   }
65   
66   return TRUE;
67 }
68   
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);
75   }
76 }
77   
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);
84     data->sourceid = 0;
85   }
86 }
87   
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) {
90   GError *err;
91   gchar *debug_info;
92   
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");
97   g_clear_error (&err);
98   g_free (debug_info);
99   
100   g_main_loop_quit (data->main_loop);
101 }
102   
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) {
106   GstAudioInfo info;
107   GstCaps *audio_caps;
108   
109   g_print ("Source has been created. Configuring.\n");
110   data->app_source = source;
111   
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);
119 }
120   
121 int main(int argc, char *argv[]) {
122   CustomData data;
123   GstBus *bus;
124   
125   /* Initialize cumstom data structure */
126   memset (&data, 0, sizeof (data));
127   data.b = 1; /* For waveform generation */
128   data.d = 1;
129   
130   /* Initialize GStreamer */
131   gst_init (&argc, &argv);
132   
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);
136   
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);
142   
143   /* Start playing the pipeline */
144   gst_element_set_state (data.pipeline, GST_STATE_PLAYING);
145   
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);
149   
150   /* Free resources */
151   gst_element_set_state (data.pipeline, GST_STATE_NULL);
152   gst_object_unref (data.pipeline);
153   return 0;
154 }