tizen 2.3 release
[framework/multimedia/gst-plugins-base0.10.git] / tests / examples / app / appsrc-stream.c
1 /* GStreamer
2  *
3  * appsrc-stream.c: example for using appsrc in streaming mode.
4  *
5  * Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gst/gst.h>
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 GST_DEBUG_CATEGORY (appsrc_playbin_debug);
34 #define GST_CAT_DEFAULT appsrc_playbin_debug
35
36 /*
37  * an example application of using appsrc in streaming push mode. We simply push
38  * buffers into appsrc. The size of the buffers we push can be any size we
39  * choose.
40  *
41  * This example is very close to how one would deal with a streaming webserver
42  * that does not support range requests or does not report the total file size.
43  *
44  * Some optimisations are done so that we don't push too much data. We connect
45  * to the need-data and enough-data signals to start/stop sending buffers.
46  *
47  * Appsrc in streaming mode (the default) does not support seeking so we don't
48  * have to handle any seek callbacks.
49  *
50  * Some formats are able to estimate the duration of the media file based on the
51  * file length (mp3, mpeg,..), others report an unknown length (ogg,..).
52  */
53 typedef struct _App App;
54
55 struct _App
56 {
57   GstElement *playbin;
58   GstElement *appsrc;
59
60   GMainLoop *loop;
61   guint sourceid;
62
63   GMappedFile *file;
64   guint8 *data;
65   gsize length;
66   guint64 offset;
67 };
68
69 App s_app;
70
71 #define CHUNK_SIZE  4096
72
73 /* This method is called by the idle GSource in the mainloop. We feed CHUNK_SIZE
74  * bytes into appsrc.
75  * The ide handler is added to the mainloop when appsrc requests us to start
76  * sending data (need-data signal) and is removed when appsrc has enough data
77  * (enough-data signal).
78  */
79 static gboolean
80 read_data (App * app)
81 {
82   GstBuffer *buffer;
83   guint len;
84   GstFlowReturn ret;
85
86   buffer = gst_buffer_new ();
87
88   if (app->offset >= app->length) {
89     /* we are EOS, send end-of-stream and remove the source */
90     g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret);
91     return FALSE;
92   }
93
94   /* read the next chunk */
95   len = CHUNK_SIZE;
96   if (app->offset + len > app->length)
97     len = app->length - app->offset;
98
99   GST_BUFFER_DATA (buffer) = app->data + app->offset;
100   GST_BUFFER_SIZE (buffer) = len;
101
102   GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u", buffer,
103       app->offset, len);
104   g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret);
105   gst_buffer_unref (buffer);
106   if (ret != GST_FLOW_OK) {
107     /* some error, stop sending data */
108     return FALSE;
109   }
110
111   app->offset += len;
112
113   return TRUE;
114 }
115
116 /* This signal callback is called when appsrc needs data, we add an idle handler
117  * to the mainloop to start pushing data into the appsrc */
118 static void
119 start_feed (GstElement * playbin, guint size, App * app)
120 {
121   if (app->sourceid == 0) {
122     GST_DEBUG ("start feeding");
123     app->sourceid = g_idle_add ((GSourceFunc) read_data, app);
124   }
125 }
126
127 /* This callback is called when appsrc has enough data and we can stop sending.
128  * We remove the idle handler from the mainloop */
129 static void
130 stop_feed (GstElement * playbin, App * app)
131 {
132   if (app->sourceid != 0) {
133     GST_DEBUG ("stop feeding");
134     g_source_remove (app->sourceid);
135     app->sourceid = 0;
136   }
137 }
138
139 /* this callback is called when playbin2 has constructed a source object to read
140  * from. Since we provided the appsrc:// uri to playbin2, this will be the
141  * appsrc that we must handle. We set up some signals to start and stop pushing
142  * data into appsrc */
143 static void
144 found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app)
145 {
146   /* get a handle to the appsrc */
147   g_object_get (orig, pspec->name, &app->appsrc, NULL);
148
149   GST_DEBUG ("got appsrc %p", app->appsrc);
150
151   /* we can set the length in appsrc. This allows some elements to estimate the
152    * total duration of the stream. It's a good idea to set the property when you
153    * can but it's not required. */
154   g_object_set (app->appsrc, "size", (gint64) app->length, NULL);
155
156   /* configure the appsrc, we will push data into the appsrc from the
157    * mainloop. */
158   g_signal_connect (app->appsrc, "need-data", G_CALLBACK (start_feed), app);
159   g_signal_connect (app->appsrc, "enough-data", G_CALLBACK (stop_feed), app);
160 }
161
162 static gboolean
163 bus_message (GstBus * bus, GstMessage * message, App * app)
164 {
165   GST_DEBUG ("got message %s",
166       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
167
168   switch (GST_MESSAGE_TYPE (message)) {
169     case GST_MESSAGE_ERROR:
170       g_error ("received error");
171       g_main_loop_quit (app->loop);
172       break;
173     case GST_MESSAGE_EOS:
174       g_main_loop_quit (app->loop);
175       break;
176     default:
177       break;
178   }
179   return TRUE;
180 }
181
182 int
183 main (int argc, char *argv[])
184 {
185   App *app = &s_app;
186   GError *error = NULL;
187   GstBus *bus;
188
189   gst_init (&argc, &argv);
190
191   GST_DEBUG_CATEGORY_INIT (appsrc_playbin_debug, "appsrc-playbin", 0,
192       "appsrc playbin example");
193
194   if (argc < 2) {
195     g_print ("usage: %s <filename>\n", argv[0]);
196     return -1;
197   }
198
199   /* try to open the file as an mmapped file */
200   app->file = g_mapped_file_new (argv[1], FALSE, &error);
201   if (error) {
202     g_print ("failed to open file: %s\n", error->message);
203     g_error_free (error);
204     return -2;
205   }
206   /* get some vitals, this will be used to read data from the mmapped file and
207    * feed it to appsrc. */
208   app->length = g_mapped_file_get_length (app->file);
209   app->data = (guint8 *) g_mapped_file_get_contents (app->file);
210   app->offset = 0;
211
212   /* create a mainloop to get messages and to handle the idle handler that will
213    * feed data to appsrc. */
214   app->loop = g_main_loop_new (NULL, TRUE);
215
216   app->playbin = gst_element_factory_make ("playbin2", NULL);
217   g_assert (app->playbin);
218
219   bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin));
220
221   /* add watch for messages */
222   gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
223
224   /* set to read from appsrc */
225   g_object_set (app->playbin, "uri", "appsrc://", NULL);
226
227   /* get notification when the source is created so that we get a handle to it
228    * and can configure it */
229   g_signal_connect (app->playbin, "deep-notify::source",
230       (GCallback) found_source, app);
231
232   /* go to playing and wait in a mainloop. */
233   gst_element_set_state (app->playbin, GST_STATE_PLAYING);
234
235   /* this mainloop is stopped when we receive an error or EOS */
236   g_main_loop_run (app->loop);
237
238   GST_DEBUG ("stopping");
239
240   gst_element_set_state (app->playbin, GST_STATE_NULL);
241
242   /* free the file */
243   g_mapped_file_unref (app->file);
244
245   gst_object_unref (bus);
246   g_main_loop_unref (app->loop);
247
248   return 0;
249 }