OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [native/] [jni/] [gstreamer-peer/] [gst_input_stream.c] - Blame information for rev 774

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.