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