1 /* GStreamer Push File Source
2 * Copyright (C) <2007> Tim-Philipp Müller <tim centricular net>
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.
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.
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.
21 * SECTION:element-pushfilesrc
24 * This element is only useful for debugging purposes. It implements an URI
25 * protocol handler for the 'pushfile' protocol and behaves like a file source
26 * element that cannot be activated in pull-mode. This makes it very easy to
27 * debug demuxers or decoders that can operate both pull and push-based in
28 * connection with the playbin element (which creates a source based on the
32 * <title>Example launch line</title>
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
44 #include "gstpushfilesrc.h"
48 GST_DEBUG_CATEGORY_STATIC (pushfilesrc_debug);
49 #define GST_CAT_DEFAULT pushfilesrc_debug
58 PROP_INITIAL_TIMESTAMP,
63 #define DEFAULT_TIME_SEGMENT FALSE
64 #define DEFAULT_STREAM_TIME 0
65 #define DEFAULT_START_TIME 0
66 #define DEFAULT_INITIAL_TIMESTAMP GST_CLOCK_TIME_NONE
67 #define DEFAULT_RATE 1.0
68 #define DEFAULT_APPLIED_RATE 1.0
70 static void gst_push_file_src_set_property (GObject * object,
71 guint prop_id, const GValue * value, GParamSpec * pspec);
72 static void gst_push_file_src_get_property (GObject * object,
73 guint prop_id, GValue * value, GParamSpec * pspec);
75 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
80 static void gst_push_file_src_uri_handler_init (gpointer g_iface,
83 #define gst_push_file_src_parent_class parent_class
84 G_DEFINE_TYPE_WITH_CODE (GstPushFileSrc, gst_push_file_src, GST_TYPE_BIN,
85 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
86 gst_push_file_src_uri_handler_init));
89 gst_push_file_src_dispose (GObject * obj)
91 GstPushFileSrc *src = GST_PUSH_FILE_SRC (obj);
94 gst_element_remove_pad (GST_ELEMENT (src), src->srcpad);
98 gst_bin_remove (GST_BIN (src), src->filesrc);
102 G_OBJECT_CLASS (parent_class)->dispose (obj);
106 gst_push_file_src_class_init (GstPushFileSrcClass * g_class)
108 GObjectClass *gobject_class;
109 GstElementClass *element_class;
111 gobject_class = G_OBJECT_CLASS (g_class);
112 element_class = GST_ELEMENT_CLASS (g_class);
114 GST_DEBUG_CATEGORY_INIT (pushfilesrc_debug, "pushfilesrc", 0,
115 "pushfilesrc element");
117 gobject_class->dispose = gst_push_file_src_dispose;
118 gobject_class->set_property = gst_push_file_src_set_property;
119 gobject_class->get_property = gst_push_file_src_get_property;
121 g_object_class_install_property (gobject_class, PROP_LOCATION,
122 g_param_spec_string ("location", "File Location",
123 "Location of the file to read", NULL,
124 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
125 GST_PARAM_MUTABLE_READY));
127 g_object_class_install_property (gobject_class, PROP_TIME_SEGMENT,
128 g_param_spec_boolean ("time-segment", "Time Segment",
129 "Emit TIME SEGMENTS", DEFAULT_TIME_SEGMENT, G_PARAM_READWRITE));
131 g_object_class_install_property (gobject_class, PROP_STREAM_TIME,
132 g_param_spec_int64 ("stream-time", "Stream Time",
133 "Initial Stream Time (if time-segment TRUE)", 0, G_MAXINT64,
134 DEFAULT_STREAM_TIME, G_PARAM_READWRITE));
136 g_object_class_install_property (gobject_class, PROP_START_TIME,
137 g_param_spec_int64 ("start-time", "Start Time",
138 "Initial Start Time (if time-segment TRUE)", 0, G_MAXINT64,
139 DEFAULT_START_TIME, G_PARAM_READWRITE));
141 g_object_class_install_property (gobject_class, PROP_INITIAL_TIMESTAMP,
142 g_param_spec_uint64 ("initial-timestamp", "Initial Timestamp",
143 "Initial Buffer Timestamp (if time-segment TRUE)", 0, G_MAXUINT64,
144 DEFAULT_INITIAL_TIMESTAMP, G_PARAM_READWRITE));
146 g_object_class_install_property (gobject_class, PROP_RATE,
147 g_param_spec_double ("rate", "Rate", "Rate to use in TIME SEGMENT",
148 G_MINDOUBLE, G_MAXDOUBLE, DEFAULT_RATE, G_PARAM_READWRITE));
150 g_object_class_install_property (gobject_class, PROP_APPLIED_RATE,
151 g_param_spec_double ("applied-rate", "Applied Rate",
152 "Applied rate to use in TIME SEGMENT", G_MINDOUBLE, G_MAXDOUBLE,
153 DEFAULT_APPLIED_RATE, G_PARAM_READWRITE));
155 gst_element_class_add_static_pad_template (element_class, &srctemplate);
157 gst_element_class_set_static_metadata (element_class, "Push File Source",
159 "Implements pushfile:// URI-handler for push-based file access",
160 "Tim-Philipp Müller <tim centricular net>");
164 gst_push_file_src_set_property (GObject * object, guint prop_id,
165 const GValue * value, GParamSpec * pspec)
167 GstPushFileSrc *src = (GstPushFileSrc *) object;
171 g_object_set_property (G_OBJECT (src->filesrc), "location", value);
173 case PROP_TIME_SEGMENT:
174 src->time_segment = g_value_get_boolean (value);
176 case PROP_STREAM_TIME:
177 src->stream_time = g_value_get_int64 (value);
179 case PROP_START_TIME:
180 src->start_time = g_value_get_int64 (value);
182 case PROP_INITIAL_TIMESTAMP:
183 src->initial_timestamp = g_value_get_uint64 (value);
186 src->rate = g_value_get_double (value);
188 case PROP_APPLIED_RATE:
189 src->applied_rate = g_value_get_double (value);
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198 gst_push_file_src_get_property (GObject * object, guint prop_id, GValue * value,
201 GstPushFileSrc *src = (GstPushFileSrc *) object;
205 g_object_get_property (G_OBJECT (src->filesrc), "location", value);
207 case PROP_TIME_SEGMENT:
208 g_value_set_boolean (value, src->time_segment);
210 case PROP_STREAM_TIME:
211 g_value_set_int64 (value, src->stream_time);
213 case PROP_START_TIME:
214 g_value_set_int64 (value, src->start_time);
216 case PROP_INITIAL_TIMESTAMP:
217 g_value_set_uint64 (value, src->initial_timestamp);
220 g_value_set_double (value, src->rate);
222 case PROP_APPLIED_RATE:
223 g_value_set_double (value, src->applied_rate);
226 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 static GstPadProbeReturn
232 gst_push_file_src_ghostpad_buffer_probe (GstPad * pad, GstPadProbeInfo * info,
233 GstPushFileSrc * src)
235 GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
237 if (src->time_segment && !src->seen_first_buffer) {
238 GST_BUFFER_TIMESTAMP (buffer) = src->initial_timestamp;
239 src->seen_first_buffer = TRUE;
241 return GST_PAD_PROBE_OK;
244 static GstPadProbeReturn
245 gst_push_file_src_ghostpad_event_probe (GstPad * pad, GstPadProbeInfo * info,
246 GstPushFileSrc * src)
248 GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
250 switch (GST_EVENT_TYPE (event)) {
251 case GST_EVENT_SEGMENT:
253 if (src->time_segment) {
255 GstEvent *replacement;
256 GST_DEBUG_OBJECT (src, "Replacing outgoing segment with TIME SEGMENT");
257 gst_segment_init (&segment, GST_FORMAT_TIME);
258 segment.start = src->start_time;
259 segment.time = src->stream_time;
260 segment.rate = src->rate;
261 segment.applied_rate = src->applied_rate;
262 replacement = gst_event_new_segment (&segment);
263 gst_event_unref (event);
264 GST_PAD_PROBE_INFO_DATA (info) = replacement;
270 return GST_PAD_PROBE_OK;
274 gst_push_file_src_ghostpad_event (GstPad * pad, GstObject * parent,
277 GstPushFileSrc *src = (GstPushFileSrc *) parent;
280 switch (GST_EVENT_TYPE (event)) {
282 if (src->time_segment) {
283 /* When working in time we don't allow seeks */
284 GST_DEBUG_OBJECT (src, "Refusing seek event in TIME mode");
285 gst_event_unref (event);
291 ret = gst_pad_event_default (pad, parent, event);
299 gst_push_file_src_ghostpad_query (GstPad * pad, GstObject * parent,
302 GstPushFileSrc *src = (GstPushFileSrc *) parent;
305 switch (GST_QUERY_TYPE (query)) {
306 case GST_QUERY_SCHEDULING:
307 /* When working in time we don't allow seeks */
308 if (src->time_segment)
309 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEQUENTIAL, 1, -1,
312 gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1,
314 gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
318 res = gst_pad_query_default (pad, parent, query);
325 gst_push_file_src_init (GstPushFileSrc * src)
327 src->time_segment = DEFAULT_TIME_SEGMENT;
328 src->stream_time = DEFAULT_STREAM_TIME;
329 src->start_time = DEFAULT_START_TIME;
330 src->initial_timestamp = DEFAULT_INITIAL_TIMESTAMP;
331 src->rate = DEFAULT_RATE;
332 src->applied_rate = DEFAULT_APPLIED_RATE;
333 src->seen_first_buffer = FALSE;
335 src->filesrc = gst_element_factory_make ("filesrc", "real-filesrc");
339 gst_bin_add (GST_BIN (src), src->filesrc);
340 pad = gst_element_get_static_pad (src->filesrc, "src");
341 g_assert (pad != NULL);
342 src->srcpad = gst_ghost_pad_new ("src", pad);
343 /* FIXME^H^HCORE: try pushfile:///foo/bar.ext ! typefind ! fakesink without
344 * this and watch core bugginess (some pad stays in flushing state) */
345 gst_pad_set_query_function (src->srcpad,
346 GST_DEBUG_FUNCPTR (gst_push_file_src_ghostpad_query));
347 gst_pad_set_event_function (src->srcpad,
348 GST_DEBUG_FUNCPTR (gst_push_file_src_ghostpad_event));
349 /* Add outgoing event probe to replace segment and buffer timestamp */
350 gst_pad_add_probe (src->srcpad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
351 (GstPadProbeCallback) gst_push_file_src_ghostpad_event_probe,
353 gst_pad_add_probe (src->srcpad, GST_PAD_PROBE_TYPE_BUFFER,
354 (GstPadProbeCallback) gst_push_file_src_ghostpad_buffer_probe,
356 gst_element_add_pad (GST_ELEMENT (src), src->srcpad);
357 gst_object_unref (pad);
361 /*** GSTURIHANDLER INTERFACE *************************************************/
364 gst_push_file_src_uri_get_type (GType type)
369 static const gchar *const *
370 gst_push_file_src_uri_get_protocols (GType type)
372 static const gchar *protocols[] = { "pushfile", NULL };
378 gst_push_file_src_uri_get_uri (GstURIHandler * handler)
380 GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
381 gchar *fileuri, *pushfileuri;
383 if (src->filesrc == NULL)
386 fileuri = gst_uri_handler_get_uri (GST_URI_HANDLER (src->filesrc));
389 pushfileuri = g_strconcat ("push", fileuri, NULL);
396 gst_push_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
399 GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
401 if (src->filesrc == NULL) {
402 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
403 "Could not create file source element");
407 /* skip 'push' bit */
408 return gst_uri_handler_set_uri (GST_URI_HANDLER (src->filesrc), uri + 4,
413 gst_push_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
415 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
417 iface->get_type = gst_push_file_src_uri_get_type;
418 iface->get_protocols = gst_push_file_src_uri_get_protocols;
419 iface->get_uri = gst_push_file_src_uri_get_uri;
420 iface->set_uri = gst_push_file_src_uri_set_uri;