|
1 /* GStreamer |
|
2 * |
|
3 * appsrc-seekable.c: example for using appsrc in seekable 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 seekable mode. When the |
|
38 * appsrc requests data with the need-data signal, we retrieve a buffer and |
|
39 * push it to appsrc. We can also use the method as demonstrated in |
|
40 * appsrc-stream.c, ie. pushing buffers when we can. |
|
41 * |
|
42 * This is a good example how one would deal with a remote http server that |
|
43 * supports range requests. |
|
44 * |
|
45 * Appsrc in seekable mode needs seeking support and we must thus connect |
|
46 * to the seek signal to perform any seeks when requested. |
|
47 * |
|
48 * In seekable mode we should set the size of the source material. |
|
49 */ |
|
50 typedef struct _App App; |
|
51 |
|
52 struct _App |
|
53 { |
|
54 GstElement *playbin; |
|
55 GstElement *appsrc; |
|
56 |
|
57 GMainLoop *loop; |
|
58 |
|
59 GMappedFile *file; |
|
60 guint8 *data; |
|
61 gsize length; |
|
62 guint64 offset; |
|
63 }; |
|
64 |
|
65 App s_app; |
|
66 |
|
67 #define CHUNK_SIZE 4096 |
|
68 |
|
69 /* This method is called by the need-data signal callback, we feed data into the |
|
70 * appsrc with an arbitrary size. |
|
71 */ |
|
72 static void |
|
73 feed_data (GstElement * appsrc, guint size, App * app) |
|
74 { |
|
75 GstBuffer *buffer; |
|
76 guint len; |
|
77 GstFlowReturn ret; |
|
78 |
|
79 buffer = gst_buffer_new (); |
|
80 |
|
81 if (app->offset >= app->length) { |
|
82 /* we are EOS, send end-of-stream */ |
|
83 g_signal_emit_by_name (app->appsrc, "end-of-stream", &ret); |
|
84 return; |
|
85 } |
|
86 |
|
87 /* read any amount of data, we are allowed to return less if we are EOS */ |
|
88 len = CHUNK_SIZE; |
|
89 if (app->offset + len > app->length) |
|
90 len = app->length - app->offset; |
|
91 |
|
92 GST_BUFFER_DATA (buffer) = app->data + app->offset; |
|
93 GST_BUFFER_SIZE (buffer) = len; |
|
94 |
|
95 GST_DEBUG ("feed buffer %p, offset %" G_GUINT64_FORMAT "-%u", buffer, |
|
96 app->offset, len); |
|
97 g_signal_emit_by_name (app->appsrc, "push-buffer", buffer, &ret); |
|
98 gst_buffer_unref (buffer); |
|
99 |
|
100 app->offset += len; |
|
101 |
|
102 return; |
|
103 } |
|
104 |
|
105 /* called when appsrc wants us to return data from a new position with the next |
|
106 * call to push-buffer. */ |
|
107 static gboolean |
|
108 seek_data (GstElement * appsrc, guint64 position, App * app) |
|
109 { |
|
110 GST_DEBUG ("seek to offset %" G_GUINT64_FORMAT, position); |
|
111 app->offset = position; |
|
112 |
|
113 return TRUE; |
|
114 } |
|
115 |
|
116 /* this callback is called when playbin2 has constructed a source object to read |
|
117 * from. Since we provided the appsrc:// uri to playbin2, this will be the |
|
118 * appsrc that we must handle. We set up some signals to push data into appsrc |
|
119 * and one to perform a seek. */ |
|
120 static void |
|
121 found_source (GObject * object, GObject * orig, GParamSpec * pspec, App * app) |
|
122 { |
|
123 /* get a handle to the appsrc */ |
|
124 g_object_get (orig, pspec->name, &app->appsrc, NULL); |
|
125 |
|
126 GST_DEBUG ("got appsrc %p", app->appsrc); |
|
127 |
|
128 /* we can set the length in appsrc. This allows some elements to estimate the |
|
129 * total duration of the stream. It's a good idea to set the property when you |
|
130 * can but it's not required. */ |
|
131 g_object_set (app->appsrc, "size", (gint64) app->length, NULL); |
|
132 /* we are seekable in push mode, this means that the element usually pushes |
|
133 * out buffers of an undefined size and that seeks happen only occasionally |
|
134 * and only by request of the user. */ |
|
135 gst_util_set_object_arg (G_OBJECT (app->appsrc), "stream-type", "seekable"); |
|
136 |
|
137 /* configure the appsrc, we will push a buffer to appsrc when it needs more |
|
138 * data */ |
|
139 g_signal_connect (app->appsrc, "need-data", G_CALLBACK (feed_data), app); |
|
140 g_signal_connect (app->appsrc, "seek-data", G_CALLBACK (seek_data), app); |
|
141 } |
|
142 |
|
143 static gboolean |
|
144 bus_message (GstBus * bus, GstMessage * message, App * app) |
|
145 { |
|
146 GST_DEBUG ("got message %s", |
|
147 gst_message_type_get_name (GST_MESSAGE_TYPE (message))); |
|
148 |
|
149 switch (GST_MESSAGE_TYPE (message)) { |
|
150 case GST_MESSAGE_ERROR: |
|
151 g_error ("received error"); |
|
152 g_main_loop_quit (app->loop); |
|
153 break; |
|
154 case GST_MESSAGE_EOS: |
|
155 g_main_loop_quit (app->loop); |
|
156 break; |
|
157 default: |
|
158 break; |
|
159 } |
|
160 return TRUE; |
|
161 } |
|
162 |
|
163 int |
|
164 main (int argc, char *argv[]) |
|
165 { |
|
166 App *app = &s_app; |
|
167 GError *error = NULL; |
|
168 GstBus *bus; |
|
169 |
|
170 gst_init (&argc, &argv); |
|
171 |
|
172 GST_DEBUG_CATEGORY_INIT (appsrc_playbin_debug, "appsrc-playbin", 0, |
|
173 "appsrc playbin example"); |
|
174 |
|
175 if (argc < 2) { |
|
176 g_print ("usage: %s <filename>\n", argv[0]); |
|
177 return -1; |
|
178 } |
|
179 |
|
180 /* try to open the file as an mmapped file */ |
|
181 app->file = g_mapped_file_new (argv[1], FALSE, &error); |
|
182 if (error) { |
|
183 g_print ("failed to open file: %s\n", error->message); |
|
184 g_error_free (error); |
|
185 return -2; |
|
186 } |
|
187 /* get some vitals, this will be used to read data from the mmapped file and |
|
188 * feed it to appsrc. */ |
|
189 app->length = g_mapped_file_get_length (app->file); |
|
190 app->data = (guint8 *) g_mapped_file_get_contents (app->file); |
|
191 app->offset = 0; |
|
192 |
|
193 /* create a mainloop to get messages */ |
|
194 app->loop = g_main_loop_new (NULL, TRUE); |
|
195 |
|
196 app->playbin = gst_element_factory_make ("playbin2", NULL); |
|
197 g_assert (app->playbin); |
|
198 |
|
199 bus = gst_pipeline_get_bus (GST_PIPELINE (app->playbin)); |
|
200 |
|
201 /* add watch for messages */ |
|
202 gst_bus_add_watch (bus, (GstBusFunc) bus_message, app); |
|
203 |
|
204 /* set to read from appsrc */ |
|
205 g_object_set (app->playbin, "uri", "appsrc://", NULL); |
|
206 |
|
207 /* get notification when the source is created so that we get a handle to it |
|
208 * and can configure it */ |
|
209 g_signal_connect (app->playbin, "deep-notify::source", |
|
210 (GCallback) found_source, app); |
|
211 |
|
212 /* go to playing and wait in a mainloop. */ |
|
213 gst_element_set_state (app->playbin, GST_STATE_PLAYING); |
|
214 |
|
215 /* this mainloop is stopped when we receive an error or EOS */ |
|
216 g_main_loop_run (app->loop); |
|
217 |
|
218 GST_DEBUG ("stopping"); |
|
219 |
|
220 gst_element_set_state (app->playbin, GST_STATE_NULL); |
|
221 |
|
222 /* free the file */ |
|
223 g_mapped_file_free (app->file); |
|
224 |
|
225 gst_object_unref (bus); |
|
226 g_main_loop_unref (app->loop); |
|
227 |
|
228 return 0; |
|
229 } |