libvlcjni.c 19.3 KB
Newer Older
Rafaël Carré's avatar
Rafaël Carré committed
1
2
3
/*****************************************************************************
 * libvlcjni.c
 *****************************************************************************
Edward Wang's avatar
Edward Wang committed
4
 * Copyright © 2010-2013 VLC authors and VideoLAN
Rafaël Carré's avatar
Rafaël Carré committed
5
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
6
7
8
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
Rafaël Carré's avatar
Rafaël Carré committed
9
10
11
12
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
13
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
Rafaël Carré's avatar
Rafaël Carré committed
15
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16
17
18
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Rafaël Carré's avatar
Rafaël Carré committed
19
20
 *****************************************************************************/

21
22
#include <dirent.h>
#include <errno.h>
23
#include <string.h>
24
#include <pthread.h>
25
#include <sys/stat.h>
26
#include <sys/types.h>
27
#include <unistd.h>
28

29
#include <vlc/vlc.h>
30
31
#include <vlc_common.h>
#include <vlc_url.h>
32

33
#include <jni.h>
34

35
#include <android/api-level.h>
36

37
#include "libvlcjni.h"
ivoire's avatar
ivoire committed
38
#include "aout.h"
39
#include "utils.h"
40

41
42
43
#define VOUT_ANDROID_SURFACE 0
#define VOUT_OPENGLES2       1

44
#define LOG_TAG "VLC/JNI/main"
45
46
#include "log.h"

47
libvlc_media_t *new_media(jlong instance, JNIEnv *env, jobject thiz, jstring fileLocation, bool noOmx, bool noVideo)
48
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
49
    libvlc_instance_t *libvlc = (libvlc_instance_t*)(intptr_t)instance;
50
    jboolean isCopy;
51
52
53
    const char *psz_location = (*env)->GetStringUTFChars(env, fileLocation, &isCopy);
    libvlc_media_t *p_md = libvlc_media_new_location(libvlc, psz_location);
    (*env)->ReleaseStringUTFChars(env, fileLocation, psz_location);
54
55
56
    if (!p_md)
        return NULL;

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    if (!noOmx) {
        jclass cls = (*env)->GetObjectClass(env, thiz);
        jmethodID methodId = (*env)->GetMethodID(env, cls, "useIOMX", "()Z");
        if ((*env)->CallBooleanMethod(env, thiz, methodId)) {
            /*
             * Set higher caching values if using iomx decoding, since some omx
             * decoders have a very high latency, and if the preroll data isn't
             * enough to make the decoder output a frame, the playback timing gets
             * started too soon, and every decoded frame appears to be too late.
             * On Nexus One, the decoder latency seems to be 25 input packets
             * for 320x170 H.264, a few packets less on higher resolutions.
             * On Nexus S, the decoder latency seems to be about 7 packets.
             */
            libvlc_media_add_option(p_md, ":file-caching=1500");
            libvlc_media_add_option(p_md, ":network-caching=1500");
72
            libvlc_media_add_option(p_md, ":codec=mediacodec,iomx,all");
73
        }
74
75
        if (noVideo)
            libvlc_media_add_option(p_md, ":no-video");
76
    }
77
    return p_md;
78
79
}

80
libvlc_media_player_t *getMediaPlayer(JNIEnv *env, jobject thiz)
81
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
82
    return (libvlc_media_player_t*)(intptr_t)getLong(env, thiz, "mInternalMediaPlayerInstance");
83
84
}

Rafaël Carré's avatar
Rafaël Carré committed
85
86
static void releaseMediaPlayer(JNIEnv *env, jobject thiz)
{
87
88
    libvlc_media_player_t* p_mp = getMediaPlayer(env, thiz);
    if (p_mp)
89
    {
90
91
        libvlc_media_player_stop(p_mp);
        libvlc_media_player_release(p_mp);
92
        setLong(env, thiz, "mInternalMediaPlayerInstance", 0);
93
94
95
    }
}

96
97
98
99
100
/* Pointer to the Java virtual machine
 * Note: It's okay to use a static variable for the VM pointer since there
 * can only be one instance of this shared library in a single VM
 */
JavaVM *myVm;
101

102
static jobject eventHandlerInstance = NULL;
103

104
105
/** vout lock declared in vout.c */
extern pthread_mutex_t vout_android_lock;
106
extern pthread_cond_t vout_android_surf_attached;
107

108
109
110
111
static void vlc_event_callback(const libvlc_event_t *ev, void *data)
{
    JNIEnv *env;

112
    bool isAttached = false;
113

114
    if (eventHandlerInstance == NULL)
Sébastien Toque's avatar
Sébastien Toque committed
115
        return;
116

Rafaël Carré's avatar
Rafaël Carré committed
117
118
    if ((*myVm)->GetEnv(myVm, (void**) &env, JNI_VERSION_1_2) < 0) {
        if ((*myVm)->AttachCurrentThread(myVm, &env, NULL) < 0)
119
            return;
120
        isAttached = true;
121
122
    }

123
124
125
126
127
128
129
130
131
132
    /* Creating the bundle in C allows us to subscribe to more events
     * and get better flexibility for each event. For example, we can
     * have totally different types of data for each event, instead of,
     * for example, only an integer and/or string.
     */
    jclass clsBundle = (*env)->FindClass(env, "android/os/Bundle");
    jmethodID clsCtor = (*env)->GetMethodID(env, clsBundle, "<init>", "()V" );
    jobject bundle = (*env)->NewObject(env, clsBundle, clsCtor);

    jmethodID putInt = (*env)->GetMethodID(env, clsBundle, "putInt", "(Ljava/lang/String;I)V" );
133
    jmethodID putFloat = (*env)->GetMethodID(env, clsBundle, "putFloat", "(Ljava/lang/String;F)V" );
134
135
    jmethodID putString = (*env)->GetMethodID(env, clsBundle, "putString", "(Ljava/lang/String;Ljava/lang/String;)V" );

136
137
138
139
140
    if (ev->type == libvlc_MediaPlayerPositionChanged) {
            jstring sData = (*env)->NewStringUTF(env, "data");
            (*env)->CallVoidMethod(env, bundle, putFloat, sData, ev->u.media_player_position_changed.new_position);
            (*env)->DeleteLocalRef(env, sData);
    } else if(ev->type == libvlc_MediaPlayerVout) {
141
142
143
144
        /* For determining the vout/ES track change */
        jstring sData = (*env)->NewStringUTF(env, "data");
        (*env)->CallVoidMethod(env, bundle, putInt, sData, ev->u.media_player_vout.new_count);
        (*env)->DeleteLocalRef(env, sData);
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    } else if(ev->type == libvlc_MediaListItemAdded ||
              ev->type == libvlc_MediaListItemDeleted ) {
        jstring item_uri = (*env)->NewStringUTF(env, "item_uri");
        jstring item_index = (*env)->NewStringUTF(env, "item_index");
        char* mrl = libvlc_media_get_mrl(
            ev->type == libvlc_MediaListItemAdded ?
            ev->u.media_list_item_added.item :
            ev->u.media_list_item_deleted.item
            );
        jstring item_uri_value = (*env)->NewStringUTF(env, mrl);
        jint item_index_value;
        if(ev->type == libvlc_MediaListItemAdded)
            item_index_value = ev->u.media_list_item_added.index;
        else
            item_index_value = ev->u.media_list_item_deleted.index;

        (*env)->CallVoidMethod(env, bundle, putString, item_uri, item_uri_value);
        (*env)->CallVoidMethod(env, bundle, putInt, item_index, item_index_value);

        (*env)->DeleteLocalRef(env, item_uri);
        (*env)->DeleteLocalRef(env, item_uri_value);
        (*env)->DeleteLocalRef(env, item_index);
        free(mrl);
168
169
    }

170
    /* Get the object class */
171
    jclass cls = (*env)->GetObjectClass(env, eventHandlerInstance);
172
    if (!cls) {
173
        LOGE("EventHandler: failed to get class reference");
174
        goto end;
175
176
177
    }

    /* Find the callback ID */
178
    jmethodID methodID = (*env)->GetMethodID(env, cls, "callback", "(ILandroid/os/Bundle;)V");
179
    if (methodID) {
180
        (*env)->CallVoidMethod(env, eventHandlerInstance, methodID, ev->type, bundle);
181
    } else {
182
        LOGE("EventHandler: failed to get the callback method");
183
184
    }

185
end:
Edward Wang's avatar
Edward Wang committed
186
    (*env)->DeleteLocalRef(env, bundle);
187
188
    if (isAttached)
        (*myVm)->DetachCurrentThread(myVm);
189
190
}

191
jint JNI_OnLoad(JavaVM *vm, void *reserved)
192
{
193
194
195
    // Keep a reference on the Java VM.
    myVm = vm;

196
    pthread_mutex_init(&vout_android_lock, NULL);
197
    pthread_cond_init(&vout_android_surf_attached, NULL);
198

199
    LOGD("JNI interface loaded.");
200
    return JNI_VERSION_1_2;
201
202
}

203
204
void JNI_OnUnload(JavaVM* vm, void* reserved) {
    pthread_mutex_destroy(&vout_android_lock);
205
    pthread_cond_destroy(&vout_android_surf_attached);
206
207
}

Rafaël Carré's avatar
Rafaël Carré committed
208
209
// FIXME: use atomics
static bool verbosity;
210

211
void Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
212
{
213
214
215
216
217
    //only use OpenSLES if java side says we can
    jclass cls = (*env)->GetObjectClass(env, thiz);
    jmethodID methodId = (*env)->GetMethodID(env, cls, "getAout", "()I");
    bool use_opensles = (*env)->CallIntMethod(env, thiz, methodId) == AOUT_OPENSLES;

218
219
220
    methodId = (*env)->GetMethodID(env, cls, "getVout", "()I");
    bool use_opengles2 = (*env)->CallIntMethod(env, thiz, methodId) == VOUT_OPENGLES2;

221
222
223
    methodId = (*env)->GetMethodID(env, cls, "timeStretchingEnabled", "()Z");
    bool enable_time_stretch = (*env)->CallBooleanMethod(env, thiz, methodId);

Edward Wang's avatar
Edward Wang committed
224
225
226
    methodId = (*env)->GetMethodID(env, cls, "frameSkipEnabled", "()Z");
    bool enable_frame_skip = (*env)->CallBooleanMethod(env, thiz, methodId);

Edward Wang's avatar
Edward Wang committed
227
228
229
230
231
    methodId = (*env)->GetMethodID(env, cls, "getDeblocking", "()I");
    int deblocking = (*env)->CallIntMethod(env, thiz, methodId);
    char deblockstr[2] = "3";
    snprintf(deblockstr, 2, "%d", deblocking);
    LOGD("Using deblocking level %d", deblocking);
232

Edward Wang's avatar
Edward Wang committed
233
234
235
236
237
238
239
240
    methodId = (*env)->GetMethodID(env, cls, "getNetworkCaching", "()I");
    int networkCaching = (*env)->CallIntMethod(env, thiz, methodId);
    char networkCachingstr[25] = "0";
    if(networkCaching > 0) {
        snprintf(networkCachingstr, 25, "--network-caching=%d", networkCaching);
        LOGD("Using network caching of %d ms", networkCaching);
    }

241
242
243
244
    methodId = (*env)->GetMethodID(env, cls, "getChroma", "()Ljava/lang/String;");
    jstring chroma = (*env)->CallObjectMethod(env, thiz, methodId);
    const char *chromastr = (*env)->GetStringUTFChars(env, chroma, 0);
    LOGD("Chroma set to \"%s\"", chromastr);
Edward Wang's avatar
Edward Wang committed
245

246
247
248
    methodId = (*env)->GetMethodID(env, cls, "getSubtitlesEncoding", "()Ljava/lang/String;");
    jstring subsencoding = (*env)->CallObjectMethod(env, thiz, methodId);
    const char *subsencodingstr = (*env)->GetStringUTFChars(env, subsencoding, 0);
Edward Wang's avatar
Edward Wang committed
249
    LOGD("Subtitle encoding set to \"%s\"", subsencodingstr);
250

251
252
253
    methodId = (*env)->GetMethodID(env, cls, "isVerboseMode", "()Z");
    verbosity = (*env)->CallBooleanMethod(env, thiz, methodId);

254
    /* Don't add any invalid options, otherwise it causes LibVLC to crash */
255
    const char *argv[] = {
256
        "-I", "dummy",
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
257
258
259
        "--no-osd",
        "--no-video-title-show",
        "--no-stats",
260
261
        "--no-plugins-cache",
        "--no-drop-late-frames",
262
263
264
265
        /* The VLC default is to pick the highest resolution possible
         * (i.e. 1080p). For mobile, pick a more sane default for slow
         * mobile data networks and slower hardware. */
        "--preferred-resolution", "360",
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
266
        "--avcodec-fast",
267
        "--avcodec-threads=0",
268
        "--subsdec-encoding", subsencodingstr,
269
        enable_time_stretch ? "--audio-time-stretch" : "--no-audio-time-stretch",
Edward Wang's avatar
Edward Wang committed
270
        "--avcodec-skiploopfilter", deblockstr,
271
272
        "--avcodec-skip-frame", enable_frame_skip ? "2" : "0",
        "--avcodec-skip-idct", enable_frame_skip ? "2" : "0",
Edward Wang's avatar
Edward Wang committed
273
        (networkCaching > 0) ? networkCachingstr : "",
274
        use_opensles ? "--aout=opensles" : "--aout=android_audiotrack",
275
        use_opengles2 ? "--vout=gles2" : "--vout=androidsurface",
276
        "--androidsurface-chroma", chromastr != NULL && chromastr[0] != 0 ? chromastr : "RV32",
277
    };
278
    libvlc_instance_t *instance = libvlc_new(sizeof(argv) / sizeof(*argv), argv);
279

Ludovic Fauvet's avatar
Ludovic Fauvet committed
280
    setLong(env, thiz, "mLibVlcInstance", (jlong)(intptr_t) instance);
281

282
    (*env)->ReleaseStringUTFChars(env, chroma, chromastr);
283
284
    (*env)->ReleaseStringUTFChars(env, subsencoding, subsencodingstr);

285
286
    if (!instance)
    {
287
        jclass exc = (*env)->FindClass(env, "org/videolan/libvlc/LibVlcException");
288
289
290
291
        (*env)->ThrowNew(env, exc, "Unable to instantiate LibVLC");
    }

    LOGI("LibVLC initialized: %p", instance);
292

Rafaël Carré's avatar
Rafaël Carré committed
293
    libvlc_log_set(instance, debug_log, &verbosity);
294
295
}

296
void Java_org_videolan_libvlc_LibVLC_nativeDestroy(JNIEnv *env, jobject thiz)
297
{
298
    releaseMediaPlayer(env, thiz);
Edward Wang's avatar
Edward Wang committed
299
    jlong libVlcInstance = getLong(env, thiz, "mLibVlcInstance");
300
301
302
    if (!libVlcInstance)
        return; // Already destroyed

Ludovic Fauvet's avatar
Ludovic Fauvet committed
303
    libvlc_instance_t *instance = (libvlc_instance_t*)(intptr_t) libVlcInstance;
Rafaël Carré's avatar
Rafaël Carré committed
304
    libvlc_log_unset(instance);
305
306
    libvlc_release(instance);

Edward Wang's avatar
Edward Wang committed
307
    setLong(env, thiz, "mLibVlcInstance", 0);
308
309
}

310
void Java_org_videolan_libvlc_LibVLC_detachEventHandler(JNIEnv *env, jobject thiz)
311
{
312
313
314
    if (eventHandlerInstance != NULL) {
        (*env)->DeleteGlobalRef(env, eventHandlerInstance);
        eventHandlerInstance = NULL;
315
316
317
    }
}

318
void Java_org_videolan_libvlc_LibVLC_setEventHandler(JNIEnv *env, jobject thiz, jobject eventHandler)
319
{
320
321
322
    if (eventHandlerInstance != NULL) {
        (*env)->DeleteGlobalRef(env, eventHandlerInstance);
        eventHandlerInstance = NULL;
323
324
    }

325
    eventHandlerInstance = getEventHandlerReference(env, thiz, eventHandler);
326
327
}

328
329
static void create_player_and_play(JNIEnv* env, jobject thiz,
                                   jlong instance, int position) {
330
331
332
    /* Release previous media player, if any */
    releaseMediaPlayer(env, thiz);

333
    /* Create a media player playing environment */
Ludovic Fauvet's avatar
Ludovic Fauvet committed
334
    libvlc_media_player_t *mp = libvlc_media_player_new((libvlc_instance_t*)(intptr_t)instance);
335
336
    jobject myJavaLibVLC = (*env)->NewGlobalRef(env, thiz);

337
338
339
340
    //if AOUT_AUDIOTRACK_JAVA, we use amem
    jclass cls = (*env)->GetObjectClass(env, thiz);
    jmethodID methodId = (*env)->GetMethodID(env, cls, "getAout", "()I");
    if ( (*env)->CallIntMethod(env, thiz, methodId) == AOUT_AUDIOTRACK_JAVA )
341
    {
342
        libvlc_audio_set_callbacks(mp, aout_play, aout_pause, NULL, NULL, NULL,
Sébastien Toque's avatar
Sébastien Toque committed
343
344
                                   (void*) myJavaLibVLC);
        libvlc_audio_set_format_callbacks(mp, aout_open, aout_close);
345
346
    }

347
348
    /* Connect the event manager */
    libvlc_event_manager_t *ev = libvlc_media_player_event_manager(mp);
349
350
351
352
353
    static const libvlc_event_type_t mp_events[] = {
        libvlc_MediaPlayerPlaying,
        libvlc_MediaPlayerPaused,
        libvlc_MediaPlayerEndReached,
        libvlc_MediaPlayerStopped,
354
        libvlc_MediaPlayerVout,
355
356
        libvlc_MediaPlayerPositionChanged,
        libvlc_MediaPlayerEncounteredError
357
    };
358
    for(int i = 0; i < (sizeof(mp_events) / sizeof(*mp_events)); i++)
359
360
        libvlc_event_attach(ev, mp_events[i], vlc_event_callback, myVm);

361

362
    /* Keep a pointer to this media player */
Ludovic Fauvet's avatar
Ludovic Fauvet committed
363
    setLong(env, thiz, "mInternalMediaPlayerInstance", (jlong)(intptr_t)mp);
364

365
366
367
368
    cls = (*env)->GetObjectClass(env, thiz);
    jmethodID methodID = (*env)->GetMethodID(env, cls, "applyEqualizer", "()V");
    (*env)->CallVoidMethod(env, thiz, methodID);

369
    setInt(env, thiz, "mInternalMediaPlayerIndex", (jint)position);
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

    jfieldID fid = (*env)->GetFieldID(env, cls, "mMediaList", "Lorg/videolan/libvlc/MediaList;");
    jobject list = (*env)->GetObjectField(env, thiz, fid);
    jclass clsList = (*env)->FindClass(env, "org/videolan/libvlc/MediaList");

    methodID = (*env)->GetMethodID(env, clsList, "getMRL", "(I)Ljava/lang/String;");
    jstring mrl = (jstring)(*env)->CallObjectMethod(env, list, methodID, (jint)position);
    const char* p_mrl = (*env)->GetStringUTFChars(env, mrl, 0);

    libvlc_media_t* p_md = libvlc_media_new_location((libvlc_instance_t*)(intptr_t)instance, p_mrl);
    /* media options */
    methodID = (*env)->GetMethodID(env, clsList, "getMediaOptions", "(I)[Ljava/lang/String;");
    jobjectArray mediaOptions = (jobject)(*env)->CallObjectMethod(env, list, methodID, (jint)position);
    int stringCount = (*env)->GetArrayLength(env, mediaOptions);
    for(int i = 0; i < stringCount; i++) {
        jstring option = (jstring)(*env)->GetObjectArrayElement(env, mediaOptions, i);
        const char* p_st = (*env)->GetStringUTFChars(env, option, 0);
        libvlc_media_add_option(p_md, p_st); // option
        (*env)->ReleaseStringUTFChars(env, option, p_st);
    }

    (*env)->ReleaseStringUTFChars(env, mrl, p_mrl);
    (*env)->DeleteLocalRef(env, mrl);
    (*env)->DeleteLocalRef(env, mediaOptions);
    (*env)->DeleteLocalRef(env, list);
    list = NULL;

397
398
    libvlc_media_player_set_media(mp, p_md);
    libvlc_media_player_play(mp);
399
400
}

401
void Java_org_videolan_libvlc_LibVLC_playIndex(JNIEnv *env, jobject thiz,
402
403
404
405
                                            jlong instance, int position) {
    create_player_and_play(env, thiz, instance, position);
}

406
jfloat Java_org_videolan_libvlc_LibVLC_getRate(JNIEnv *env, jobject thiz) {
Edward Wang's avatar
Edward Wang committed
407
408
409
410
411
412
413
    libvlc_media_player_t* mp = getMediaPlayer(env, thiz);
    if(mp)
        return libvlc_media_player_get_rate(mp);
    else
        return 1.00;
}

414
void Java_org_videolan_libvlc_LibVLC_setRate(JNIEnv *env, jobject thiz, jfloat rate) {
Edward Wang's avatar
Edward Wang committed
415
416
417
418
419
    libvlc_media_player_t* mp = getMediaPlayer(env, thiz);
    if(mp)
        libvlc_media_player_set_rate(mp, rate);
}

420
jboolean Java_org_videolan_libvlc_LibVLC_isPlaying(JNIEnv *env, jobject thiz)
421
{
422
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
423
    if (mp)
424
        return !!libvlc_media_player_is_playing(mp);
425
426
    else
        return 0;
427
428
}

429
jboolean Java_org_videolan_libvlc_LibVLC_isSeekable(JNIEnv *env, jobject thiz)
430
{
431
432
433
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return !!libvlc_media_player_is_seekable(mp);
434
    return 0;
435
436
}

437
void Java_org_videolan_libvlc_LibVLC_play(JNIEnv *env, jobject thiz)
438
{
439
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
440
    if (mp)
441
        libvlc_media_player_play(mp);
442
443
}

444
void Java_org_videolan_libvlc_LibVLC_pause(JNIEnv *env, jobject thiz)
445
{
446
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
447
    if (mp)
448
        libvlc_media_player_pause(mp);
449
450
}

451
void Java_org_videolan_libvlc_LibVLC_stop(JNIEnv *env, jobject thiz)
452
{
453
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
454
    if (mp)
455
        libvlc_media_player_stop(mp);
456
457
}

458
jint Java_org_videolan_libvlc_LibVLC_getVolume(JNIEnv *env, jobject thiz)
459
{
460
461
462
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return (jint) libvlc_audio_get_volume(mp);
463
464
465
    return -1;
}

466
jint Java_org_videolan_libvlc_LibVLC_setVolume(JNIEnv *env, jobject thiz, jint volume)
467
{
468
469
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
470
        //Returns 0 if the volume was set, -1 if it was out of range or error
471
        return (jint) libvlc_audio_set_volume(mp, (int) volume);
472
473
474
    return -1;
}

475
jlong Java_org_videolan_libvlc_LibVLC_getTime(JNIEnv *env, jobject thiz)
476
{
477
478
479
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return libvlc_media_player_get_time(mp);
480
481
482
    return -1;
}

483
void Java_org_videolan_libvlc_LibVLC_setTime(JNIEnv *env, jobject thiz, jlong time)
484
{
485
486
487
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        libvlc_media_player_set_time(mp, time);
488
489
}

490
jfloat Java_org_videolan_libvlc_LibVLC_getPosition(JNIEnv *env, jobject thiz)
491
{
492
493
494
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return (jfloat) libvlc_media_player_get_position(mp);
495
496
497
    return -1;
}

498
void Java_org_videolan_libvlc_LibVLC_setPosition(JNIEnv *env, jobject thiz, jfloat pos)
499
{
500
501
502
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        libvlc_media_player_set_position(mp, pos);
503
504
}

505
jlong Java_org_videolan_libvlc_LibVLC_getLength(JNIEnv *env, jobject thiz)
506
{
507
508
509
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return (jlong) libvlc_media_player_get_length(mp);
510
511
512
    return -1;
}

513
jstring Java_org_videolan_libvlc_LibVLC_version(JNIEnv* env, jobject thiz)
514
{
515
516
    return (*env)->NewStringUTF(env, libvlc_get_version());
}
517

518
jstring Java_org_videolan_libvlc_LibVLC_compiler(JNIEnv* env, jobject thiz)
519
520
521
522
{
    return (*env)->NewStringUTF(env, libvlc_get_compiler());
}

523
jstring Java_org_videolan_libvlc_LibVLC_changeset(JNIEnv* env, jobject thiz)
524
525
526
{
    return (*env)->NewStringUTF(env, libvlc_get_changeset());
}