gst_classpath_src.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*gstclasspathsrc.c - Class file for the GstClasspathPlugin
  2. Copyright (C) 2007 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. #ifdef HAVE_CONFIG_H
  32. # include <config.h>
  33. #endif
  34. /*
  35. * We don't really use version numbering here, we give it the same version
  36. * number of classpath, so that gstreamer is happy.
  37. * TODO: Maybe this should be moved in config.h instead?
  38. */
  39. #define CLASSPATH_GST_PLUGIN_VERSION PACKAGE_VERSION
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <stdlib.h>
  43. #include <gst/gst.h>
  44. #include <gst/base/gstbasesrc.h>
  45. #include <gst/base/gstpushsrc.h>
  46. #include <glib.h>
  47. #include <glib/gprintf.h>
  48. #include <gdk/gdk.h>
  49. #include "gst_classpath_src.h"
  50. #include "gst_input_stream.h"
  51. GST_DEBUG_CATEGORY_STATIC (gst_classpath_src_debug);
  52. #define GST_CAT_DEFAULT gst_classpath_src_debug
  53. enum
  54. {
  55. ARG_0,
  56. ARG_INPUTSTREAM
  57. };
  58. struct _GstClasspathSrcPrivate
  59. {
  60. GstInputStream *istream;
  61. GstCaps *caps;
  62. };
  63. static const GstElementDetails gst_classpath_src_details =
  64. GST_ELEMENT_DETAILS ("ClasspathSrc",
  65. "Source/Network",
  66. "Read from a java input stream",
  67. "Mario Torre <neugens@limasoftware.net>");
  68. static GstStaticPadTemplate _template =
  69. GST_STATIC_PAD_TEMPLATE ("src",
  70. GST_PAD_SRC,
  71. GST_PAD_ALWAYS,
  72. GST_STATIC_CAPS_ANY);
  73. /* ***** plugin init ***** */
  74. static void
  75. _do_init (GType filesrc_type __attribute__ ((unused)))
  76. {
  77. GST_DEBUG_CATEGORY_INIT (gst_classpath_src_debug, "classpathsrc",
  78. 0, "classpathsrc");
  79. }
  80. GST_BOILERPLATE_FULL (GstClasspathSrc, gst_classpath_src, GstPushSrc,
  81. GST_TYPE_PUSH_SRC, _do_init);
  82. static gboolean
  83. plugin_init (GstPlugin *plugin)
  84. {
  85. return gst_element_register (plugin, "classpathsrc",
  86. GST_RANK_NONE, GST_TYPE_CLASSPATH_SRC);
  87. }
  88. GST_PLUGIN_DEFINE_STATIC (GST_VERSION_MAJOR,
  89. GST_VERSION_MINOR,
  90. "classpathsrc",
  91. "Java InputStream Reader",
  92. plugin_init, CLASSPATH_GST_PLUGIN_VERSION,
  93. GST_LICENSE_UNKNOWN, /* GPL + Exception */
  94. "Classpath", "http://www.classpath.org/")
  95. /* ***** public class methods ***** */
  96. static void gst_classpath_src_set_property (GObject *object,
  97. guint prop_id,
  98. const GValue *value,
  99. GParamSpec *pspec);
  100. static void gst_classpath_src_get_property (GObject *object,
  101. guint prop_id,
  102. GValue *value,
  103. GParamSpec *pspec);
  104. static void gst_classpath_src_finalize (GObject *object);
  105. static GstCaps *gst_classpath_src_getcaps (GstBaseSrc *basesrc);
  106. static gboolean gst_classpath_src_start (GstBaseSrc *basesrc);
  107. static gboolean gst_classpath_src_stop (GstBaseSrc *basesrc);
  108. static GstFlowReturn gst_classpath_src_create (GstPushSrc *src,
  109. GstBuffer **buffer);
  110. static GstFlowReturn
  111. gst_classpath_src_create_stream (GstClasspathSrc *src, GstBuffer **buffer);
  112. static GstFlowReturn
  113. check_read (GstClasspathSrc *src, int read, int buffer_size,
  114. GstBuffer **buffer);
  115. /* ***** public class methods: end ***** */
  116. static void
  117. gst_classpath_src_base_init (gpointer gclass)
  118. {
  119. GstElementClass *gstelement_class = GST_ELEMENT_CLASS (gclass);
  120. gst_element_class_add_pad_template (gstelement_class,
  121. gst_static_pad_template_get (&_template));
  122. gst_element_class_set_details (gstelement_class, &gst_classpath_src_details);
  123. }
  124. static void
  125. gst_classpath_src_class_init (GstClasspathSrcClass *klass)
  126. {
  127. GObjectClass *gobject_class;
  128. GstBaseSrcClass *gstbasesrc_class;
  129. GstPushSrcClass *gstpushsrc_class;
  130. GParamSpec *pspec;
  131. gobject_class = G_OBJECT_CLASS (klass);
  132. gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
  133. gstpushsrc_class = GST_PUSH_SRC_CLASS (klass);
  134. g_type_class_add_private (klass, sizeof (GstClasspathSrcPrivate));
  135. /* getter and setters */
  136. gobject_class->set_property = gst_classpath_src_set_property;
  137. gobject_class->get_property = gst_classpath_src_get_property;
  138. /* register properties */
  139. pspec = g_param_spec_pointer (GST_CLASSPATH_SRC_ISTREAM,
  140. "GstInputStream instance",
  141. "GstInputStream instance",
  142. G_PARAM_READWRITE);
  143. g_object_class_install_property (gobject_class, ARG_INPUTSTREAM, pspec);
  144. /* register callbacks */
  145. gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_classpath_src_finalize);
  146. gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_classpath_src_getcaps);
  147. gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_classpath_src_start);
  148. gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_classpath_src_stop);
  149. gstpushsrc_class->create = GST_DEBUG_FUNCPTR (gst_classpath_src_create);
  150. }
  151. /* ***** */
  152. static void
  153. gst_classpath_src_init (GstClasspathSrc *src,
  154. GstClasspathSrcClass * g_class __attribute__ ((unused)))
  155. {
  156. src->priv = G_TYPE_INSTANCE_GET_PRIVATE (src, GST_TYPE_CLASSPATH_SRC,
  157. GstClasspathSrcPrivate);
  158. src->priv->istream = NULL;
  159. src->priv->caps = NULL;
  160. }
  161. static void
  162. gst_classpath_src_finalize (GObject *object)
  163. {
  164. G_OBJECT_CLASS (parent_class)->finalize (object);
  165. }
  166. /* ************************************************************************** */
  167. static void
  168. gst_classpath_src_set_property (GObject *object,
  169. guint prop_id,
  170. const GValue *value,
  171. GParamSpec *pspec)
  172. {
  173. GstClasspathSrc *src;
  174. g_return_if_fail (GST_IS_CLASSPATH_SRC (object));
  175. src = GST_CLASSPATH_SRC (object);
  176. GST_OBJECT_LOCK (src);
  177. switch (prop_id)
  178. {
  179. case ARG_INPUTSTREAM:
  180. {
  181. GST_STATE_LOCK (src);
  182. {
  183. GstState state;
  184. state = GST_STATE (src);
  185. if (state != GST_STATE_READY && state != GST_STATE_NULL)
  186. {
  187. GST_DEBUG_OBJECT (src, "setting reader in wrong state");
  188. GST_STATE_UNLOCK (src);
  189. break;
  190. }
  191. }
  192. GST_STATE_UNLOCK (src);
  193. /* FIXME: check if this is a valid instance of GstInputStream */
  194. src->priv->istream = g_value_get_pointer (value);
  195. }
  196. break;
  197. default:
  198. G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
  199. break;
  200. }
  201. GST_OBJECT_UNLOCK (src);
  202. }
  203. static void
  204. gst_classpath_src_get_property (GObject *object,
  205. guint prop_id __attribute__ ((unused)),
  206. GValue *value __attribute__ ((unused)),
  207. GParamSpec *pspec __attribute__ ((unused)))
  208. {
  209. /* TODO */
  210. }
  211. /* ************************************************************************** */
  212. static GstCaps *gst_classpath_src_getcaps (GstBaseSrc *basesrc)
  213. {
  214. GstClasspathSrc *src;
  215. GstCaps *caps = NULL;
  216. src = GST_CLASSPATH_SRC (basesrc);
  217. if (src->priv->caps)
  218. caps = gst_caps_copy (src->priv->caps);
  219. else
  220. caps = gst_caps_new_any ();
  221. GST_DEBUG_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
  222. g_assert (GST_IS_CAPS (caps));
  223. return caps;
  224. }
  225. static GstFlowReturn
  226. gst_classpath_src_create_stream (GstClasspathSrc *src, GstBuffer **buffer)
  227. {
  228. int buffer_size = 2048;
  229. int read = -1;
  230. buffer_size = gst_input_stream_available (src->priv->istream);
  231. if (buffer_size < 0)
  232. return GST_FLOW_ERROR;
  233. else if (buffer_size == 0)
  234. return GST_FLOW_WRONG_STATE;
  235. *buffer = gst_buffer_new_and_alloc (buffer_size);
  236. if (*buffer == NULL)
  237. {
  238. return GST_FLOW_ERROR;
  239. }
  240. read = gst_input_stream_read (src->priv->istream,
  241. (int *) GST_BUFFER_DATA (*buffer),
  242. 0,
  243. buffer_size);
  244. return check_read (src, read, buffer_size, buffer);
  245. }
  246. GstFlowReturn
  247. check_read (GstClasspathSrc *src, int read, int buffer_size, GstBuffer **buffer)
  248. {
  249. if (G_UNLIKELY (read < 0))
  250. {
  251. g_warning("GST_FLOW_UNEXPECTED (read < 0)");
  252. gst_buffer_unref (*buffer);
  253. *buffer = NULL;
  254. return GST_FLOW_ERROR;
  255. }
  256. else if (G_UNLIKELY (read == 0))
  257. {
  258. g_warning("GST_FLOW_WRONG_STATE (read == 0)");
  259. gst_buffer_unref (*buffer);
  260. *buffer = NULL;
  261. return GST_FLOW_WRONG_STATE;
  262. }
  263. else if (G_UNLIKELY (read < buffer_size))
  264. {
  265. g_warning("shorter read");
  266. gst_buffer_unref (*buffer);
  267. *buffer = NULL;
  268. return GST_FLOW_ERROR;
  269. }
  270. GST_BUFFER_SIZE (*buffer) = read;
  271. gst_buffer_set_caps (*buffer, src->priv->caps);
  272. return GST_FLOW_OK;
  273. }
  274. static GstFlowReturn
  275. gst_classpath_src_create (GstPushSrc *basesrc, GstBuffer **buffer)
  276. {
  277. GstClasspathSrc *src = NULL;
  278. GstFlowReturn ret = GST_FLOW_OK;
  279. src = GST_CLASSPATH_SRC (basesrc);
  280. /* create the buffer */
  281. ret = gst_classpath_src_create_stream (src, buffer);
  282. return ret;
  283. }
  284. static gboolean
  285. gst_classpath_src_start (GstBaseSrc *basesrc)
  286. {
  287. GstClasspathSrc *src;
  288. src = GST_CLASSPATH_SRC (basesrc);
  289. if (src->priv->istream == NULL)
  290. {
  291. g_warning("GstInputStream is still null. You need to " \
  292. "pass a valid InputStream object");
  293. GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
  294. ("GstInputStream is still null. You need to " \
  295. "pass a valid InputStream"));
  296. return FALSE;
  297. }
  298. return TRUE;
  299. }
  300. static gboolean
  301. gst_classpath_src_stop (GstBaseSrc *basesrc)
  302. {
  303. GstClasspathSrc *src;
  304. src = GST_CLASSPATH_SRC (basesrc);
  305. /* clean the stream */
  306. if (src->priv->istream != NULL)
  307. gst_input_stream_clean (src->priv->istream);
  308. if (src->priv->caps) {
  309. gst_caps_unref (src->priv->caps);
  310. src->priv->caps = NULL;
  311. }
  312. return TRUE;
  313. }