1 /* GStreamer Push File Source
2 * Copyright (C) <2007> Tim-Philipp Müller <tim centricular net>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * SECTION:element-pushfilesrc
22 * @title: pushfilesrc
23 * @see_also: filesrc
24 *
25 * This element is only useful for debugging purposes. It implements an URI
26 * protocol handler for the 'pushfile' protocol and behaves like a file source
27 * element that cannot be activated in pull-mode. This makes it very easy to
28 * debug demuxers or decoders that can operate both pull and push-based in
29 * connection with the playbin element (which creates a source based on the
30 * URI passed).
31 *
32 * ## Example launch line
33 * |[
34 * gst-launch-1.0 -m playbin uri=pushfile:///home/you/some/file.ogg
35 * ]| This plays back the given file using playbin, with the demuxer operating
36 * push-based.
37 *
38 */
39
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43
44 #include "gstdebugutilselements.h"
45 #include "gstpushfilesrc.h"
46
47 #include <gst/gst.h>
48
49 GST_DEBUG_CATEGORY_STATIC (pushfilesrc_debug);
50 #define GST_CAT_DEFAULT pushfilesrc_debug
51
52 enum
53 {
54 PROP_0,
55 PROP_LOCATION,
56 PROP_TIME_SEGMENT,
57 PROP_STREAM_TIME,
58 PROP_START_TIME,
59 PROP_INITIAL_TIMESTAMP,
60 PROP_RATE,
61 PROP_APPLIED_RATE
62 };
63
64 #define DEFAULT_TIME_SEGMENT FALSE
65 #define DEFAULT_STREAM_TIME 0
66 #define DEFAULT_START_TIME 0
67 #define DEFAULT_INITIAL_TIMESTAMP GST_CLOCK_TIME_NONE
68 #define DEFAULT_RATE 1.0
69 #define DEFAULT_APPLIED_RATE 1.0
70
71 static void gst_push_file_src_set_property (GObject * object,
72 guint prop_id, const GValue * value, GParamSpec * pspec);
73 static void gst_push_file_src_get_property (GObject * object,
74 guint prop_id, GValue * value, GParamSpec * pspec);
75
76 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
77 GST_PAD_SRC,
78 GST_PAD_ALWAYS,
79 GST_STATIC_CAPS_ANY);
80
81 static void gst_push_file_src_uri_handler_init (gpointer g_iface,
82 gpointer iface_data);
83
84 #define gst_push_file_src_parent_class parent_class
85 G_DEFINE_TYPE_WITH_CODE (GstPushFileSrc, gst_push_file_src, GST_TYPE_BIN,
86 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
87 gst_push_file_src_uri_handler_init));
88 GST_ELEMENT_REGISTER_DEFINE (pushfilesrc, "pushfilesrc",
89 GST_RANK_NONE, gst_push_file_src_get_type ());
90
91 static void
gst_push_file_src_dispose(GObject * obj)92 gst_push_file_src_dispose (GObject * obj)
93 {
94 GstPushFileSrc *src = GST_PUSH_FILE_SRC (obj);
95
96 if (src->srcpad) {
97 gst_element_remove_pad (GST_ELEMENT (src), src->srcpad);
98 src->srcpad = NULL;
99 }
100 if (src->filesrc) {
101 gst_bin_remove (GST_BIN (src), src->filesrc);
102 src->filesrc = NULL;
103 }
104
105 G_OBJECT_CLASS (parent_class)->dispose (obj);
106 }
107
108 static void
gst_push_file_src_class_init(GstPushFileSrcClass * g_class)109 gst_push_file_src_class_init (GstPushFileSrcClass * g_class)
110 {
111 GObjectClass *gobject_class;
112 GstElementClass *element_class;
113
114 gobject_class = G_OBJECT_CLASS (g_class);
115 element_class = GST_ELEMENT_CLASS (g_class);
116
117 GST_DEBUG_CATEGORY_INIT (pushfilesrc_debug, "pushfilesrc", 0,
118 "pushfilesrc element");
119
120 gobject_class->dispose = gst_push_file_src_dispose;
121 gobject_class->set_property = gst_push_file_src_set_property;
122 gobject_class->get_property = gst_push_file_src_get_property;
123
124 g_object_class_install_property (gobject_class, PROP_LOCATION,
125 g_param_spec_string ("location", "File Location",
126 "Location of the file to read", NULL,
127 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
128 GST_PARAM_MUTABLE_READY));
129
130 g_object_class_install_property (gobject_class, PROP_TIME_SEGMENT,
131 g_param_spec_boolean ("time-segment", "Time Segment",
132 "Emit TIME SEGMENTS", DEFAULT_TIME_SEGMENT, G_PARAM_READWRITE));
133
134 g_object_class_install_property (gobject_class, PROP_STREAM_TIME,
135 g_param_spec_int64 ("stream-time", "Stream Time",
136 "Initial Stream Time (if time-segment TRUE)", 0, G_MAXINT64,
137 DEFAULT_STREAM_TIME, G_PARAM_READWRITE));
138
139 g_object_class_install_property (gobject_class, PROP_START_TIME,
140 g_param_spec_int64 ("start-time", "Start Time",
141 "Initial Start Time (if time-segment TRUE)", 0, G_MAXINT64,
142 DEFAULT_START_TIME, G_PARAM_READWRITE));
143
144 g_object_class_install_property (gobject_class, PROP_INITIAL_TIMESTAMP,
145 g_param_spec_uint64 ("initial-timestamp", "Initial Timestamp",
146 "Initial Buffer Timestamp (if time-segment TRUE)", 0, G_MAXUINT64,
147 DEFAULT_INITIAL_TIMESTAMP, G_PARAM_READWRITE));
148
149 g_object_class_install_property (gobject_class, PROP_RATE,
150 g_param_spec_double ("rate", "Rate", "Rate to use in TIME SEGMENT",
151 G_MINDOUBLE, G_MAXDOUBLE, DEFAULT_RATE, G_PARAM_READWRITE));
152
153 g_object_class_install_property (gobject_class, PROP_APPLIED_RATE,
154 g_param_spec_double ("applied-rate", "Applied Rate",
155 "Applied rate to use in TIME SEGMENT", G_MINDOUBLE, G_MAXDOUBLE,
156 DEFAULT_APPLIED_RATE, G_PARAM_READWRITE));
157
158 gst_element_class_add_static_pad_template (element_class, &srctemplate);
159
160 gst_element_class_set_static_metadata (element_class, "Push File Source",
161 "Testing",
162 "Implements pushfile:// URI-handler for push-based file access",
163 "Tim-Philipp Müller <tim centricular net>");
164 }
165
166 static void
gst_push_file_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)167 gst_push_file_src_set_property (GObject * object, guint prop_id,
168 const GValue * value, GParamSpec * pspec)
169 {
170 GstPushFileSrc *src = (GstPushFileSrc *) object;
171
172 switch (prop_id) {
173 case PROP_LOCATION:
174 g_object_set_property (G_OBJECT (src->filesrc), "location", value);
175 break;
176 case PROP_TIME_SEGMENT:
177 src->time_segment = g_value_get_boolean (value);
178 break;
179 case PROP_STREAM_TIME:
180 src->stream_time = g_value_get_int64 (value);
181 break;
182 case PROP_START_TIME:
183 src->start_time = g_value_get_int64 (value);
184 break;
185 case PROP_INITIAL_TIMESTAMP:
186 src->initial_timestamp = g_value_get_uint64 (value);
187 break;
188 case PROP_RATE:
189 src->rate = g_value_get_double (value);
190 break;
191 case PROP_APPLIED_RATE:
192 src->applied_rate = g_value_get_double (value);
193 break;
194 default:
195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196 break;
197 }
198 }
199
200 static void
gst_push_file_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)201 gst_push_file_src_get_property (GObject * object, guint prop_id, GValue * value,
202 GParamSpec * pspec)
203 {
204 GstPushFileSrc *src = (GstPushFileSrc *) object;
205
206 switch (prop_id) {
207 case PROP_LOCATION:
208 g_object_get_property (G_OBJECT (src->filesrc), "location", value);
209 break;
210 case PROP_TIME_SEGMENT:
211 g_value_set_boolean (value, src->time_segment);
212 break;
213 case PROP_STREAM_TIME:
214 g_value_set_int64 (value, src->stream_time);
215 break;
216 case PROP_START_TIME:
217 g_value_set_int64 (value, src->start_time);
218 break;
219 case PROP_INITIAL_TIMESTAMP:
220 g_value_set_uint64 (value, src->initial_timestamp);
221 break;
222 case PROP_RATE:
223 g_value_set_double (value, src->rate);
224 break;
225 case PROP_APPLIED_RATE:
226 g_value_set_double (value, src->applied_rate);
227 break;
228 default:
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 break;
231 }
232 }
233
234 static GstPadProbeReturn
gst_push_file_src_ghostpad_buffer_probe(GstPad * pad,GstPadProbeInfo * info,GstPushFileSrc * src)235 gst_push_file_src_ghostpad_buffer_probe (GstPad * pad, GstPadProbeInfo * info,
236 GstPushFileSrc * src)
237 {
238 GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
239
240 if (src->time_segment && !src->seen_first_buffer) {
241 GST_BUFFER_TIMESTAMP (buffer) = src->initial_timestamp;
242 src->seen_first_buffer = TRUE;
243 }
244 return GST_PAD_PROBE_OK;
245 }
246
247 static GstPadProbeReturn
gst_push_file_src_ghostpad_event_probe(GstPad * pad,GstPadProbeInfo * info,GstPushFileSrc * src)248 gst_push_file_src_ghostpad_event_probe (GstPad * pad, GstPadProbeInfo * info,
249 GstPushFileSrc * src)
250 {
251 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
252
253 switch (GST_EVENT_TYPE (event)) {
254 case GST_EVENT_SEGMENT:
255 {
256 if (src->time_segment) {
257 GstSegment segment;
258 GstEvent *replacement;
259 GST_DEBUG_OBJECT (src, "Replacing outgoing segment with TIME SEGMENT");
260 gst_segment_init (&segment, GST_FORMAT_TIME);
261 segment.start = src->start_time;
262 segment.time = src->stream_time;
263 segment.rate = src->rate;
264 segment.applied_rate = src->applied_rate;
265 replacement = gst_event_new_segment (&segment);
266 gst_event_unref (event);
267 GST_PAD_PROBE_INFO_DATA (info) = replacement;
268 }
269 }
270 default:
271 break;
272 }
273 return GST_PAD_PROBE_OK;
274 }
275
276 static gboolean
gst_push_file_src_ghostpad_event(GstPad * pad,GstObject * parent,GstEvent * event)277 gst_push_file_src_ghostpad_event (GstPad * pad, GstObject * parent,
278 GstEvent * event)
279 {
280 GstPushFileSrc *src = (GstPushFileSrc *) parent;
281 gboolean ret;
282
283 switch (GST_EVENT_TYPE (event)) {
284 case GST_EVENT_SEEK:
285 if (src->time_segment) {
286 /* When working in time we don't allow seeks */
287 GST_DEBUG_OBJECT (src, "Refusing seek event in TIME mode");
288 gst_event_unref (event);
289 ret = FALSE;
290 break;
291 }
292 /* PASSTHROUGH */
293 default:
294 ret = gst_pad_event_default (pad, parent, event);
295 break;
296 }
297
298 return ret;
299 }
300
301 static gboolean
gst_push_file_src_ghostpad_query(GstPad * pad,GstObject * parent,GstQuery * query)302 gst_push_file_src_ghostpad_query (GstPad * pad, GstObject * parent,
303 GstQuery * query)
304 {
305 GstPushFileSrc *src = (GstPushFileSrc *) parent;
306 gboolean res;
307
308 switch (GST_QUERY_TYPE (query)) {
309 case GST_QUERY_SCHEDULING:
310 /* When working in time we don't allow seeks */
311 if (src->time_segment)
312 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEQUENTIAL, 1, -1,
313 0);
314 else
315 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1,
316 0);
317 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
318 res = TRUE;
319 break;
320 default:
321 res = gst_pad_query_default (pad, parent, query);
322 break;
323 }
324 return res;
325 }
326
327 static void
gst_push_file_src_init(GstPushFileSrc * src)328 gst_push_file_src_init (GstPushFileSrc * src)
329 {
330 src->time_segment = DEFAULT_TIME_SEGMENT;
331 src->stream_time = DEFAULT_STREAM_TIME;
332 src->start_time = DEFAULT_START_TIME;
333 src->initial_timestamp = DEFAULT_INITIAL_TIMESTAMP;
334 src->rate = DEFAULT_RATE;
335 src->applied_rate = DEFAULT_APPLIED_RATE;
336 src->seen_first_buffer = FALSE;
337
338 src->filesrc = gst_element_factory_make ("filesrc", "real-filesrc");
339 if (src->filesrc) {
340 GstPad *pad;
341
342 gst_bin_add (GST_BIN (src), src->filesrc);
343 pad = gst_element_get_static_pad (src->filesrc, "src");
344 g_assert (pad != NULL);
345 src->srcpad = gst_ghost_pad_new ("src", pad);
346 /* FIXME^H^HCORE: try pushfile:///foo/bar.ext ! typefind ! fakesink without
347 * this and watch core bugginess (some pad stays in flushing state) */
348 gst_pad_set_query_function (src->srcpad,
349 GST_DEBUG_FUNCPTR (gst_push_file_src_ghostpad_query));
350 gst_pad_set_event_function (src->srcpad,
351 GST_DEBUG_FUNCPTR (gst_push_file_src_ghostpad_event));
352 /* Add outgoing event probe to replace segment and buffer timestamp */
353 gst_pad_add_probe (src->srcpad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
354 (GstPadProbeCallback) gst_push_file_src_ghostpad_event_probe,
355 src, NULL);
356 gst_pad_add_probe (src->srcpad, GST_PAD_PROBE_TYPE_BUFFER,
357 (GstPadProbeCallback) gst_push_file_src_ghostpad_buffer_probe,
358 src, NULL);
359 gst_element_add_pad (GST_ELEMENT (src), src->srcpad);
360 gst_object_unref (pad);
361 }
362 }
363
364 /*** GSTURIHANDLER INTERFACE *************************************************/
365
366 static GstURIType
gst_push_file_src_uri_get_type(GType type)367 gst_push_file_src_uri_get_type (GType type)
368 {
369 return GST_URI_SRC;
370 }
371
372 static const gchar *const *
gst_push_file_src_uri_get_protocols(GType type)373 gst_push_file_src_uri_get_protocols (GType type)
374 {
375 static const gchar *protocols[] = { "pushfile", NULL };
376
377 return protocols;
378 }
379
380 static gchar *
gst_push_file_src_uri_get_uri(GstURIHandler * handler)381 gst_push_file_src_uri_get_uri (GstURIHandler * handler)
382 {
383 GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
384 gchar *fileuri, *pushfileuri;
385
386 if (src->filesrc == NULL)
387 return NULL;
388
389 fileuri = gst_uri_handler_get_uri (GST_URI_HANDLER (src->filesrc));
390 if (fileuri == NULL)
391 return NULL;
392 pushfileuri = g_strconcat ("push", fileuri, NULL);
393 g_free (fileuri);
394
395 return pushfileuri;
396 }
397
398 static gboolean
gst_push_file_src_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)399 gst_push_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
400 GError ** error)
401 {
402 GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
403
404 if (src->filesrc == NULL) {
405 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
406 "Could not create file source element");
407 return FALSE;
408 }
409
410 /* skip 'push' bit */
411 return gst_uri_handler_set_uri (GST_URI_HANDLER (src->filesrc), uri + 4,
412 error);
413 }
414
415 static void
gst_push_file_src_uri_handler_init(gpointer g_iface,gpointer iface_data)416 gst_push_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
417 {
418 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
419
420 iface->get_type = gst_push_file_src_uri_get_type;
421 iface->get_protocols = gst_push_file_src_uri_get_protocols;
422 iface->get_uri = gst_push_file_src_uri_get_uri;
423 iface->set_uri = gst_push_file_src_uri_set_uri;
424 }
425