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  * @see_also: filesrc
23  *
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
29  * URI passed).
30  *
31  * <refsect2>
32  * <title>Example launch line</title>
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  * </refsect2>
38  */
39 
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43 
44 #include "gstpushfilesrc.h"
45 
46 #include <gst/gst.h>
47 
48 GST_DEBUG_CATEGORY_STATIC (pushfilesrc_debug);
49 #define GST_CAT_DEFAULT pushfilesrc_debug
50 
51 enum
52 {
53   PROP_0,
54   PROP_LOCATION,
55   PROP_TIME_SEGMENT,
56   PROP_STREAM_TIME,
57   PROP_START_TIME,
58   PROP_INITIAL_TIMESTAMP,
59   PROP_RATE,
60   PROP_APPLIED_RATE
61 };
62 
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
69 
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);
74 
75 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
76     GST_PAD_SRC,
77     GST_PAD_ALWAYS,
78     GST_STATIC_CAPS_ANY);
79 
80 static void gst_push_file_src_uri_handler_init (gpointer g_iface,
81     gpointer iface_data);
82 
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));
87 
88 static void
gst_push_file_src_dispose(GObject * obj)89 gst_push_file_src_dispose (GObject * obj)
90 {
91   GstPushFileSrc *src = GST_PUSH_FILE_SRC (obj);
92 
93   if (src->srcpad) {
94     gst_element_remove_pad (GST_ELEMENT (src), src->srcpad);
95     src->srcpad = NULL;
96   }
97   if (src->filesrc) {
98     gst_bin_remove (GST_BIN (src), src->filesrc);
99     src->filesrc = NULL;
100   }
101 
102   G_OBJECT_CLASS (parent_class)->dispose (obj);
103 }
104 
105 static void
gst_push_file_src_class_init(GstPushFileSrcClass * g_class)106 gst_push_file_src_class_init (GstPushFileSrcClass * g_class)
107 {
108   GObjectClass *gobject_class;
109   GstElementClass *element_class;
110 
111   gobject_class = G_OBJECT_CLASS (g_class);
112   element_class = GST_ELEMENT_CLASS (g_class);
113 
114   GST_DEBUG_CATEGORY_INIT (pushfilesrc_debug, "pushfilesrc", 0,
115       "pushfilesrc element");
116 
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;
120 
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));
126 
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));
130 
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));
135 
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));
140 
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));
145 
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));
149 
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));
154 
155   gst_element_class_add_static_pad_template (element_class, &srctemplate);
156 
157   gst_element_class_set_static_metadata (element_class, "Push File Source",
158       "Testing",
159       "Implements pushfile:// URI-handler for push-based file access",
160       "Tim-Philipp Müller <tim centricular net>");
161 }
162 
163 static void
gst_push_file_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)164 gst_push_file_src_set_property (GObject * object, guint prop_id,
165     const GValue * value, GParamSpec * pspec)
166 {
167   GstPushFileSrc *src = (GstPushFileSrc *) object;
168 
169   switch (prop_id) {
170     case PROP_LOCATION:
171       g_object_set_property (G_OBJECT (src->filesrc), "location", value);
172       break;
173     case PROP_TIME_SEGMENT:
174       src->time_segment = g_value_get_boolean (value);
175       break;
176     case PROP_STREAM_TIME:
177       src->stream_time = g_value_get_int64 (value);
178       break;
179     case PROP_START_TIME:
180       src->start_time = g_value_get_int64 (value);
181       break;
182     case PROP_INITIAL_TIMESTAMP:
183       src->initial_timestamp = g_value_get_uint64 (value);
184       break;
185     case PROP_RATE:
186       src->rate = g_value_get_double (value);
187       break;
188     case PROP_APPLIED_RATE:
189       src->applied_rate = g_value_get_double (value);
190       break;
191     default:
192       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193       break;
194   }
195 }
196 
197 static void
gst_push_file_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)198 gst_push_file_src_get_property (GObject * object, guint prop_id, GValue * value,
199     GParamSpec * pspec)
200 {
201   GstPushFileSrc *src = (GstPushFileSrc *) object;
202 
203   switch (prop_id) {
204     case PROP_LOCATION:
205       g_object_get_property (G_OBJECT (src->filesrc), "location", value);
206       break;
207     case PROP_TIME_SEGMENT:
208       g_value_set_boolean (value, src->time_segment);
209       break;
210     case PROP_STREAM_TIME:
211       g_value_set_int64 (value, src->stream_time);
212       break;
213     case PROP_START_TIME:
214       g_value_set_int64 (value, src->start_time);
215       break;
216     case PROP_INITIAL_TIMESTAMP:
217       g_value_set_uint64 (value, src->initial_timestamp);
218       break;
219     case PROP_RATE:
220       g_value_set_double (value, src->rate);
221       break;
222     case PROP_APPLIED_RATE:
223       g_value_set_double (value, src->applied_rate);
224       break;
225     default:
226       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227       break;
228   }
229 }
230 
231 static GstPadProbeReturn
gst_push_file_src_ghostpad_buffer_probe(GstPad * pad,GstPadProbeInfo * info,GstPushFileSrc * src)232 gst_push_file_src_ghostpad_buffer_probe (GstPad * pad, GstPadProbeInfo * info,
233     GstPushFileSrc * src)
234 {
235   GstBuffer *buffer = GST_PAD_PROBE_INFO_BUFFER (info);
236 
237   if (src->time_segment && !src->seen_first_buffer) {
238     GST_BUFFER_TIMESTAMP (buffer) = src->initial_timestamp;
239     src->seen_first_buffer = TRUE;
240   }
241   return GST_PAD_PROBE_OK;
242 }
243 
244 static GstPadProbeReturn
gst_push_file_src_ghostpad_event_probe(GstPad * pad,GstPadProbeInfo * info,GstPushFileSrc * src)245 gst_push_file_src_ghostpad_event_probe (GstPad * pad, GstPadProbeInfo * info,
246     GstPushFileSrc * src)
247 {
248   GstEvent *event = GST_PAD_PROBE_INFO_EVENT (info);
249 
250   switch (GST_EVENT_TYPE (event)) {
251     case GST_EVENT_SEGMENT:
252     {
253       if (src->time_segment) {
254         GstSegment 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;
265       }
266     }
267     default:
268       break;
269   }
270   return GST_PAD_PROBE_OK;
271 }
272 
273 static gboolean
gst_push_file_src_ghostpad_event(GstPad * pad,GstObject * parent,GstEvent * event)274 gst_push_file_src_ghostpad_event (GstPad * pad, GstObject * parent,
275     GstEvent * event)
276 {
277   GstPushFileSrc *src = (GstPushFileSrc *) parent;
278   gboolean ret;
279 
280   switch (GST_EVENT_TYPE (event)) {
281     case GST_EVENT_SEEK:
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);
286         ret = FALSE;
287         break;
288       }
289       /* PASSTHROUGH */
290     default:
291       ret = gst_pad_event_default (pad, parent, event);
292       break;
293   }
294 
295   return ret;
296 }
297 
298 static gboolean
gst_push_file_src_ghostpad_query(GstPad * pad,GstObject * parent,GstQuery * query)299 gst_push_file_src_ghostpad_query (GstPad * pad, GstObject * parent,
300     GstQuery * query)
301 {
302   GstPushFileSrc *src = (GstPushFileSrc *) parent;
303   gboolean res;
304 
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,
310             0);
311       else
312         gst_query_set_scheduling (query, GST_SCHEDULING_FLAG_SEEKABLE, 1, -1,
313             0);
314       gst_query_add_scheduling_mode (query, GST_PAD_MODE_PUSH);
315       res = TRUE;
316       break;
317     default:
318       res = gst_pad_query_default (pad, parent, query);
319       break;
320   }
321   return res;
322 }
323 
324 static void
gst_push_file_src_init(GstPushFileSrc * src)325 gst_push_file_src_init (GstPushFileSrc * src)
326 {
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;
334 
335   src->filesrc = gst_element_factory_make ("filesrc", "real-filesrc");
336   if (src->filesrc) {
337     GstPad *pad;
338 
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,
352         src, NULL);
353     gst_pad_add_probe (src->srcpad, GST_PAD_PROBE_TYPE_BUFFER,
354         (GstPadProbeCallback) gst_push_file_src_ghostpad_buffer_probe,
355         src, NULL);
356     gst_element_add_pad (GST_ELEMENT (src), src->srcpad);
357     gst_object_unref (pad);
358   }
359 }
360 
361 /*** GSTURIHANDLER INTERFACE *************************************************/
362 
363 static GstURIType
gst_push_file_src_uri_get_type(GType type)364 gst_push_file_src_uri_get_type (GType type)
365 {
366   return GST_URI_SRC;
367 }
368 
369 static const gchar *const *
gst_push_file_src_uri_get_protocols(GType type)370 gst_push_file_src_uri_get_protocols (GType type)
371 {
372   static const gchar *protocols[] = { "pushfile", NULL };
373 
374   return protocols;
375 }
376 
377 static gchar *
gst_push_file_src_uri_get_uri(GstURIHandler * handler)378 gst_push_file_src_uri_get_uri (GstURIHandler * handler)
379 {
380   GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
381   gchar *fileuri, *pushfileuri;
382 
383   if (src->filesrc == NULL)
384     return NULL;
385 
386   fileuri = gst_uri_handler_get_uri (GST_URI_HANDLER (src->filesrc));
387   if (fileuri == NULL)
388     return NULL;
389   pushfileuri = g_strconcat ("push", fileuri, NULL);
390   g_free (fileuri);
391 
392   return pushfileuri;
393 }
394 
395 static gboolean
gst_push_file_src_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)396 gst_push_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
397     GError ** error)
398 {
399   GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
400 
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");
404     return FALSE;
405   }
406 
407   /* skip 'push' bit */
408   return gst_uri_handler_set_uri (GST_URI_HANDLER (src->filesrc), uri + 4,
409       error);
410 }
411 
412 static void
gst_push_file_src_uri_handler_init(gpointer g_iface,gpointer iface_data)413 gst_push_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
414 {
415   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
416 
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;
421 }
422