add parent to query function
[platform/upstream/gst-plugins-good.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
59 #define gst_push_file_src_parent_class parent_class
60 G_DEFINE_TYPE_WITH_CODE (GstPushFileSrc, gst_push_file_src, GST_TYPE_BIN,
61     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
62         gst_push_file_src_uri_handler_init));
63
64 static void
65 gst_push_file_src_dispose (GObject * obj)
66 {
67   GstPushFileSrc *src = GST_PUSH_FILE_SRC (obj);
68
69   if (src->srcpad) {
70     gst_element_remove_pad (GST_ELEMENT (src), src->srcpad);
71     src->srcpad = NULL;
72   }
73   if (src->filesrc) {
74     gst_bin_remove (GST_BIN (src), src->filesrc);
75     src->filesrc = NULL;
76   }
77
78   G_OBJECT_CLASS (parent_class)->dispose (obj);
79 }
80
81 static void
82 gst_push_file_src_class_init (GstPushFileSrcClass * g_class)
83 {
84   GObjectClass *gobject_class;
85   GstElementClass *element_class;
86
87   gobject_class = G_OBJECT_CLASS (g_class);
88   element_class = GST_ELEMENT_CLASS (g_class);
89
90   GST_DEBUG_CATEGORY_INIT (pushfilesrc_debug, "pushfilesrc", 0,
91       "pushfilesrc element");
92
93   gobject_class->dispose = gst_push_file_src_dispose;
94
95   gst_element_class_add_pad_template (element_class,
96       gst_static_pad_template_get (&srctemplate));
97
98   gst_element_class_set_details_simple (element_class, "Push File Source",
99       "Testing",
100       "Implements pushfile:// URI-handler for push-based file access",
101       "Tim-Philipp Müller <tim centricular net>");
102 }
103
104 static gboolean
105 gst_push_file_src_ghostpad_query (GstPad * pad, GstObject * parent,
106     GstQuery * query)
107 {
108   gboolean res;
109
110   switch (GST_QUERY_TYPE (query)) {
111     case GST_QUERY_SCHEDULING:
112       gst_query_set_scheduling (query, FALSE, TRUE, FALSE, 1, -1, 1);
113       res = TRUE;
114       break;
115     default:
116       res = gst_proxy_pad_query_default (pad, parent, query);
117       break;
118   }
119   return res;
120 }
121
122 static void
123 gst_push_file_src_init (GstPushFileSrc * src)
124 {
125   src->filesrc = gst_element_factory_make ("filesrc", "real-filesrc");
126   if (src->filesrc) {
127     GstPad *pad;
128
129     gst_bin_add (GST_BIN (src), src->filesrc);
130     pad = gst_element_get_static_pad (src->filesrc, "src");
131     g_assert (pad != NULL);
132     src->srcpad = gst_ghost_pad_new ("src", pad);
133     /* FIXME^H^HCORE: try pushfile:///foo/bar.ext ! typefind ! fakesink without
134      * this and watch core bugginess (some pad stays in flushing state) */
135     gst_pad_set_query_function (src->srcpad,
136         GST_DEBUG_FUNCPTR (gst_push_file_src_ghostpad_query));
137     gst_element_add_pad (GST_ELEMENT (src), src->srcpad);
138     gst_object_unref (pad);
139   }
140 }
141
142 /*** GSTURIHANDLER INTERFACE *************************************************/
143
144 static GstURIType
145 gst_push_file_src_uri_get_type (GType type)
146 {
147   return GST_URI_SRC;
148 }
149
150 static const gchar *const *
151 gst_push_file_src_uri_get_protocols (GType type)
152 {
153   static const gchar *protocols[] = { "pushfile", NULL };
154
155   return protocols;
156 }
157
158 static gchar *
159 gst_push_file_src_uri_get_uri (GstURIHandler * handler)
160 {
161   GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
162   gchar *fileuri, *pushfileuri;
163
164   if (src->filesrc == NULL)
165     return NULL;
166
167   fileuri = gst_uri_handler_get_uri (GST_URI_HANDLER (src->filesrc));;
168   if (fileuri == NULL)
169     return NULL;
170   pushfileuri = g_strconcat ("push", fileuri, NULL);
171   g_free (fileuri);
172
173   return pushfileuri;
174 }
175
176 static gboolean
177 gst_push_file_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
178     GError ** error)
179 {
180   GstPushFileSrc *src = GST_PUSH_FILE_SRC (handler);
181
182   if (src->filesrc == NULL) {
183     g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
184         "Could not create file source element");
185     return FALSE;
186   }
187
188   /* skip 'push' bit */
189   return gst_uri_handler_set_uri (GST_URI_HANDLER (src->filesrc), uri + 4,
190       error);
191 }
192
193 static void
194 gst_push_file_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
195 {
196   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
197
198   iface->get_type = gst_push_file_src_uri_get_type;
199   iface->get_protocols = gst_push_file_src_uri_get_protocols;
200   iface->get_uri = gst_push_file_src_uri_get_uri;
201   iface->set_uri = gst_push_file_src_uri_set_uri;
202 }