ac4f4badf878b70e2e20e94fe9a2b18956c87f12
[framework/multimedia/gst-plugins-good0.10.git] / gst / debugutils / 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  * @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 -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 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
52     GST_PAD_SRC,
53     GST_PAD_ALWAYS,
54     GST_STATIC_CAPS_ANY);
55
56 static void gst_push_file_src_uri_handler_init (gpointer g_iface,
57     gpointer iface_data);
58 static void gst_file_push_src_add_uri_handler (GType type);
59
60 GST_BOILERPLATE_FULL (GstPushFileSrc, gst_push_file_src, GstBin, GST_TYPE_BIN,
61     gst_file_push_src_add_uri_handler);
62
63 static void
64 gst_file_push_src_add_uri_handler (GType type)
65 {
66   static const GInterfaceInfo info = {
67     gst_push_file_src_uri_handler_init,
68     NULL,
69     NULL
70   };
71
72   g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &info);
73   GST_DEBUG_CATEGORY_INIT (pushfilesrc_debug, "pushfilesrc", 0,
74       "pushfilesrc element");
75 }
76
77 static void
78 gst_push_file_src_base_init (gpointer g_class)
79 {
80   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
81
82   gst_element_class_add_pad_template (element_class,
83       gst_static_pad_template_get (&srctemplate));
84
85   gst_element_class_set_details_simple (element_class, "Push File Source",
86       "Testing",
87       "Implements pushfile:// URI-handler for push-based file access",
88       "Tim-Philipp Müller <tim centricular net>");
89 }
90
91 static void
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
109 gst_push_file_src_class_init (GstPushFileSrcClass * g_class)
110 {
111   GObjectClass *gobject_class;
112
113   gobject_class = G_OBJECT_CLASS (g_class);
114
115   gobject_class->dispose = gst_push_file_src_dispose;
116 }
117
118 static gboolean
119 gst_push_file_src_ghostpad_checkgetrange (GstPad * pad)
120 {
121   return FALSE;
122 }
123
124 static void
125 gst_push_file_src_init (GstPushFileSrc * src, GstPushFileSrcClass * g_class)
126 {
127   src->filesrc = gst_element_factory_make ("filesrc", "real-filesrc");
128   if (src->filesrc) {
129     GstPad *pad;
130
131     gst_bin_add (GST_BIN (src), src->filesrc);
132     pad = gst_element_get_static_pad (src->filesrc, "src");
133     g_assert (pad != NULL);
134     src->srcpad = gst_ghost_pad_new ("src", pad);
135     /* FIXME^H^HCORE: try pushfile:///foo/bar.ext ! typefind ! fakesink without
136      * this and watch core bugginess (some pad stays in flushing state) */
137     gst_pad_set_checkgetrange_function (src->srcpad,
138         GST_DEBUG_FUNCPTR (gst_push_file_src_ghostpad_checkgetrange));
139     gst_element_add_pad (GST_ELEMENT (src), src->srcpad);
140     gst_object_unref (pad);
141   }
142 }
143
144 /*** GSTURIHANDLER INTERFACE *************************************************/
145
146 static GstURIType
147 gst_push_file_src_uri_get_type (void)
148 {
149   return GST_URI_SRC;
150 }
151
152 static gchar **
153 gst_push_file_src_uri_get_protocols (void)
154 {
155   static gchar *protocols[] = { (char *) "pushfile", NULL };
156
157   return protocols;
158 }
159
160 static const gchar *
161 gst_push_file_src_uri_get_uri (GstURIHandler * handler)
162 {
163   GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
164
165   if (src->filesrc == NULL)
166     return NULL;
167
168   return gst_uri_handler_get_uri (GST_URI_HANDLER (src->filesrc));
169 }
170
171 static gboolean
172 gst_push_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
173 {
174   GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
175
176   if (src->filesrc == NULL || !g_str_has_prefix (uri, "pushfile://"))
177     return FALSE;
178
179   /* skip 'push' bit */
180   return gst_uri_handler_set_uri (GST_URI_HANDLER (src->filesrc), uri + 4);
181 }
182
183 static void
184 gst_push_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
185 {
186   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
187
188   iface->get_type = gst_push_file_src_uri_get_type;
189   iface->get_protocols = gst_push_file_src_uri_get_protocols;
190   iface->get_uri = gst_push_file_src_uri_get_uri;
191   iface->set_uri = gst_push_file_src_uri_set_uri;
192 }