3 * appsrc-stream2.c: example for using appsrc in streaming mode.
5 * Copyright (C) 2008 Wim Taymans <wim.taymans@gmail.com>
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.
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.
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
33 GST_DEBUG_CATEGORY (appsrc_playbin_debug);
34 #define GST_CAT_DEFAULT appsrc_playbin_debug
37 * an example application of using appsrc in streaming pull mode. When the
38 * appsrc request data with the need-data signal, we retrieve a buffer of an
39 * arbitrary size and push it to appsrc.
41 * This example keeps the internal buffer queue of appsrc to a minimal size,
42 * only feeding data to appsrc when needed.
44 * This is a good example how one would deal with a live resource, such as a udp
45 * socket where one would feed the most recently acquired buffer to appsrc.
47 * Usually one would timestamp the buffers with the running_time of the
48 * pipeline or configure the appsrc to do timestamping by setting the
49 * do-timestamp property to TRUE.
51 * Appsrc in streaming mode (the default) does not support seeking so we don't
52 * have to handle any seek callbacks.
54 * Some formats are able to estimate the duration of the media file based on the
55 * file length (mp3, mpeg,..), others report an unknown length (ogg,..).
57 typedef struct _App App;
74 #define CHUNK_SIZE 4096
76 /* This method is called by the need-data signal callback, we feed data into the
80 feed_data (GstElement * appsrc, guint size, App * app)
86 if (app->offset >= app->length) {
87 /* we are EOS, send end-of-stream */
88 g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret);
92 /* read the next chunk */
93 buffer = gst_buffer_new ();
96 if (app->offset + len > app->length)
97 len = app->length - app->offset;
99 gst_buffer_append_memory (buffer,
100 gst_memory_new_wrapped (GST_MEMORY_FLAG_READONLY,
101 app->data, app->length, app->offset, len, NULL, NULL));
103 GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u", buffer,
105 g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret);
106 gst_buffer_unref (buffer);
113 /* this callback is called when playbin has constructed a source object to read
114 * from. Since we provided the appsrc:// uri to playbin, this will be the
115 * appsrc that we must handle. We set up a signals to push data into appsrc. */
117 found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app)
119 /* get a handle to the appsrc */
120 g_object_get (orig, pspec->name, &app->appsrc, NULL);
122 GST_DEBUG ("got appsrc %p", app->appsrc);
124 /* we can set the length in appsrc. This allows some elements to estimate the
125 * total duration of the stream. It's a good idea to set the property when you
126 * can but it's not required. */
127 g_object_set (app->appsrc, "size", (gint64) app->length, NULL);
129 /* configure the appsrc, we will push a buffer to appsrc when it needs more
131 g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app);
135 bus_message (GstBus * bus, GstMessage * message, App * app)
137 GST_DEBUG ("got message %s",
138 gst_message_type_get_name (GST_MESSAGE_TYPE (message)));
140 switch (GST_MESSAGE_TYPE (message)) {
141 case GST_MESSAGE_ERROR:
142 g_error ("received error");
143 g_main_loop_quit (app->loop);
145 case GST_MESSAGE_EOS:
146 g_main_loop_quit (app->loop);
155 main (int argc, char *argv[])
158 GError *error = NULL;
161 gst_init (&argc, &argv);
163 GST_DEBUG_CATEGORY_INIT (appsrc_playbin_debug, "appsrc-playbin", 0,
164 "appsrc playbin example");
167 g_print ("usage: %s <filename>\n", argv[0]);
171 /* try to open the file as an mmapped file */
172 app->file = g_mapped_file_new (argv[1], FALSE, &error);
174 g_print ("failed to open file: %s\n", error->message);
175 g_error_free (error);
178 /* get some vitals, this will be used to read data from the mmapped file and
179 * feed it to appsrc. */
180 app->length = g_mapped_file_get_length (app->file);
181 app->data = (guint8 *) g_mapped_file_get_contents (app->file);
184 /* create a mainloop to get messages */
185 app->loop = g_main_loop_new (NULL, TRUE);
187 app->playbin = gst_element_factory_make ("playbin", NULL);
188 g_assert (app->playbin);
190 bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin));
192 /* add watch for messages */
193 gst_bus_add_watch (bus, (GstBusFunc) bus_message, app);
195 /* set to read from appsrc */
196 g_object_set (app->playbin, "uri", "appsrc://", NULL);
198 /* get notification when the source is created so that we get a handle to it
199 * and can configure it */
200 g_signal_connect (app->playbin, "deep-notify::source",
201 (GCallback) found_source, app);
203 /* go to playing and wait in a mainloop. */
204 gst_element_set_state (app->playbin, GST_STATE_PLAYING);
206 /* this mainloop is stopped when we receive an error or EOS */
207 g_main_loop_run (app->loop);
209 GST_DEBUG ("stopping");
211 gst_element_set_state (app->playbin, GST_STATE_NULL);
214 g_mapped_file_unref (app->file);
216 gst_object_unref (bus);
217 g_main_loop_unref (app->loop);