gst_input_stream.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*GstInputStream.c - Header 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. #include <jni.h>
  32. #include <jcl.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <gdk/gdk.h>
  36. #include <glib.h>
  37. #include "gst_peer.h"
  38. #include "gnu_javax_sound_sampled_gstreamer_io_GstInputStream.h"
  39. #include "gst_input_stream.h"
  40. /* for caching */
  41. static jmethodID readID = NULL;
  42. static jmethodID pointerConstructorID = NULL;
  43. static jmethodID availableID = NULL;
  44. static jfieldID streamID = NULL;
  45. static jfieldID pointerDataID = NULL;
  46. struct _GstInputStreamPrivate
  47. {
  48. JavaVM *vm;
  49. jclass readerClass;
  50. jclass pointerClass;
  51. jobject reader;
  52. };
  53. /* ************************************************************************** */
  54. static void init_pointer_IDs (JNIEnv* env);
  55. /* ************************************************************************** */
  56. /* JNI Methods */
  57. JNIEXPORT void JNICALL
  58. Java_gnu_javax_sound_sampled_gstreamer_io_GstInputStream_init_1id_1cache
  59. (JNIEnv *env, jclass clazz)
  60. {
  61. readID = (*env)->GetMethodID(env, clazz, "read", "([BII)I");
  62. availableID = (*env)->GetMethodID(env, clazz, "available", "()I");
  63. streamID = (*env)->GetFieldID(env, clazz, "gstInputStream",
  64. "Lgnu/classpath/Pointer;");
  65. init_pointer_IDs(env);
  66. }
  67. JNIEXPORT void JNICALL
  68. Java_gnu_javax_sound_sampled_gstreamer_io_GstInputStream_init_1instance
  69. (JNIEnv *env, jobject reader)
  70. {
  71. GstInputStream *istream = NULL;
  72. jclass localReader = NULL;
  73. jclass localPointer = NULL;
  74. jobject _pointer = NULL;
  75. istream = (GstInputStream *) JCL_malloc (env, sizeof (GstInputStream));
  76. if (istream == NULL)
  77. return;
  78. istream->priv = (GstInputStreamPrivate *)
  79. JCL_malloc (env, sizeof (GstInputStreamPrivate));
  80. if (istream->priv == NULL)
  81. {
  82. JCL_free (env, istream);
  83. return;
  84. }
  85. /* get a local references first */
  86. localReader = (*env)->GetObjectClass(env, reader);
  87. if (localReader == NULL)
  88. {
  89. JCL_free (env, istream->priv);
  90. JCL_free (env, istream);
  91. JCL_ThrowException (env, "java/lang/InternalError",
  92. "Class Initialization failed.");
  93. return;
  94. }
  95. #if SIZEOF_VOID_P == 8
  96. localPointer = JCL_FindClass (env, "gnu/classpath/Pointer64");
  97. #else
  98. # if SIZEOF_VOID_P == 4
  99. localPointer = JCL_FindClass (env, "gnu/classpath/Pointer32");
  100. # else
  101. # error "Pointer size is not supported."
  102. # endif /* SIZEOF_VOID_P == 4 */
  103. #endif /* SIZEOF_VOID_P == 8 */
  104. if (localReader == NULL || localPointer == NULL)
  105. {
  106. JCL_free (env, istream->priv);
  107. JCL_free (env, istream);
  108. JCL_ThrowException (env, "java/lang/InternalError",
  109. "Class Initialization failed.");
  110. return;
  111. }
  112. /* fill out our structure */
  113. istream->priv->readerClass = (*env)->NewGlobalRef(env, localReader);
  114. istream->priv->pointerClass = (*env)->NewGlobalRef(env, localPointer);
  115. (*env)->GetJavaVM(env, &istream->priv->vm);
  116. istream->priv->reader = (*env)->NewGlobalRef(env, reader);
  117. _pointer = (*env)->GetObjectField(env, reader, streamID);
  118. /* this should be always null */
  119. if (_pointer == NULL)
  120. {
  121. #if SIZEOF_VOID_P == 8
  122. _pointer = (*env)->NewObject(env, istream->priv->pointerClass,
  123. pointerConstructorID, (jlong) istream);
  124. #else
  125. _pointer = (*env)->NewObject(env, istream->priv->pointerClass,
  126. pointerConstructorID, (jint) istream);
  127. #endif
  128. }
  129. else
  130. {
  131. #if SIZEOF_VOID_P == 8
  132. (*env)->SetLongField(env, reader, streamID, (jlong) istream);
  133. #else
  134. (*env)->SetIntField(env, reader, streamID, (jint) istream);
  135. #endif
  136. }
  137. /* store back our pointer into the calling class */
  138. (*env)->SetObjectField(env, reader, streamID, _pointer);
  139. }
  140. /* exported library functions */
  141. void
  142. gst_input_stream_clean (GstInputStream *self)
  143. {
  144. JNIEnv *env = NULL;
  145. env = gst_get_jenv (self->priv->vm);
  146. (*env)->DeleteGlobalRef (env, self->priv->reader);
  147. (*env)->DeleteGlobalRef (env, self->priv->readerClass);
  148. (*env)->DeleteGlobalRef (env, self->priv->pointerClass);
  149. JCL_free (env, self->priv);
  150. JCL_free (env, self);
  151. }
  152. int
  153. gst_input_stream_available (GstInputStream *self)
  154. {
  155. JNIEnv *env = NULL;
  156. if (self == NULL || self->priv == NULL ||
  157. self->priv->vm == NULL || self->priv->reader == NULL)
  158. {
  159. return -1;
  160. }
  161. env = gst_get_jenv (self->priv->vm);
  162. if (env == NULL)
  163. {
  164. g_warning("GstInputStream::gst_input_stream_available " \
  165. "failed to get java env");
  166. return -1;
  167. }
  168. return (*env)->CallIntMethod (env, self->priv->reader, availableID);
  169. }
  170. int
  171. gst_input_stream_read (GstInputStream *self, int *data, int offset,
  172. int length)
  173. {
  174. JNIEnv *env = NULL;
  175. int ret = -1;
  176. jbyteArray buffer;
  177. jbyte *bytes = NULL;
  178. if (self == NULL || self->priv == NULL ||
  179. self->priv->vm == NULL || self->priv->reader == NULL)
  180. {
  181. return -1;
  182. }
  183. env = gst_get_jenv (self->priv->vm);
  184. if (env == NULL)
  185. {
  186. g_warning("GstInputStream::gst_input_stream_read failed to get java env");
  187. return -1;
  188. }
  189. buffer = (*env)->NewByteArray (env, length);
  190. if (buffer == NULL)
  191. {
  192. g_warning ("GstInputStream::gst_input_stream_read called, failed");
  193. return -1;
  194. }
  195. ret = (*env)->CallIntMethod (env, self->priv->reader, readID, buffer, 0,
  196. length);
  197. if (ret < 0)
  198. {
  199. (*env)->DeleteLocalRef(env, buffer);
  200. return ret;
  201. }
  202. bytes = (*env)->GetByteArrayElements (env, buffer, NULL);
  203. /* copy bytes and release */
  204. memcpy (data + offset, bytes, ret);
  205. (*env)->ReleaseByteArrayElements (env, buffer, bytes, 0);
  206. (*env)->DeleteLocalRef (env, buffer);
  207. return ret;
  208. }
  209. /* private functions */
  210. static void init_pointer_IDs (JNIEnv* env)
  211. {
  212. jclass pointerClass = NULL;
  213. #if SIZEOF_VOID_P == 8
  214. pointerClass = JCL_FindClass (env, "gnu/classpath/Pointer64");
  215. if (pointerClass != NULL)
  216. {
  217. pointerDataID = (*env)->GetFieldID (env, pointerClass, "data", "J");
  218. pointerConstructorID = (*env)->GetMethodID (env, pointerClass, "<init>",
  219. "(J)V");
  220. }
  221. #else
  222. # if SIZEOF_VOID_P == 4
  223. pointerClass = JCL_FindClass (env, "gnu/classpath/Pointer32");
  224. if (pointerClass != NULL)
  225. {
  226. pointerDataID = (*env)->GetFieldID(env, pointerClass, "data", "I");
  227. pointerConstructorID = (*env)->GetMethodID(env, pointerClass,
  228. "<init>", "(I)V");
  229. }
  230. # else
  231. # error "Pointer size is not supported."
  232. # endif /* SIZEOF_VOID_P == 4 */
  233. #endif /* SIZEOF_VOID_P == 8 */
  234. }