986dfb8ed5b11ccc4e4c9dcbeaad32c45bac7583
[platform/upstream/gstreamer.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 /* FIXME: remove once we depend on GLib >= 2.22 */
34 #if !GLIB_CHECK_VERSION (2, 21, 3)
35 #define g_mapped_file_unref g_mapped_file_free
36 #endif
37
38 GST_DEBUG_CATEGORY (appsrc_playbin_debug);
39 #define GST_CAT_DEFAULT appsrc_playbin_debug
40
41 /*
42  * an example application of using appsrc in streaming push mode. We simply push
43  * buffers into appsrc. The size of the buffers we push can be any size we
44  * choose.
45  *
46  * This example is very close to how one would deal with a streaming webserver
47  * that does not support range requests or does not report the total file size.
48  *
49  * Some optimisations are done so that we don't push too much data. We connect
50  * to the need-data and enough-data signals to start/stop sending buffers.
51  *
52  * Appsrc in streaming mode (the default) does not support seeking so we don't
53  * have to handle any seek callbacks.
54  *
55  * Some formats are able to estimate the duration of the media file based on the
56  * file length (mp3, mpeg,..), others report an unknown length (ogg,..).
57  */
58 typedef struct _App App;
59
60 struct _App
61 {
62   GstElement *playbin;
63   GstElement *appsrc;
64
65   GMainLoop *loop;
66   guint sourceid;
67
68   GMappedFile *file;
69   guint8 *data;
70   gsize length;
71   guint64 offset;
72 };
73
74 App s_app;
75
76 #define CHUNK_SIZE  4096
77
78 /* This method is called by the idle GSource in the mainloop. We feed CHUNK_SIZE
79  * bytes into appsrc.
80  * The ide handler is added to the mainloop when appsrc requests us to start
81  * sending data (need-data signal) and is removed when appsrc has enough data
82  * (enough-data signal).
83  */
84 static gboolean
85 read_data (App * app)
86 {
87   GstBuffer *buffer;
88   guint len;
89   GstFlowReturn ret;
90
91   buffer = gst_buffer_new ();
92
93   if (app->offset >= app->length) {
94     /* we are EOS, send end-of-stream and remove the source */
95     g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret);
96     return FALSE;
97   }
98
99   /* read the next chunk */
100   len = CHUNK_SIZE;
101   if (app->offset + len > app->length)
102     len = app->length - app->offset;
103
104   GST_BUFFER_DATA (buffer) = app->data + app->offset;
105   GST_BUFFER_SIZE (buffer) = len;
106
107   GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u", buffer,
108       app->offset, len);
109   g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret);
110   gst_buffer_unref (buffer);
111   if (ret != GST_FLOW_OK) {
112     /* some error, stop sending data */
113     return FALSE;
114   }
115
116   app->offset += len;
117
118   return TRUE;
119 }
120
121 /* This signal callback is called when appsrc needs data, we add an idle handler
122  * to the mainloop to start pushing data into the appsrc */
123 static void
124 start_feed (GstElement * playbin, guint size, App * app)
125 {
126   if (app->sourceid == 0) {
127     GST_DEBUG ("start feeding");
128     app->sourceid = g_idle_add ((GSourceFunc) read_data, app);
129   }
130 }
131
132 /* This callback is called when appsrc has enough data and we can stop sending.
133  * We remove the idle handler from the mainloop */
134 static void
135 stop_feed (GstElement * playbin, App * app)
136 {
137   if (app->sourceid != 0) {
138     GST_DEBUG ("stop feeding");
139     g_source_remove (app->sourceid);
140     app->sourceid = 0;
141   }
142 }
143
144 /* this callback is called when playbin2 has constructed a source object to read
145  * from. Since we provided the appsrc:// uri to playbin2, this will be the
146  * appsrc that we must handle. We set up some signals to start and stop pushing
147  * data into appsrc */
148 static void
149 found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app)
150 {
151   /* get a handle to the appsrc */
152   g_object_get (orig, pspec->name, &app->appsrc, NULL);
153
154   GST_DEBUG ("got appsrc %p", app->appsrc);
155
156   /* we can set the length in appsrc. This allows some elements to estimate the
157    * total duration of the stream. It's a good idea to set the property when you
158    * can but it's not required. */
159   g_object_set (app->appsrc, "size", (gint64) app->length, NULL);
160
161   /* configure the appsrc, we will push data into the appsrc from the
162    * mainloop. */
163   g_signal_connect (app->appsrc, "need-data", G_CALLBACK (start_feed), app);
164   g_signal_connect (app->appsrc, "enough-data", G_CALLBACK (stop_feed), app);
165 }
166
167 static gboolean
168 bus_message (GstBus * bus, GstMessage * message, App * app)
169 {
170   GST_DEBUG ("got message %s",
171       gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
172
173   switch (GST_MESSAGE_TYPE (message)) {
174     case GST_MESSAGE_ERROR:
175       g_error ("received error");
176       g_main_loop_quit (app->loop);
177       break;
178     case GST_MESSAGE_EOS:
179       g_main_loop_quit (app->loop);
180       break;
181     default:
182       break;
183   }
184   return TRUE;
185 }
186
187 int
188 main (int argc, char *argv[])
189 {
190   App *app = &s_app;
191   GError *error = NULL;
192   GstBus *bus;
193
194   gst_init (&argc, &argv);
195
196   GST_DEBUG_CATEGORY_INIT (appsrc_playbin_debug, "appsrc-playbin", 0,
197       "appsrc playbin example");
198
199   if (argc < 2) {
200     g_print ("usage: %s <filename>\n", argv[0]);
201     return -1;
202   }
203
204   /* try to open the file as an mmapped file */
205   app->file = g_mapped_file_new (argv[1], FALSE, &error);
206   if (error) {
207     g_print ("failed to open file: %s\n", error->message);
208     g_error_free (error);
209     return -2;
210   }
211   /* get some vitals, this will be used to read data from the mmapped file and
212    * feed it to appsrc. */
213   app->length = g_mapped_file_get_length (app->file);
214   app->data = (guint8 *) g_mapped_file_get_contents (app->file);
215   app->offset = 0;
216
217   /* create a mainloop to get messages and to handle the idle handler that will
218    * feed data to appsrc. */
219   app->loop = g_main_loop_new (NULL, TRUE);
220
221   app->playbin = gst_element_factory_make ("playbin2", NULL);
222   g_assert (app->playbin);
223
224   bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin));
225
226   /* add watch for messages */
227   gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
228
229   /* set to read from appsrc */
230   g_object_set (app->playbin, "uri", "appsrc://", NULL);
231
232   /* get notification when the source is created so that we get a handle to it
233    * and can configure it */
234   g_signal_connect (app->playbin, "deep-notify::source",
235       (GCallback) found_source, app);
236
237   /* go to playing and wait in a mainloop. */
238   gst_element_set_state (app->playbin, GST_STATE_PLAYING);
239
240   /* this mainloop is stopped when we receive an error or EOS */
241   g_main_loop_run (app->loop);
242
243   GST_DEBUG ("stopping");
244
245   gst_element_set_state (app->playbin, GST_STATE_NULL);
246
247   /* free the file */
248   g_mapped_file_unref (app->file);
249
250   gst_object_unref (bus);
251   g_main_loop_unref (app->loop);
252
253   return 0;
254 }