3fb916301bd6fb72b7e93a2c8539689e4da4720d
[platform/upstream/gstreamer.git] / gst / debug / gstpushfilesrc.c
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-pushfilesrc
22  * @short_description: Works like a filesrc, but only push-based (for debugging)
23  * @see_also: filesrc
24  *
25  * <refsect2>
26  * <para>
27  * This element is only useful for debugging purposes. It implements an URI
28  * protocol handler for the 'pushfile' protocol and behaves like a file source
29  * element that cannot be activated in pull-mode. This makes it very easy to
30  * debug demuxers or decoders that can operate both pull and push-based in
31  * connection with the playbin element (which creates a source based on the
32  * URI passed).
33  * </para>
34  * <title>Example launch line</title>
35  * <para>
36  * <programlisting>
37  * gst-launch -m playbin uri=pushfile:///home/you/some/file.ogg
38  * </programlisting>
39  * This plays back the given file using playbin, with the demuxer operating
40  * push-based.
41  * </para>
42  * </refsect2>
43  */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include "gstpushfilesrc.h"
50
51 #include <gst/gst.h>
52
53 GST_DEBUG_CATEGORY_STATIC (pushfilesrc_debug);
54 #define GST_CAT_DEFAULT pushfilesrc_debug
55
56 static const GstElementDetails pushfilesrc_details =
57 GST_ELEMENT_DETAILS ("Push File Source",
58     "Testing",
59     "Implements pushfile:// URI-handler for push-based file access",
60     "Tim-Philipp Müller <tim centricular net>");
61
62 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
63     GST_PAD_SRC,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS_ANY);
66
67 static void gst_push_file_src_uri_handler_init (gpointer g_iface,
68     gpointer iface_data);
69 static void gst_file_push_src_add_uri_handler (GType type);
70
71 GST_BOILERPLATE_FULL (GstPushFileSrc, gst_push_file_src, GstBin, GST_TYPE_BIN,
72     gst_file_push_src_add_uri_handler);
73
74 static void
75 gst_file_push_src_add_uri_handler (GType type)
76 {
77   static const GInterfaceInfo info = {
78     gst_push_file_src_uri_handler_init,
79     NULL,
80     NULL
81   };
82
83   g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &info);
84   GST_DEBUG_CATEGORY_INIT (pushfilesrc_debug, "pushfilesrc", 0,
85       "pushfilesrc element");
86 }
87
88 static void
89 gst_push_file_src_base_init (gpointer g_class)
90 {
91   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
92
93   gst_element_class_add_pad_template (element_class,
94       gst_static_pad_template_get (&srctemplate));
95
96   gst_element_class_set_details (element_class, &pushfilesrc_details);
97 }
98
99 static void
100 gst_push_file_src_dispose (GObject * obj)
101 {
102   GstPushFileSrc *src = GST_PUSH_FILE_SRC (obj);
103
104   if (src->srcpad) {
105     gst_element_remove_pad (GST_ELEMENT (src), src->srcpad);
106     src->srcpad = NULL;
107   }
108   if (src->filesrc) {
109     gst_bin_remove (GST_BIN (src), src->filesrc);
110     src->filesrc = NULL;
111   }
112
113   G_OBJECT_CLASS (parent_class)->dispose (obj);
114 }
115
116 static void
117 gst_push_file_src_class_init (GstPushFileSrcClass * g_class)
118 {
119   GObjectClass *gobject_class;
120
121   gobject_class = G_OBJECT_CLASS (g_class);
122
123   gobject_class->dispose = gst_push_file_src_dispose;
124 }
125
126 static gboolean
127 gst_push_file_src_ghostpad_checkgetrange (GstPad * pad)
128 {
129   return FALSE;
130 }
131
132 static void
133 gst_push_file_src_init (GstPushFileSrc * src, GstPushFileSrcClass * g_class)
134 {
135   src->filesrc = gst_element_factory_make ("filesrc", "real-filesrc");
136   if (src->filesrc) {
137     GstPad *pad;
138
139     gst_bin_add (GST_BIN (src), src->filesrc);
140     pad = gst_element_get_static_pad (src->filesrc, "src");
141     g_assert (pad != NULL);
142     src->srcpad = gst_ghost_pad_new ("src", pad);
143     /* FIXME^H^HCORE: try pushfile:///foo/bar.ext ! typefind ! fakesink without
144      * this and watch core bugginess (some pad stays in flushing state) */
145     gst_pad_set_checkgetrange_function (src->srcpad,
146         GST_DEBUG_FUNCPTR (gst_push_file_src_ghostpad_checkgetrange));
147     gst_element_add_pad (GST_ELEMENT (src), src->srcpad);
148     gst_object_unref (pad);
149   }
150 }
151
152 /*** GSTURIHANDLER INTERFACE *************************************************/
153
154 static GstURIType
155 gst_push_file_src_uri_get_type (void)
156 {
157   return GST_URI_SRC;
158 }
159 static gchar **
160 gst_push_file_src_uri_get_protocols (void)
161 {
162   static gchar *protocols[] = { "pushfile", NULL };
163
164   return protocols;
165 }
166 static const gchar *
167 gst_push_file_src_uri_get_uri (GstURIHandler * handler)
168 {
169   GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
170
171   if (src->filesrc == NULL)
172     return NULL;
173
174   return gst_uri_handler_get_uri (GST_URI_HANDLER (src->filesrc));
175 }
176
177 static gboolean
178 gst_push_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
179 {
180   GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
181
182   if (src->filesrc == NULL || !g_str_has_prefix (uri, "pushfile://"))
183     return FALSE;
184
185   /* skip 'push' bit */
186   return gst_uri_handler_set_uri (GST_URI_HANDLER (src->filesrc), uri + 4);
187 }
188
189 static void
190 gst_push_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
191 {
192   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
193
194   iface->get_type = gst_push_file_src_uri_get_type;
195   iface->get_protocols = gst_push_file_src_uri_get_protocols;
196   iface->get_uri = gst_push_file_src_uri_get_uri;
197   iface->set_uri = gst_push_file_src_uri_set_uri;
198 }
199
200 gboolean
201 gst_push_file_src_plugin_init (GstPlugin * plugin)
202 {
203   return gst_element_register (plugin, "pushfilesrc", GST_RANK_NONE,
204       GST_TYPE_PUSH_FILE_SRC);
205 }