Tizen 2.1 base
[profile/ivi/gst-plugins-ugly0.10.git] / gst / realmedia / pnmsrc.c
1 /* GStreamer
2  * Copyright (C) <2009> Wim Taymans <wim.taymans@gmail.com>
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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <string.h>
25
26 #include "pnmsrc.h"
27
28 GST_DEBUG_CATEGORY_STATIC (pnmsrc_debug);
29 #define GST_CAT_DEFAULT pnmsrc_debug
30
31 /* PNMSrc signals and args */
32 enum
33 {
34   /* FILL ME */
35   LAST_SIGNAL
36 };
37
38 #define DEFAULT_LOCATION        NULL
39
40 enum
41 {
42   PROP_0,
43   PROP_LOCATION,
44   PROP_LAST
45 };
46
47 static GstStaticPadTemplate gst_pnm_src_template =
48 GST_STATIC_PAD_TEMPLATE ("src",
49     GST_PAD_SRC,
50     GST_PAD_ALWAYS,
51     GST_STATIC_CAPS ("application/vnd.rn-realmedia")
52     );
53
54 static GstFlowReturn gst_pnm_src_create (GstPushSrc * psrc, GstBuffer ** buf);
55
56 static void gst_pnm_src_uri_handler_init (gpointer g_iface,
57     gpointer iface_data);
58
59 static void
60 _do_init (GType pnmsrc_type)
61 {
62   static const GInterfaceInfo urihandler_info = {
63     gst_pnm_src_uri_handler_init,
64     NULL,
65     NULL
66   };
67
68   g_type_add_interface_static (pnmsrc_type, GST_TYPE_URI_HANDLER,
69       &urihandler_info);
70 }
71
72 GST_BOILERPLATE_FULL (GstPNMSrc, gst_pnm_src, GstPushSrc, GST_TYPE_PUSH_SRC,
73     _do_init);
74
75 static void gst_pnm_src_finalize (GObject * object);
76
77 static void gst_pnm_src_set_property (GObject * object, guint prop_id,
78     const GValue * value, GParamSpec * pspec);
79 static void gst_pnm_src_get_property (GObject * object, guint prop_id,
80     GValue * value, GParamSpec * pspec);
81
82
83 static void
84 gst_pnm_src_base_init (gpointer klass)
85 {
86   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
87
88   gst_element_class_add_static_pad_template (element_class,
89       &gst_pnm_src_template);
90
91   gst_element_class_set_details_simple (element_class, "PNM packet receiver",
92       "Source/Network",
93       "Receive data over the network via PNM",
94       "Wim Taymans <wim.taymans@gmail.com>");
95
96   GST_DEBUG_CATEGORY_INIT (pnmsrc_debug, "pnmsrc",
97       0, "Source for the pnm:// uri");
98 }
99
100 static void
101 gst_pnm_src_class_init (GstPNMSrcClass * klass)
102 {
103   GObjectClass *gobject_class;
104   GstPushSrcClass *gstpushsrc_class;
105
106   gobject_class = (GObjectClass *) klass;
107   gstpushsrc_class = (GstPushSrcClass *) klass;
108
109   parent_class = g_type_class_peek_parent (klass);
110
111   gobject_class->set_property = gst_pnm_src_set_property;
112   gobject_class->get_property = gst_pnm_src_get_property;
113
114   gobject_class->finalize = gst_pnm_src_finalize;
115
116   g_object_class_install_property (gobject_class, PROP_LOCATION,
117       g_param_spec_string ("location", "PNM Location",
118           "Location of the PNM url to read",
119           DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
120
121   gstpushsrc_class->create = gst_pnm_src_create;
122 }
123
124 static void
125 gst_pnm_src_init (GstPNMSrc * pnmsrc, GstPNMSrcClass * klass)
126 {
127   pnmsrc->location = g_strdup (DEFAULT_LOCATION);
128 }
129
130 gboolean
131 gst_pnm_src_plugin_init (GstPlugin * plugin)
132 {
133   return gst_element_register (plugin, "pnmsrc",
134       GST_RANK_MARGINAL, GST_TYPE_PNM_SRC);
135 }
136
137 static void
138 gst_pnm_src_finalize (GObject * object)
139 {
140   GstPNMSrc *pnmsrc;
141
142   pnmsrc = GST_PNM_SRC (object);
143
144   g_free (pnmsrc->location);
145
146   G_OBJECT_CLASS (parent_class)->finalize (object);
147 }
148
149 static void
150 gst_pnm_src_set_property (GObject * object, guint prop_id,
151     const GValue * value, GParamSpec * pspec)
152 {
153   GstPNMSrc *src;
154
155   src = GST_PNM_SRC (object);
156
157   switch (prop_id) {
158     case PROP_LOCATION:
159       g_free (src->location);
160       src->location = g_value_dup_string (value);
161       break;
162     default:
163       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
164       break;
165   }
166 }
167
168 static void
169 gst_pnm_src_get_property (GObject * object, guint prop_id,
170     GValue * value, GParamSpec * pspec)
171 {
172   GstPNMSrc *src;
173
174   src = GST_PNM_SRC (object);
175
176   switch (prop_id) {
177     case PROP_LOCATION:
178       g_value_set_string (value, src->location);
179       break;
180     default:
181       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182       break;
183   }
184 }
185
186 static GstFlowReturn
187 gst_pnm_src_create (GstPushSrc * psrc, GstBuffer ** buf)
188 {
189   GstPNMSrc *src;
190   GstMessage *m;
191   gchar *url;
192
193   src = GST_PNM_SRC (psrc);
194
195   if (src->location == NULL)
196     return GST_FLOW_ERROR;
197   url = g_strdup_printf ("rtsp%s", &src->location[3]);
198
199   /* the only thing we do is redirect to an RTSP url */
200   m = gst_message_new_element (GST_OBJECT_CAST (src),
201       gst_structure_new ("redirect", "new-location", G_TYPE_STRING, url, NULL));
202   g_free (url);
203
204   gst_element_post_message (GST_ELEMENT_CAST (src), m);
205
206
207   return GST_FLOW_UNEXPECTED;
208 }
209
210 /*** GSTURIHANDLER INTERFACE *************************************************/
211
212 static GstURIType
213 gst_pnm_src_uri_get_type (void)
214 {
215   return GST_URI_SRC;
216 }
217
218 static gchar **
219 gst_pnm_src_uri_get_protocols (void)
220 {
221   static gchar *protocols[] = { (gchar *) "pnm", NULL };
222
223   return protocols;
224 }
225
226 static const gchar *
227 gst_pnm_src_uri_get_uri (GstURIHandler * handler)
228 {
229   GstPNMSrc *src = GST_PNM_SRC (handler);
230
231   return src->location;
232 }
233
234 static gboolean
235 gst_pnm_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
236 {
237   GstPNMSrc *src = GST_PNM_SRC (handler);
238
239   if (!g_str_has_prefix (uri, "pnm://"))
240     return FALSE;
241
242   g_free (src->location);
243   src->location = g_strdup (uri);
244
245   return TRUE;
246 }
247
248 static void
249 gst_pnm_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
250 {
251   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
252
253   iface->get_type = gst_pnm_src_uri_get_type;
254   iface->get_protocols = gst_pnm_src_uri_get_protocols;
255   iface->get_uri = gst_pnm_src_uri_get_uri;
256   iface->set_uri = gst_pnm_src_uri_set_uri;
257 }