Merged with mainline at revision 128810.
[official-gcc.git] / libjava / classpath / native / jni / gstreamer-peer / gstclasspathsrc.c
blobafce1f1d4c2697094a307f6da43f9bdb6dfa63ac
1 /*gstclasspathsrc.c - Class file for the GstClasspathPlugin
2 Copyright (C) 2007 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 #ifdef HAVE_CONFIG_H
39 # include <config.h>
40 #endif
43 * We don't really use version numbering here, we give it the same version
44 * number of classpath, so that gstreamer is happy.
45 * TODO: Maybe this should be moved in config.h instead?
47 #define CLASSPATH_GST_PLUGIN_VERSION PACKAGE_VERSION
49 #include <stdio.h>
50 #include <string.h>
51 #include <stdlib.h>
53 #include <gst/gst.h>
54 #include <gst/base/gstbasesrc.h>
55 #include <gst/base/gstpushsrc.h>
57 #include <glib.h>
58 #include <glib/gprintf.h>
60 #include <gdk/gdk.h>
62 #include "gstclasspathsrc.h"
63 #include "gstinputstream.h"
65 GST_DEBUG_CATEGORY_STATIC (gst_classpath_src_debug);
66 #define GST_CAT_DEFAULT gst_classpath_src_debug
68 enum
70 ARG_0,
71 ARG_INPUTSTREAM
74 static const GstElementDetails gst_classpath_src_details =
75 GST_ELEMENT_DETAILS ("ClasspathSrc",
76 "Source/Network",
77 "Read from a java input stream",
78 "Mario Torre <neugens@limasoftware.net>");
80 static GstStaticPadTemplate _template =
81 GST_STATIC_PAD_TEMPLATE ("src",
82 GST_PAD_SRC,
83 GST_PAD_ALWAYS,
84 GST_STATIC_CAPS_ANY);
86 /* ***** plugin init ***** */
88 static void
89 _do_init (GType filesrc_type __attribute__ ((unused)))
91 GST_DEBUG_CATEGORY_INIT (gst_classpath_src_debug, "classpathsrc",
92 0, "classpathsrc");
95 GST_BOILERPLATE_FULL (GstClasspathSrc, gst_classpath_src, GstPushSrc,
96 GST_TYPE_PUSH_SRC, _do_init);
98 static gboolean
99 plugin_init (GstPlugin *plugin)
101 return gst_element_register (plugin, "classpathsrc",
102 GST_RANK_NONE, GST_TYPE_CLASSPATH_SRC);
105 GST_PLUGIN_DEFINE_STATIC (GST_VERSION_MAJOR,
106 GST_VERSION_MINOR,
107 "classpathsrc",
108 "Java InputStream Reader",
109 plugin_init, CLASSPATH_GST_PLUGIN_VERSION,
110 GST_LICENSE_UNKNOWN,
111 "Classpath", "http://www.classpath.org/")
113 /* ***** public class methods ***** */
115 static void gst_classpath_src_set_property (GObject *object,
116 guint prop_id,
117 const GValue *value,
118 GParamSpec *pspec);
120 static void gst_classpath_src_get_property (GObject *object,
121 guint prop_id,
122 GValue *value,
123 GParamSpec *pspec);
125 static void gst_classpath_src_finalize (GObject *object);
127 static gboolean gst_classpath_src_start (GstBaseSrc *basesrc);
129 static gboolean gst_classpath_src_stop (GstBaseSrc *basesrc);
131 static GstFlowReturn gst_classpath_src_create (GstPushSrc *src,
132 GstBuffer **buffer);
134 /* ***** public class methods: end ***** */
136 static void
137 gst_classpath_src_base_init (gpointer gclass)
139 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (gclass);
141 gst_element_class_add_pad_template (gstelement_class,
142 gst_static_pad_template_get (&_template));
144 gst_element_class_set_details (gstelement_class, &gst_classpath_src_details);
147 static void
148 gst_classpath_src_class_init (GstClasspathSrcClass *klass)
150 GObjectClass *gobject_class;
151 GstElementClass *gstelement_class;
152 GstBaseSrcClass *gstbasesrc_class;
153 GstPushSrcClass *gstpushsrc_class;
155 GParamSpec *pspec;
157 gobject_class = G_OBJECT_CLASS (klass);
158 gstelement_class = GST_ELEMENT_CLASS (klass);
159 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
160 gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
162 /* getter and setters */
164 gobject_class->set_property = gst_classpath_src_set_property;
165 gobject_class->get_property = gst_classpath_src_get_property;
167 /* register properties */
168 pspec = g_param_spec_pointer (GST_CLASSPATH_SRC_ISTREAM,
169 "GstInputStream instance",
170 "GstInputStream instance",
171 G_PARAM_READWRITE);
172 g_object_class_install_property (gobject_class, ARG_INPUTSTREAM, pspec);
174 /* register callbacks */
175 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_classpath_src_finalize);
177 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_classpath_src_start);
178 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_classpath_src_stop);
180 gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_classpath_src_create);
183 /* ***** */
185 static void
186 gst_classpath_src_init (GstClasspathSrc *src,
187 GstClasspathSrcClass * g_class __attribute__ ((unused)))
189 src->istream = NULL;
190 src->read_position = 0;
193 static void
194 gst_classpath_src_finalize (GObject *object)
196 G_OBJECT_CLASS (parent_class)->finalize (object);
199 /* ************************************************************************** */
201 static void
202 gst_classpath_src_set_property (GObject *object,
203 guint prop_id,
204 const GValue *value,
205 GParamSpec *pspec)
207 GstClasspathSrc *src;
209 g_return_if_fail (GST_IS_CLASSPATH_SRC (object));
211 src = GST_CLASSPATH_SRC (object);
213 GST_OBJECT_LOCK (src);
214 switch (prop_id)
216 case ARG_INPUTSTREAM:
218 GST_STATE_LOCK (src);
220 GstState state;
221 state = GST_STATE (src);
223 if (state != GST_STATE_READY && state != GST_STATE_NULL)
225 GST_DEBUG_OBJECT (src, "setting location in wrong state");
226 GST_STATE_UNLOCK (src);
227 break;
230 GST_STATE_UNLOCK (src);
232 if (GST_IS_INPUT_STREAM (g_value_get_pointer (value)))
234 src->istream = g_value_get_pointer (value);
236 else
238 GST_INFO_OBJECT (src, "invalid instance of GstInputStream");
241 break;
243 default:
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245 break;
247 GST_OBJECT_UNLOCK (src);
250 static void
251 gst_classpath_src_get_property (GObject *object,
252 guint prop_id __attribute__ ((unused)),
253 GValue *value __attribute__ ((unused)),
254 GParamSpec *pspec __attribute__ ((unused)))
256 /* TODO */
257 G_OBJECT_CLASS (parent_class)->finalize (object);
260 /* ************************************************************************** */
262 static GstFlowReturn
263 gst_classpath_src_create (GstPushSrc *basesrc,
264 GstBuffer **buffer)
266 GstClasspathSrc *src;
267 int read = -1;
269 src = GST_CLASSPATH_SRC (basesrc);
271 /* create the buffer */
272 *buffer = gst_buffer_new_and_alloc (2048);
273 if (*buffer == NULL)
275 return GST_FLOW_ERROR;
278 GST_BUFFER_SIZE (*buffer) = 0;
280 GST_OBJECT_LOCK (src);
281 read = gst_input_stream_read (src->istream, (int *) GST_BUFFER_DATA (*buffer), 0,
282 2048);
283 GST_OBJECT_UNLOCK (src);
285 if (G_UNLIKELY (read < 0))
287 gst_buffer_unref (*buffer);
288 return GST_FLOW_UNEXPECTED;
291 GST_OBJECT_LOCK (src);
293 GST_BUFFER_SIZE (*buffer) = read;
294 GST_BUFFER_OFFSET (*buffer) = src->read_position;
295 GST_BUFFER_OFFSET_END (*buffer) = src->read_position + read;
297 src->read_position += read;
299 GST_OBJECT_UNLOCK (src);
301 gst_buffer_set_caps (*buffer, GST_PAD_CAPS (GST_BASE_SRC_PAD (src)));
303 return GST_FLOW_OK;
306 static gboolean
307 gst_classpath_src_start (GstBaseSrc *basesrc)
309 GstClasspathSrc *src;
311 src = GST_CLASSPATH_SRC (basesrc);
313 if (src->istream == NULL)
315 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
316 ("GstInputStream is still null. you need to pass a valid InputStream"));
318 return FALSE;
320 GST_OBJECT_LOCK (src);
321 src->read_position = 0;
322 GST_OBJECT_UNLOCK (src);
324 return TRUE;
327 static gboolean
328 gst_classpath_src_stop (GstBaseSrc *basesrc __attribute__ ((unused)))
330 /* nothing to do */
331 return TRUE;