]> git.sesse.net Git - vlc/blob - bindings/java/vlc-glue-jni.cc
Java bindings by Filippo Carone.
[vlc] / bindings / java / vlc-glue-jni.cc
1 /*****************************************************************************
2  * JVLC.java: JNI interface for vlc Java Bindings
3  *****************************************************************************
4  * Copyright (C) 1998-2005 the VideoLAN team
5  *
6  * Authors: Filippo Carone <filippo@carone.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *****************************************************************************/
22
23 /* These are a must*/
24 #include <jni.h>
25 #include <vlc/vlc.h>
26
27 #include <stdio.h>  // for printf
28 #include <stdlib.h> // for calloc
29 #include <string.h> // for strcmp
30
31
32 /* JVLC internal imports, generated by gcjh */
33 #include "org_videolan_jvlc_JVLC.h"
34 #include "org_videolan_jvlc_JVLCVariable.h"
35 #include "org_videolan_jvlc_JVLCIntVariable.h"
36 #include "org_videolan_jvlc_JVLCBoolVariable.h"
37 #include "org_videolan_jvlc_JVLCFloatVariable.h"
38 #include "org_videolan_jvlc_JVLCStringVariable.h"
39 #include "org_videolan_jvlc_JVLCTimeVariable.h"
40 #include "org_videolan_jvlc_JVLCVarVariable.h"
41 #include "org_videolan_jvlc_JVLCVarValue.h"
42
43 int getClassID (JNIEnv *env, jobject obj);
44
45 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_create (JNIEnv *env, jobject obj) {
46   return VLC_Create();
47 }
48
49 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_init (JNIEnv *env, jobject obj, jobjectArray args) {
50
51   /* build the argv vector */
52   const char **argv;
53
54   /* count the arguments */
55   //  int argc = (int) (*env)->GetArrayLength(env, args)
56   int argc = (int) env->GetArrayLength((jarray) args);
57
58   /* convert java strings to c strings
59      how many pointers do we need? */
60   argv = (const char **) malloc(argc * sizeof(char*));
61
62   /* now fill the argv* */
63   for (int i = 0; i < argc; i++) {
64     argv[i] = env->GetStringUTFChars((jstring) env->GetObjectArrayElement(args, i),
65                                      0
66                                     );
67   }
68  
69   /* execute the library call */ 
70   int res = VLC_Init(getClassID(env,obj), argc, (char **) argv);
71   
72   free(argv);
73
74   return res;
75 }
76
77 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_addInterface (JNIEnv *env, jobject obj, jstring moduleName, jboolean blocking, jboolean startPlay) {
78   const char *psz_module = NULL;
79   int i_blocking, i_startPlay;
80
81   if (moduleName != NULL) {
82     psz_module = env->GetStringUTFChars(moduleName, 0);
83   }
84
85   if (blocking == true)  i_blocking  = 1;
86   else                   i_blocking  = 0;
87
88   if (startPlay == true) i_startPlay = 1;
89   else                   i_startPlay = 0;
90
91   int addIntf_res = VLC_AddIntf(getClassID(env,obj), psz_module, i_blocking, i_startPlay);
92   
93   if (psz_module != NULL) {
94     env->ReleaseStringUTFChars(moduleName, psz_module);
95   }
96   return addIntf_res;
97 }
98
99 JNIEXPORT jstring JNICALL Java_org_videolan_jvlc_JVLC_getVersion (JNIEnv *env, jobject obj) {
100   return env->NewStringUTF(VLC_Version());
101 }
102
103 JNIEXPORT jstring JNICALL Java_org_videolan_jvlc_JVLC_getError (JNIEnv *env, jobject obj, jint errorCode) {
104   return env->NewStringUTF(VLC_Error(errorCode));
105 }
106
107 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_die (JNIEnv *env, jobject obj) {
108   return VLC_Die(getClassID(env, obj));
109 }
110
111 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_cleanUp (JNIEnv *env, jobject obj) {
112   return VLC_CleanUp(getClassID(env, obj));
113 }
114
115 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_setVariable (JNIEnv *env, jobject obj, jobject jvlcVariable) {
116   vlc_value_t value;
117   char*       psz_var = NULL;
118   jstring     varName;
119   jstring     stringValue = NULL;
120
121   if (jvlcVariable != NULL) {
122     /* get the name of jvlcVariable class */
123     jclass    variableClass = env->GetObjectClass(jvlcVariable);
124     jclass    varSuperClass = env->GetSuperclass (variableClass);
125     jmethodID getNameMethod = env->GetMethodID   (varSuperClass, "getName", "()Ljava/lang/String;");
126     jstring   varName       = (jstring) env->CallObjectMethod (jvlcVariable, getNameMethod);
127
128     /* i_int */
129     jclass tmpClass = env->FindClass("org/videolan/jvlc/JVLCIntVariable");
130     if (env->IsInstanceOf(jvlcVariable, tmpClass)) {
131       jmethodID mid = env->GetMethodID(variableClass, "getIntValue", "()I");
132       value.i_int   = env->CallIntMethod(jvlcVariable, mid);
133       goto done;
134     }
135
136     /* b_bool */
137     tmpClass = env->FindClass("org/videolan/jvlc/JVLCBoolVariable");
138     if (env->IsInstanceOf(jvlcVariable, tmpClass )) {
139       jmethodID mid = env->GetMethodID(variableClass, "getBoolValue", "()Z");
140       value.b_bool  = env->CallBooleanMethod(jvlcVariable, mid);
141       goto done;
142     }
143
144     /* f_float */
145     tmpClass = env->FindClass("org/videolan/jvlc/JVLCFloatVariable");
146     if (env->IsInstanceOf(jvlcVariable, tmpClass )) {
147       jmethodID mid = env->GetMethodID(variableClass, "getFloatValue", "()F");
148       value.f_float = env->CallBooleanMethod(jvlcVariable, mid);
149       goto done;
150     }
151
152     /* psz_string */
153     tmpClass = env->FindClass("org/videolan/jvlc/JVLCStringVariable");
154     if (env->IsInstanceOf(jvlcVariable, tmpClass )) {
155       jmethodID mid       = env->GetMethodID(variableClass, "getStringValue", "()Ljava/lang/String");
156       jstring stringValue = (jstring) env->CallObjectMethod(jvlcVariable, mid);
157       value.psz_string    = (char *)env->GetStringUTFChars(stringValue, 0);
158       goto done;
159     }
160
161     /* i_time */
162     tmpClass = env->FindClass("org/videolan/jvlc/JVLCTimeVariable");
163     if (env->IsInstanceOf(jvlcVariable, tmpClass )) {
164       jmethodID mid  = env->GetMethodID(variableClass, "getTimeValue", "()L");
165       value.i_time   = env->CallLongMethod(jvlcVariable, mid);
166       goto done;
167     }
168
169     /* var */
170     tmpClass = env->FindClass("org/videolan/jvlc/JVLCVarVariable");
171     if (env->IsInstanceOf(jvlcVariable, tmpClass )) {
172       /*
173        * This is the longer one. We have to get the VarValue object 
174        * from the JVLCVarVariable and retrieve the OID from it. 
175        */
176       jmethodID getVarValueMID  = env->GetMethodID(variableClass, "getVarValue", "()Lorg/videolan/jvlc/JVLCVarValue;");
177       jobject varVariableObj = env->CallObjectMethod(jvlcVariable, getVarValueMID);
178       
179       /*
180        * Now from the varVariableObj we need to retrieve its name and OID
181        */
182
183       /*
184        * Get the VarValue name
185        */
186
187       jclass varVariableCls = env->GetObjectClass(varVariableObj);
188       jmethodID getVarName  = env->GetMethodID(varVariableCls, "getName","()Ljava/lang/String;");
189       jstring varName       = (jstring) env->CallObjectMethod(varVariableObj, getVarName);
190       value.var.psz_name    = (char *)env->GetStringUTFChars(varName, 0);
191
192       /*
193        * Get the VarValue OID
194        */
195
196       jmethodID getOID = env->GetMethodID(varVariableCls, "getOID", "()I");
197       value.var.i_object_id = env->CallIntMethod(varVariableObj, getOID);
198
199       goto done;
200      }
201
202   done:
203     psz_var = (char *) env->GetStringUTFChars(varName, 0);
204   }
205     
206   if (psz_var != NULL) {
207     int res = VLC_VariableSet(getClassID(env, obj), (char* const) psz_var, value);
208     env->ReleaseStringUTFChars(varName, psz_var);
209     if (stringValue != NULL) env->ReleaseStringUTFChars(stringValue, value.psz_string);
210     return res;
211   }
212   return -1;
213 }
214
215
216 JNIEXPORT jobject JNICALL Java_org_videolan_jvlc_JVLC_getVariable (JNIEnv *env, jobject obj, jstring varName) {
217
218   const char* psz_var = env->GetStringUTFChars(varName, 0);
219   vlc_value_t value;
220   if (VLC_VariableGet(getClassID(env, obj), psz_var, &value) != VLC_SUCCESS) {
221     // throw exception
222     return NULL;
223   }
224
225
226
227   return NULL;
228
229 }
230
231 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_addTarget (JNIEnv *env, jobject obj, jstring URI, jobjectArray options, jint insertMode, jint position) {
232   
233   char*  psz_target   = NULL;
234   char** ppsz_options = NULL;
235   int options_number = 0;
236   
237   if (URI != NULL) {
238     psz_target = (char *) env->GetStringUTFChars(URI, 0);
239   }
240
241   if (options != NULL) {
242     options_number = env->GetArrayLength(options);
243     ppsz_options = (char **) malloc (options_number * sizeof(char*));
244
245     for (int i = 0; i < options_number; i++) {
246       ppsz_options[i] = (char *) env->GetStringUTFChars((jstring) env->GetObjectArrayElement(options, i), 0);
247     }
248   }
249   
250   int res = VLC_AddTarget(getClassID(env, obj), (char const *)psz_target, (const char **) ppsz_options, options_number, insertMode, position);
251
252   if (ppsz_options != NULL) {
253     for (int i = 0; i < options_number; i++) {
254       free(ppsz_options[i]);
255     }
256     free(ppsz_options);
257   }
258   if (psz_target != NULL) {
259     free(psz_target);
260   }
261
262 }
263
264 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_play (JNIEnv *env, jobject obj) {
265   return VLC_Play(getClassID(env, obj));
266 }
267
268 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_pause (JNIEnv *env, jobject obj) {
269   return VLC_Pause(getClassID(env, obj));
270 }
271
272 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_stop (JNIEnv *env, jobject obj) {
273   return VLC_Stop(getClassID(env, obj));
274 }
275
276 JNIEXPORT jboolean JNICALL Java_org_videolan_jvlc_JVLC_isPlaying (JNIEnv *env, jobject obj) {
277   return VLC_IsPlaying(getClassID(env, obj));
278 }
279
280 JNIEXPORT jfloat JNICALL Java_org_videolan_jvlc_JVLC_getPosition (JNIEnv *env, jobject obj) {
281   return VLC_PositionGet(getClassID(env, obj));
282 }
283
284 JNIEXPORT jfloat JNICALL Java_org_videolan_jvlc_JVLC_setPosition (JNIEnv *env, jobject obj, jfloat position) {
285   return VLC_PositionSet(getClassID(env, obj), position);
286 }
287
288 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_getTime (JNIEnv *env, jobject obj) {
289   return VLC_TimeGet(getClassID(env, obj));
290 }
291
292 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_setTime (JNIEnv *env, jobject obj, jint seconds, jboolean relative) {
293   return VLC_TimeSet(getClassID(env, obj), seconds, relative);
294 }
295
296 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_getLength (JNIEnv *env, jobject obj) {
297   return VLC_LengthGet(getClassID(env, obj));
298 }
299
300 JNIEXPORT jfloat JNICALL Java_org_videolan_jvlc_JVLC_speedFaster (JNIEnv *env, jobject obj) {
301   return VLC_SpeedFaster(getClassID(env, obj));
302 }
303
304 JNIEXPORT jfloat JNICALL Java_org_videolan_jvlc_JVLC_speedSlower (JNIEnv *env, jobject obj) {
305   return VLC_SpeedSlower(getClassID(env, obj));
306 }
307
308 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_getPlaylistIndex (JNIEnv *env, jobject obj) {
309   return VLC_PlaylistIndex(getClassID(env, obj));
310 }
311
312 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_getPlaylistItems (JNIEnv *env, jobject obj) {
313   return VLC_PlaylistNumberOfItems(getClassID(env, obj));
314 }
315
316 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_playlistNext (JNIEnv *env, jobject obj) {
317   return VLC_PlaylistNext(getClassID(env, obj));
318 }
319
320 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_playlistPrev (JNIEnv *env, jobject obj) {
321   return VLC_PlaylistPrev(getClassID(env, obj));
322 }
323
324 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_playlistClear (JNIEnv *env, jobject obj) {
325   return VLC_PlaylistClear(getClassID(env, obj));
326 }
327
328 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_getVolume (JNIEnv *env, jobject obj) {
329   return VLC_VolumeGet(getClassID(env, obj));
330 }
331
332 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_setVolume (JNIEnv *env, jobject obj, jint volume) {
333   return VLC_VolumeSet(getClassID(env, obj), volume);
334 }
335
336 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_muteVolume (JNIEnv *env, jobject obj) {
337   return VLC_VolumeMute(getClassID(env, obj));
338 }
339
340 JNIEXPORT jint JNICALL Java_org_videolan_jvlc_JVLC_fullScreen (JNIEnv *env, jobject obj) {
341   return VLC_FullScreen(getClassID(env, obj));
342 }
343
344 /*
345  * This method is an helper method 
346  * only valid if obj == JVLC
347  */
348
349 int getClassID (JNIEnv *env, jobject obj) {
350   /* get the id field of object */
351   jclass    cls   = env->GetObjectClass(obj);
352   jmethodID mid   = env->GetMethodID(cls, "getID", "()I");
353   int       field = env->CallIntMethod(obj, mid);
354   return field;
355 }
356