libvlcjni.c 20.9 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 "vout.h"
40
#include "utils.h"
41
#include "native_crash_handler.h"
42

43
44
45
#define VOUT_ANDROID_SURFACE 0
#define VOUT_OPENGLES2       1

46
47
48
49
#define HW_ACCELERATION_DISABLED 0
#define HW_ACCELERATION_DECODING 1
#define HW_ACCELERATION_FULL     2

50
#define LOG_TAG "VLC/JNI/main"
51
52
#include "log.h"

53
54
55
56
57
58
#ifdef HAVE_IOMX_DR
#define NO_IOMX_DR "--no-omxil-dr"
#else
#define NO_IOMX_DR ""
#endif

59
libvlc_media_t *new_media(jlong instance, JNIEnv *env, jobject thiz, jstring fileLocation, bool noOmx, bool noVideo)
60
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
61
    libvlc_instance_t *libvlc = (libvlc_instance_t*)(intptr_t)instance;
62
    jboolean isCopy;
63
64
65
    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);
66
67
68
    if (!p_md)
        return NULL;

69
70
    if (!noOmx) {
        jclass cls = (*env)->GetObjectClass(env, thiz);
71
72
73
        jmethodID methodId = (*env)->GetMethodID(env, cls, "getHardwareAcceleration", "()I");
        int hardwareAcceleration = (*env)->CallIntMethod(env, thiz, methodId);
        if (hardwareAcceleration == HW_ACCELERATION_DECODING || hardwareAcceleration == HW_ACCELERATION_FULL) {
74
75
76
77
78
79
80
81
82
83
84
            /*
             * 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");
85
            libvlc_media_add_option(p_md, ":codec=mediacodec,iomx,all");
86
        }
87
88
        if (noVideo)
            libvlc_media_add_option(p_md, ":no-video");
89
    }
90
    return p_md;
91
92
}

93
libvlc_media_player_t *getMediaPlayer(JNIEnv *env, jobject thiz)
94
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
95
    return (libvlc_media_player_t*)(intptr_t)getLong(env, thiz, "mInternalMediaPlayerInstance");
96
97
}

Rafaël Carré's avatar
Rafaël Carré committed
98
99
static void releaseMediaPlayer(JNIEnv *env, jobject thiz)
{
100
101
    libvlc_media_player_t* p_mp = getMediaPlayer(env, thiz);
    if (p_mp)
102
    {
103
104
        libvlc_media_player_stop(p_mp);
        libvlc_media_player_release(p_mp);
105
        setLong(env, thiz, "mInternalMediaPlayerInstance", 0);
106
107
108
    }
}

109
110
111
112
113
/* 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;
114

115
static jobject eventHandlerInstance = NULL;
116
117
118
119
120

static void vlc_event_callback(const libvlc_event_t *ev, void *data)
{
    JNIEnv *env;

121
    bool isAttached = false;
122

123
    if (eventHandlerInstance == NULL)
Sébastien Toque's avatar
Sébastien Toque committed
124
        return;
125

Rafaël Carré's avatar
Rafaël Carré committed
126
127
    if ((*myVm)->GetEnv(myVm, (void**) &env, JNI_VERSION_1_2) < 0) {
        if ((*myVm)->AttachCurrentThread(myVm, &env, NULL) < 0)
128
            return;
129
        isAttached = true;
130
131
    }

132
133
134
135
136
137
138
139
140
141
    /* 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" );
142
    jmethodID putFloat = (*env)->GetMethodID(env, clsBundle, "putFloat", "(Ljava/lang/String;F)V" );
143
144
    jmethodID putString = (*env)->GetMethodID(env, clsBundle, "putString", "(Ljava/lang/String;Ljava/lang/String;)V" );

145
146
147
148
149
    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) {
150
151
152
153
        /* 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);
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    } 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);
177
178
    }

179
    /* Get the object class */
180
    jclass cls = (*env)->GetObjectClass(env, eventHandlerInstance);
181
    if (!cls) {
182
        LOGE("EventHandler: failed to get class reference");
183
        goto end;
184
185
186
    }

    /* Find the callback ID */
187
    jmethodID methodID = (*env)->GetMethodID(env, cls, "callback", "(ILandroid/os/Bundle;)V");
188
    if (methodID) {
189
        (*env)->CallVoidMethod(env, eventHandlerInstance, methodID, ev->type, bundle);
190
    } else {
191
        LOGE("EventHandler: failed to get the callback method");
192
193
    }

194
end:
Edward Wang's avatar
Edward Wang committed
195
    (*env)->DeleteLocalRef(env, bundle);
196
197
    if (isAttached)
        (*myVm)->DetachCurrentThread(myVm);
198
199
}

200
jint JNI_OnLoad(JavaVM *vm, void *reserved)
201
{
202
203
204
    // Keep a reference on the Java VM.
    myVm = vm;

205
    pthread_mutex_init(&vout_android_lock, NULL);
206
    pthread_cond_init(&vout_android_surf_attached, NULL);
207

208
    LOGD("JNI interface loaded.");
209
    return JNI_VERSION_1_2;
210
211
}

212
213
void JNI_OnUnload(JavaVM* vm, void* reserved) {
    pthread_mutex_destroy(&vout_android_lock);
214
    pthread_cond_destroy(&vout_android_surf_attached);
215
216
}

Rafaël Carré's avatar
Rafaël Carré committed
217
218
// FIXME: use atomics
static bool verbosity;
219

220
void Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
221
{
222
223
224
225
226
    //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;

227
228
229
    methodId = (*env)->GetMethodID(env, cls, "getVout", "()I");
    bool use_opengles2 = (*env)->CallIntMethod(env, thiz, methodId) == VOUT_OPENGLES2;

230
231
232
    methodId = (*env)->GetMethodID(env, cls, "timeStretchingEnabled", "()Z");
    bool enable_time_stretch = (*env)->CallBooleanMethod(env, thiz, methodId);

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

Edward Wang's avatar
Edward Wang committed
236
237
    methodId = (*env)->GetMethodID(env, cls, "getDeblocking", "()I");
    int deblocking = (*env)->CallIntMethod(env, thiz, methodId);
Rafaël Carré's avatar
Rafaël Carré committed
238
239
    char deblockstr[2];
    snprintf(deblockstr, sizeof(deblockstr), "%d", deblocking);
Edward Wang's avatar
Edward Wang committed
240
    LOGD("Using deblocking level %d", deblocking);
241

Edward Wang's avatar
Edward Wang committed
242
243
    methodId = (*env)->GetMethodID(env, cls, "getNetworkCaching", "()I");
    int networkCaching = (*env)->CallIntMethod(env, thiz, methodId);
Rafaël Carré's avatar
Rafaël Carré committed
244
    char networkCachingstr[25];
Edward Wang's avatar
Edward Wang committed
245
    if(networkCaching > 0) {
Rafaël Carré's avatar
Rafaël Carré committed
246
        snprintf(networkCachingstr, sizeof(networkCachingstr), "--network-caching=%d", networkCaching);
Edward Wang's avatar
Edward Wang committed
247
248
249
        LOGD("Using network caching of %d ms", networkCaching);
    }

250
251
252
253
    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
254

255
256
257
    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
258
    LOGD("Subtitle encoding set to \"%s\"", subsencodingstr);
259

260
261
262
    methodId = (*env)->GetMethodID(env, cls, "isVerboseMode", "()Z");
    verbosity = (*env)->CallBooleanMethod(env, thiz, methodId);

263
264
    methodId = (*env)->GetMethodID(env, cls, "getHardwareAcceleration", "()I");
    int hardwareAcceleration = (*env)->CallIntMethod(env, thiz, methodId);
265
266
267
    /* With the MediaCodec opaque mode we cannot use the OpenGL ES vout. */
    if (hardwareAcceleration == HW_ACCELERATION_FULL)
        use_opengles2 = false;
268

flx42's avatar
flx42 committed
269
270
    methodId = (*env)->GetMethodID(env, cls, "getCachePath", "()Ljava/lang/String;");
    jstring cachePath = (*env)->CallObjectMethod(env, thiz, methodId);
271
272
273
274
275
    if (cachePath) {
        const char *cache_path = (*env)->GetStringUTFChars(env, cachePath, 0);
        setenv("DVDCSS_CACHE", cache_path, 1);
        (*env)->ReleaseStringUTFChars(env, cachePath, cache_path);
    }
flx42's avatar
flx42 committed
276

277
    /* Don't add any invalid options, otherwise it causes LibVLC to crash */
278
    const char *argv[] = {
279
        /* CPU intensive plugin, setting for slow devices */
280
        enable_time_stretch ? "--audio-time-stretch" : "--no-audio-time-stretch",
281
282

        /* avcodec speed settings for slow devices */
283
        //"--avcodec-fast", // non-spec-compliant speedup tricks
Edward Wang's avatar
Edward Wang committed
284
        "--avcodec-skiploopfilter", deblockstr,
285
286
        "--avcodec-skip-frame", enable_frame_skip ? "2" : "0",
        "--avcodec-skip-idct", enable_frame_skip ? "2" : "0",
287
288
289
290
291

        /* Remove me when UTF-8 is enforced by law */
        "--subsdec-encoding", subsencodingstr,

        /* XXX: why can't the default be fine ? #7792 */
Edward Wang's avatar
Edward Wang committed
292
        (networkCaching > 0) ? networkCachingstr : "",
293
294

        /* Android audio API is a mess */
295
        use_opensles ? "--aout=opensles" : "--aout=android_audiotrack",
296
297

        /* Android video API is a mess */
298
        use_opengles2 ? "--vout=gles2" : "--vout=androidsurface",
299
        "--androidsurface-chroma", chromastr != NULL && chromastr[0] != 0 ? chromastr : "RV32",
300
        /* XXX: we can't recover from direct rendering failure */
301
        (hardwareAcceleration == HW_ACCELERATION_FULL) ? "" : "--no-mediacodec-dr",
302
        (hardwareAcceleration == HW_ACCELERATION_FULL) ? "" : NO_IOMX_DR,
303
    };
304
    libvlc_instance_t *instance = libvlc_new(sizeof(argv) / sizeof(*argv), argv);
305

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

308
    (*env)->ReleaseStringUTFChars(env, chroma, chromastr);
309
310
    (*env)->ReleaseStringUTFChars(env, subsencoding, subsencodingstr);

311
312
    if (!instance)
    {
313
        jclass exc = (*env)->FindClass(env, "org/videolan/libvlc/LibVlcException");
314
315
316
317
        (*env)->ThrowNew(env, exc, "Unable to instantiate LibVLC");
    }

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

Rafaël Carré's avatar
Rafaël Carré committed
319
    libvlc_log_set(instance, debug_log, &verbosity);
320
321

    init_native_crash_handler(env, thiz);
322
323
}

324
void Java_org_videolan_libvlc_LibVLC_nativeDestroy(JNIEnv *env, jobject thiz)
325
{
326
327
    destroy_native_crash_handler(env);

328
    releaseMediaPlayer(env, thiz);
Edward Wang's avatar
Edward Wang committed
329
    jlong libVlcInstance = getLong(env, thiz, "mLibVlcInstance");
330
331
332
    if (!libVlcInstance)
        return; // Already destroyed

Ludovic Fauvet's avatar
Ludovic Fauvet committed
333
    libvlc_instance_t *instance = (libvlc_instance_t*)(intptr_t) libVlcInstance;
Rafaël Carré's avatar
Rafaël Carré committed
334
    libvlc_log_unset(instance);
335
336
    libvlc_release(instance);

Edward Wang's avatar
Edward Wang committed
337
    setLong(env, thiz, "mLibVlcInstance", 0);
338
339
}

340
void Java_org_videolan_libvlc_LibVLC_detachEventHandler(JNIEnv *env, jobject thiz)
341
{
342
343
344
    if (eventHandlerInstance != NULL) {
        (*env)->DeleteGlobalRef(env, eventHandlerInstance);
        eventHandlerInstance = NULL;
345
346
347
    }
}

348
void Java_org_videolan_libvlc_LibVLC_setEventHandler(JNIEnv *env, jobject thiz, jobject eventHandler)
349
{
350
351
352
    if (eventHandlerInstance != NULL) {
        (*env)->DeleteGlobalRef(env, eventHandlerInstance);
        eventHandlerInstance = NULL;
353
354
    }

355
    eventHandlerInstance = getEventHandlerReference(env, thiz, eventHandler);
356
357
}

Edward Wang's avatar
Edward Wang committed
358
void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv *env, jobject thiz, jlong instance,
359
360
                                             jstring mrl, jobjectArray mediaOptions)
{
361
362
363
    /* Release previous media player, if any */
    releaseMediaPlayer(env, thiz);

364
    /* Create a media player playing environment */
Ludovic Fauvet's avatar
Ludovic Fauvet committed
365
    libvlc_media_player_t *mp = libvlc_media_player_new((libvlc_instance_t*)(intptr_t)instance);
Rafaël Carré's avatar
Rafaël Carré committed
366
    libvlc_media_player_set_video_title_display(mp, libvlc_position_disable, 0);
367
368
    jobject myJavaLibVLC = (*env)->NewGlobalRef(env, thiz);

369
370
371
372
    //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 )
373
    {
374
        libvlc_audio_set_callbacks(mp, aout_play, aout_pause, NULL, NULL, NULL,
Sébastien Toque's avatar
Sébastien Toque committed
375
376
                                   (void*) myJavaLibVLC);
        libvlc_audio_set_format_callbacks(mp, aout_open, aout_close);
377
378
    }

379
380
    /* Connect the event manager */
    libvlc_event_manager_t *ev = libvlc_media_player_event_manager(mp);
381
382
383
384
385
    static const libvlc_event_type_t mp_events[] = {
        libvlc_MediaPlayerPlaying,
        libvlc_MediaPlayerPaused,
        libvlc_MediaPlayerEndReached,
        libvlc_MediaPlayerStopped,
386
        libvlc_MediaPlayerVout,
387
388
        libvlc_MediaPlayerPositionChanged,
        libvlc_MediaPlayerEncounteredError
389
    };
390
    for(int i = 0; i < (sizeof(mp_events) / sizeof(*mp_events)); i++)
391
392
        libvlc_event_attach(ev, mp_events[i], vlc_event_callback, myVm);

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

396
397
398
399
    cls = (*env)->GetObjectClass(env, thiz);
    jmethodID methodID = (*env)->GetMethodID(env, cls, "applyEqualizer", "()V");
    (*env)->CallVoidMethod(env, thiz, methodID);

400
401
402
403
    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 */
404
405
406
407
408
409
410
411
412
413
    if (mediaOptions != NULL)
    {
        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);
        }
414
    }
415

416
417
    (*env)->ReleaseStringUTFChars(env, mrl, p_mrl);

418
419
420
421
422
423
424
425
    /* Connect the media event manager. */
    libvlc_event_manager_t *ev_media = libvlc_media_event_manager(p_md);
    static const libvlc_event_type_t mp_media_events[] = {
        libvlc_MediaParsedChanged
    };
    for(int i = 0; i < (sizeof(mp_media_events) / sizeof(*mp_media_events)); i++)
        libvlc_event_attach(ev_media, mp_media_events[i], vlc_event_callback, myVm);

426
427
    libvlc_media_player_set_media(mp, p_md);
    libvlc_media_player_play(mp);
428
429
}

430
jfloat Java_org_videolan_libvlc_LibVLC_getRate(JNIEnv *env, jobject thiz) {
Edward Wang's avatar
Edward Wang committed
431
432
433
434
435
436
437
    libvlc_media_player_t* mp = getMediaPlayer(env, thiz);
    if(mp)
        return libvlc_media_player_get_rate(mp);
    else
        return 1.00;
}

438
void Java_org_videolan_libvlc_LibVLC_setRate(JNIEnv *env, jobject thiz, jfloat rate) {
Edward Wang's avatar
Edward Wang committed
439
440
441
442
443
    libvlc_media_player_t* mp = getMediaPlayer(env, thiz);
    if(mp)
        libvlc_media_player_set_rate(mp, rate);
}

444
jboolean Java_org_videolan_libvlc_LibVLC_isPlaying(JNIEnv *env, jobject thiz)
445
{
446
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
447
    if (mp)
448
        return !!libvlc_media_player_is_playing(mp);
449
450
    else
        return 0;
451
452
}

453
jboolean Java_org_videolan_libvlc_LibVLC_isSeekable(JNIEnv *env, jobject thiz)
454
{
455
456
457
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return !!libvlc_media_player_is_seekable(mp);
458
    return 0;
459
460
}

461
void Java_org_videolan_libvlc_LibVLC_play(JNIEnv *env, jobject thiz)
462
{
463
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
464
    if (mp)
465
        libvlc_media_player_play(mp);
466
467
}

468
void Java_org_videolan_libvlc_LibVLC_pause(JNIEnv *env, jobject thiz)
469
{
470
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
471
    if (mp)
472
        libvlc_media_player_pause(mp);
473
474
}

475
void Java_org_videolan_libvlc_LibVLC_stop(JNIEnv *env, jobject thiz)
476
{
477
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
478
    if (mp)
479
        libvlc_media_player_stop(mp);
480
481
}

482
jint Java_org_videolan_libvlc_LibVLC_getVolume(JNIEnv *env, jobject thiz)
483
{
484
485
486
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return (jint) libvlc_audio_get_volume(mp);
487
488
489
    return -1;
}

490
jint Java_org_videolan_libvlc_LibVLC_setVolume(JNIEnv *env, jobject thiz, jint volume)
491
{
492
493
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
494
        //Returns 0 if the volume was set, -1 if it was out of range or error
495
        return (jint) libvlc_audio_set_volume(mp, (int) volume);
496
497
498
    return -1;
}

499
jlong Java_org_videolan_libvlc_LibVLC_getTime(JNIEnv *env, jobject thiz)
500
{
501
502
503
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return libvlc_media_player_get_time(mp);
504
505
506
    return -1;
}

507
void Java_org_videolan_libvlc_LibVLC_setTime(JNIEnv *env, jobject thiz, jlong time)
508
{
509
510
511
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        libvlc_media_player_set_time(mp, time);
512
513
}

514
jfloat Java_org_videolan_libvlc_LibVLC_getPosition(JNIEnv *env, jobject thiz)
515
{
516
517
518
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return (jfloat) libvlc_media_player_get_position(mp);
519
520
521
    return -1;
}

522
void Java_org_videolan_libvlc_LibVLC_setPosition(JNIEnv *env, jobject thiz, jfloat pos)
523
{
524
525
526
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        libvlc_media_player_set_position(mp, pos);
527
528
}

529
jlong Java_org_videolan_libvlc_LibVLC_getLength(JNIEnv *env, jobject thiz)
530
{
531
532
533
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return (jlong) libvlc_media_player_get_length(mp);
534
535
536
    return -1;
}

537
jstring Java_org_videolan_libvlc_LibVLC_version(JNIEnv* env, jobject thiz)
538
{
539
540
    return (*env)->NewStringUTF(env, libvlc_get_version());
}
541

542
jstring Java_org_videolan_libvlc_LibVLC_compiler(JNIEnv* env, jobject thiz)
543
544
545
546
{
    return (*env)->NewStringUTF(env, libvlc_get_compiler());
}

547
jstring Java_org_videolan_libvlc_LibVLC_changeset(JNIEnv* env, jobject thiz)
548
549
550
{
    return (*env)->NewStringUTF(env, libvlc_get_changeset());
}
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573

jint Java_org_videolan_libvlc_LibVLC_getTitle(JNIEnv *env, jobject thiz)
{
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return libvlc_media_player_get_title(mp);
    return -1;
}

void Java_org_videolan_libvlc_LibVLC_setTitle(JNIEnv *env, jobject thiz, jint title)
{
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        libvlc_media_player_set_title(mp, title);
}

jint Java_org_videolan_libvlc_LibVLC_getChapterCountForTitle(JNIEnv *env, jobject thiz, jint title)
{
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return libvlc_media_player_get_chapter_count_for_title(mp, title);
    return -1;
}
Edward Wang's avatar
Edward Wang committed
574
575
576
577
578
579
580
581

jint Java_org_videolan_libvlc_LibVLC_getTitleCount(JNIEnv *env, jobject thiz)
{
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return libvlc_media_player_get_title_count(mp);
    return -1;
}