libvlcjni.c 21.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
#define LOG_TAG "VLC/JNI/main"
47 48
#include "log.h"

49 50 51 52 53 54
#ifdef HAVE_IOMX_DR
#define NO_IOMX_DR "--no-omxil-dr"
#else
#define NO_IOMX_DR ""
#endif

55 56
#define VLC_JNI_VERSION JNI_VERSION_1_2

57 58 59 60 61
#define THREAD_NAME "libvlcjni"
int jni_attach_thread(JNIEnv **env, const char *thread_name);
void jni_detach_thread();
int jni_get_env(JNIEnv **env);

62 63 64 65 66 67 68 69 70 71 72 73
static void add_media_options(libvlc_media_t *p_md, JNIEnv *env, jobjectArray mediaOptions)
{
    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);
    }
}

74
libvlc_media_t *new_media(jlong instance, JNIEnv *env, jobject thiz, jstring fileLocation, bool noOmx, bool noVideo)
75
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
76
    libvlc_instance_t *libvlc = (libvlc_instance_t*)(intptr_t)instance;
77
    jboolean isCopy;
78 79 80
    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);
81 82 83
    if (!p_md)
        return NULL;

84 85 86 87 88 89 90 91 92
    jclass cls = (*env)->GetObjectClass(env, thiz);
    jmethodID methodId = (*env)->GetMethodID(env, cls, "getMediaOptions", "(ZZ)[Ljava/lang/String;");
    if (methodId != NULL)
    {
        jobjectArray mediaOptions = (*env)->CallObjectMethod(env, thiz, methodId, noOmx, noVideo);
        if (mediaOptions != NULL)
        {
            add_media_options(p_md, env, mediaOptions);
            (*env)->DeleteLocalRef(env, mediaOptions);
93
        }
94
    }
95
    return p_md;
96 97
}

98
libvlc_media_player_t *getMediaPlayer(JNIEnv *env, jobject thiz)
99
{
Ludovic Fauvet's avatar
Ludovic Fauvet committed
100
    return (libvlc_media_player_t*)(intptr_t)getLong(env, thiz, "mInternalMediaPlayerInstance");
101 102
}

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

114 115 116 117
/* 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
 */
Thomas Guillem's avatar
Thomas Guillem committed
118
static JavaVM *myVm;
119

120
static jobject eventHandlerInstance = NULL;
121 122 123 124 125

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

126
    bool isAttached = false;
127

128
    if (eventHandlerInstance == NULL)
Sébastien Toque's avatar
Sébastien Toque committed
129
        return;
130

131 132
    if (jni_get_env(&env) < 0) {
        if (jni_attach_thread(&env, THREAD_NAME) < 0)
133
            return;
134
        isAttached = true;
135 136
    }

137 138 139 140 141 142 143 144 145 146
    /* 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" );
147
    jmethodID putLong = (*env)->GetMethodID(env, clsBundle, "putLong", "(Ljava/lang/String;J)V" );
148
    jmethodID putFloat = (*env)->GetMethodID(env, clsBundle, "putFloat", "(Ljava/lang/String;F)V" );
149 150
    jmethodID putString = (*env)->GetMethodID(env, clsBundle, "putString", "(Ljava/lang/String;Ljava/lang/String;)V" );

151
    if (ev->type == libvlc_MediaPlayerPositionChanged) {
152 153 154
        jstring sData = (*env)->NewStringUTF(env, "data");
        (*env)->CallVoidMethod(env, bundle, putFloat, sData, ev->u.media_player_position_changed.new_position);
        (*env)->DeleteLocalRef(env, sData);
155 156
    } else if (ev->type == libvlc_MediaPlayerTimeChanged) {
        jstring sData = (*env)->NewStringUTF(env, "data");
157
        (*env)->CallVoidMethod(env, bundle, putLong, sData, ev->u.media_player_time_changed.new_time);
158
        (*env)->DeleteLocalRef(env, sData);
159
    } else if(ev->type == libvlc_MediaPlayerVout) {
160 161 162 163
        /* 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);
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    } 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);
187 188
    }

189
    /* Get the object class */
190
    jclass cls = (*env)->GetObjectClass(env, eventHandlerInstance);
191
    if (!cls) {
192
        LOGE("EventHandler: failed to get class reference");
193
        goto end;
194 195 196
    }

    /* Find the callback ID */
197
    jmethodID methodID = (*env)->GetMethodID(env, cls, "callback", "(ILandroid/os/Bundle;)V");
198
    if (methodID) {
199
        (*env)->CallVoidMethod(env, eventHandlerInstance, methodID, ev->type, bundle);
200
    } else {
201
        LOGE("EventHandler: failed to get the callback method");
202 203
    }

204
end:
Edward Wang's avatar
Edward Wang committed
205
    (*env)->DeleteLocalRef(env, bundle);
206
    if (isAttached)
207
        jni_detach_thread();
208 209
}

210
jint JNI_OnLoad(JavaVM *vm, void *reserved)
211
{
212 213 214
    // Keep a reference on the Java VM.
    myVm = vm;

215
    pthread_mutex_init(&vout_android_lock, NULL);
216
    pthread_cond_init(&vout_android_surf_attached, NULL);
217

218
    LOGD("JNI interface loaded.");
219
    return VLC_JNI_VERSION;
220 221
}

222 223
void JNI_OnUnload(JavaVM* vm, void* reserved) {
    pthread_mutex_destroy(&vout_android_lock);
224
    pthread_cond_destroy(&vout_android_surf_attached);
225 226
}

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
int jni_attach_thread(JNIEnv **env, const char *thread_name)
{
    JavaVMAttachArgs args;
    jint result;

    args.version = VLC_JNI_VERSION;
    args.name = thread_name;
    args.group = NULL;

    result = (*myVm)->AttachCurrentThread(myVm, env, &args);
    return result == JNI_OK ? 0 : -1;
}

void jni_detach_thread()
{
    (*myVm)->DetachCurrentThread(myVm);
}

int jni_get_env(JNIEnv **env)
{
247
    return (*myVm)->GetEnv(myVm, (void **)env, VLC_JNI_VERSION) == JNI_OK ? 0 : -1;
248 249
}

Rafaël Carré's avatar
Rafaël Carré committed
250 251
// FIXME: use atomics
static bool verbosity;
252

253
void Java_org_videolan_libvlc_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
254
{
255 256 257 258 259
    //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;

260 261 262
    methodId = (*env)->GetMethodID(env, cls, "getVout", "()I");
    bool use_opengles2 = (*env)->CallIntMethod(env, thiz, methodId) == VOUT_OPENGLES2;

263 264 265
    methodId = (*env)->GetMethodID(env, cls, "timeStretchingEnabled", "()Z");
    bool enable_time_stretch = (*env)->CallBooleanMethod(env, thiz, methodId);

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

Edward Wang's avatar
Edward Wang committed
269 270
    methodId = (*env)->GetMethodID(env, cls, "getDeblocking", "()I");
    int deblocking = (*env)->CallIntMethod(env, thiz, methodId);
Rafaël Carré's avatar
Rafaël Carré committed
271 272
    char deblockstr[2];
    snprintf(deblockstr, sizeof(deblockstr), "%d", deblocking);
Edward Wang's avatar
Edward Wang committed
273
    LOGD("Using deblocking level %d", deblocking);
274

Edward Wang's avatar
Edward Wang committed
275 276
    methodId = (*env)->GetMethodID(env, cls, "getNetworkCaching", "()I");
    int networkCaching = (*env)->CallIntMethod(env, thiz, methodId);
Rafaël Carré's avatar
Rafaël Carré committed
277
    char networkCachingstr[25];
Edward Wang's avatar
Edward Wang committed
278
    if(networkCaching > 0) {
Rafaël Carré's avatar
Rafaël Carré committed
279
        snprintf(networkCachingstr, sizeof(networkCachingstr), "--network-caching=%d", networkCaching);
Edward Wang's avatar
Edward Wang committed
280 281 282
        LOGD("Using network caching of %d ms", networkCaching);
    }

283 284 285
    methodId = (*env)->GetMethodID(env, cls, "getHttpReconnect", "()Z");
    bool enable_http_reconnect = (*env)->CallBooleanMethod(env, thiz, methodId);

286 287 288 289
    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
290

291 292 293
    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
294
    LOGD("Subtitle encoding set to \"%s\"", subsencodingstr);
295

296 297 298
    methodId = (*env)->GetMethodID(env, cls, "isVerboseMode", "()Z");
    verbosity = (*env)->CallBooleanMethod(env, thiz, methodId);

299 300
    methodId = (*env)->GetMethodID(env, cls, "isDirectRendering", "()Z");
    bool direct_rendering = (*env)->CallBooleanMethod(env, thiz, methodId);
301
    /* With the MediaCodec opaque mode we cannot use the OpenGL ES vout. */
302
    if (direct_rendering)
303
        use_opengles2 = false;
304

flx42's avatar
flx42 committed
305 306
    methodId = (*env)->GetMethodID(env, cls, "getCachePath", "()Ljava/lang/String;");
    jstring cachePath = (*env)->CallObjectMethod(env, thiz, methodId);
307 308 309 310 311
    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
312

313
    /* Don't add any invalid options, otherwise it causes LibVLC to crash */
314
    const char *argv[] = {
315
        /* CPU intensive plugin, setting for slow devices */
316
        enable_time_stretch ? "--audio-time-stretch" : "--no-audio-time-stretch",
317 318

        /* avcodec speed settings for slow devices */
319
        //"--avcodec-fast", // non-spec-compliant speedup tricks
Edward Wang's avatar
Edward Wang committed
320
        "--avcodec-skiploopfilter", deblockstr,
321 322
        "--avcodec-skip-frame", enable_frame_skip ? "2" : "0",
        "--avcodec-skip-idct", enable_frame_skip ? "2" : "0",
323 324 325 326

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

327 328 329
        /* Enable statistics */
        "--stats",

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

        /* Android audio API is a mess */
334
        use_opensles ? "--aout=opensles" : "--aout=android_audiotrack",
335 336

        /* Android video API is a mess */
337
        use_opengles2 ? "--vout=gles2" : "--vout=androidsurface",
338
        "--androidsurface-chroma", chromastr != NULL && chromastr[0] != 0 ? chromastr : "RV32",
339
        /* XXX: we can't recover from direct rendering failure */
340 341
        direct_rendering ? "" : "--no-mediacodec-dr",
        direct_rendering ? "" : NO_IOMX_DR,
342 343 344

        /* Reconnect on lost HTTP streams, e.g. network change */
        enable_http_reconnect ? "--http-reconnect" : "",
345
    };
346
    libvlc_instance_t *instance = libvlc_new(sizeof(argv) / sizeof(*argv), argv);
347

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

350
    (*env)->ReleaseStringUTFChars(env, chroma, chromastr);
351 352
    (*env)->ReleaseStringUTFChars(env, subsencoding, subsencodingstr);

353 354
    if (!instance)
    {
355
        jclass exc = (*env)->FindClass(env, "org/videolan/libvlc/LibVlcException");
356 357 358 359
        (*env)->ThrowNew(env, exc, "Unable to instantiate LibVLC");
    }

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

Rafaël Carré's avatar
Rafaël Carré committed
361
    libvlc_log_set(instance, debug_log, &verbosity);
362 363

    init_native_crash_handler(env, thiz);
364 365
}

366
void Java_org_videolan_libvlc_LibVLC_nativeDestroy(JNIEnv *env, jobject thiz)
367
{
368 369
    destroy_native_crash_handler(env);

370
    releaseMediaPlayer(env, thiz);
Edward Wang's avatar
Edward Wang committed
371
    jlong libVlcInstance = getLong(env, thiz, "mLibVlcInstance");
372 373 374
    if (!libVlcInstance)
        return; // Already destroyed

Ludovic Fauvet's avatar
Ludovic Fauvet committed
375
    libvlc_instance_t *instance = (libvlc_instance_t*)(intptr_t) libVlcInstance;
Rafaël Carré's avatar
Rafaël Carré committed
376
    libvlc_log_unset(instance);
377 378
    libvlc_release(instance);

Edward Wang's avatar
Edward Wang committed
379
    setLong(env, thiz, "mLibVlcInstance", 0);
380 381
}

382
void Java_org_videolan_libvlc_LibVLC_detachEventHandler(JNIEnv *env, jobject thiz)
383
{
384 385 386
    if (eventHandlerInstance != NULL) {
        (*env)->DeleteGlobalRef(env, eventHandlerInstance);
        eventHandlerInstance = NULL;
387 388 389
    }
}

390
void Java_org_videolan_libvlc_LibVLC_setEventHandler(JNIEnv *env, jobject thiz, jobject eventHandler)
391
{
392 393 394
    if (eventHandlerInstance != NULL) {
        (*env)->DeleteGlobalRef(env, eventHandlerInstance);
        eventHandlerInstance = NULL;
395 396
    }

397
    eventHandlerInstance = getEventHandlerReference(env, thiz, eventHandler);
398 399
}

Edward Wang's avatar
Edward Wang committed
400
void Java_org_videolan_libvlc_LibVLC_playMRL(JNIEnv *env, jobject thiz, jlong instance,
401 402
                                             jstring mrl, jobjectArray mediaOptions)
{
403 404 405
    /* Release previous media player, if any */
    releaseMediaPlayer(env, thiz);

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

411 412 413 414
    //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 )
415
    {
416
        libvlc_audio_set_callbacks(mp, aout_play, aout_pause, NULL, NULL, NULL,
Sébastien Toque's avatar
Sébastien Toque committed
417 418
                                   (void*) myJavaLibVLC);
        libvlc_audio_set_format_callbacks(mp, aout_open, aout_close);
419 420
    }

421 422
    /* Connect the event manager */
    libvlc_event_manager_t *ev = libvlc_media_player_event_manager(mp);
423 424 425 426 427
    static const libvlc_event_type_t mp_events[] = {
        libvlc_MediaPlayerPlaying,
        libvlc_MediaPlayerPaused,
        libvlc_MediaPlayerEndReached,
        libvlc_MediaPlayerStopped,
428
        libvlc_MediaPlayerVout,
429
        libvlc_MediaPlayerPositionChanged,
430
        libvlc_MediaPlayerTimeChanged,
431
        libvlc_MediaPlayerEncounteredError
432
    };
433
    for(int i = 0; i < (sizeof(mp_events) / sizeof(*mp_events)); i++)
434 435
        libvlc_event_attach(ev, mp_events[i], vlc_event_callback, myVm);

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

439 440 441 442
    cls = (*env)->GetObjectClass(env, thiz);
    jmethodID methodID = (*env)->GetMethodID(env, cls, "applyEqualizer", "()V");
    (*env)->CallVoidMethod(env, thiz, methodID);

443 444 445 446
    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 */
447
    if (mediaOptions != NULL)
448
        add_media_options(p_md, env, mediaOptions);
449

450 451
    (*env)->ReleaseStringUTFChars(env, mrl, p_mrl);

452 453 454 455 456 457 458 459
    /* 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);

460 461
    libvlc_media_player_set_media(mp, p_md);
    libvlc_media_player_play(mp);
462 463
}

464
jfloat Java_org_videolan_libvlc_LibVLC_getRate(JNIEnv *env, jobject thiz) {
Edward Wang's avatar
Edward Wang committed
465 466 467 468 469 470 471
    libvlc_media_player_t* mp = getMediaPlayer(env, thiz);
    if(mp)
        return libvlc_media_player_get_rate(mp);
    else
        return 1.00;
}

472
void Java_org_videolan_libvlc_LibVLC_setRate(JNIEnv *env, jobject thiz, jfloat rate) {
Edward Wang's avatar
Edward Wang committed
473 474 475 476 477
    libvlc_media_player_t* mp = getMediaPlayer(env, thiz);
    if(mp)
        libvlc_media_player_set_rate(mp, rate);
}

478
jboolean Java_org_videolan_libvlc_LibVLC_isPlaying(JNIEnv *env, jobject thiz)
479
{
480
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
481
    if (mp)
482
        return !!libvlc_media_player_is_playing(mp);
483 484
    else
        return 0;
485 486
}

487
jboolean Java_org_videolan_libvlc_LibVLC_isSeekable(JNIEnv *env, jobject thiz)
488
{
489 490 491
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return !!libvlc_media_player_is_seekable(mp);
492
    return 0;
493 494
}

495
void Java_org_videolan_libvlc_LibVLC_play(JNIEnv *env, jobject thiz)
496
{
497
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
498
    if (mp)
499
        libvlc_media_player_play(mp);
500 501
}

502
void Java_org_videolan_libvlc_LibVLC_pause(JNIEnv *env, jobject thiz)
503
{
504
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
505
    if (mp)
506
        libvlc_media_player_pause(mp);
507 508
}

509
void Java_org_videolan_libvlc_LibVLC_stop(JNIEnv *env, jobject thiz)
510
{
511
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
512
    if (mp)
513
        libvlc_media_player_stop(mp);
514 515
}

516 517 518 519 520 521 522 523
jint Java_org_videolan_libvlc_LibVLC_getPlayerState(JNIEnv *env, jobject thiz)
{
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return (jint) libvlc_media_player_get_state(mp);
    return -1;
}

524
jint Java_org_videolan_libvlc_LibVLC_getVolume(JNIEnv *env, jobject thiz)
525
{
526 527 528
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return (jint) libvlc_audio_get_volume(mp);
529 530 531
    return -1;
}

532
jint Java_org_videolan_libvlc_LibVLC_setVolume(JNIEnv *env, jobject thiz, jint volume)
533
{
534 535
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
536
        //Returns 0 if the volume was set, -1 if it was out of range or error
537
        return (jint) libvlc_audio_set_volume(mp, (int) volume);
538 539 540
    return -1;
}

541
jlong Java_org_videolan_libvlc_LibVLC_getTime(JNIEnv *env, jobject thiz)
542
{
543 544 545
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return libvlc_media_player_get_time(mp);
546 547 548
    return -1;
}

549
void Java_org_videolan_libvlc_LibVLC_setTime(JNIEnv *env, jobject thiz, jlong time)
550
{
551 552 553
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        libvlc_media_player_set_time(mp, time);
554 555
}

556
jfloat Java_org_videolan_libvlc_LibVLC_getPosition(JNIEnv *env, jobject thiz)
557
{
558 559 560
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return (jfloat) libvlc_media_player_get_position(mp);
561 562 563
    return -1;
}

564
void Java_org_videolan_libvlc_LibVLC_setPosition(JNIEnv *env, jobject thiz, jfloat pos)
565
{
566 567 568
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        libvlc_media_player_set_position(mp, pos);
569 570
}

571
jlong Java_org_videolan_libvlc_LibVLC_getLength(JNIEnv *env, jobject thiz)
572
{
573 574 575
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        return (jlong) libvlc_media_player_get_length(mp);
576 577 578
    return -1;
}

579
jstring Java_org_videolan_libvlc_LibVLC_version(JNIEnv* env, jobject thiz)
580
{
581 582
    return (*env)->NewStringUTF(env, libvlc_get_version());
}
583

584
jstring Java_org_videolan_libvlc_LibVLC_compiler(JNIEnv* env, jobject thiz)
585 586 587 588
{
    return (*env)->NewStringUTF(env, libvlc_get_compiler());
}

589
jstring Java_org_videolan_libvlc_LibVLC_changeset(JNIEnv* env, jobject thiz)
590 591 592
{
    return (*env)->NewStringUTF(env, libvlc_get_changeset());
}
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615

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
616 617 618 619 620 621 622 623

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;
}
Geoffrey Métais's avatar
Geoffrey Métais committed
624 625 626 627 628 629 630 631 632

void Java_org_videolan_libvlc_LibVLC_playerNavigate(JNIEnv *env, jobject thiz, jint navigate)
{
    unsigned nav = navigate;
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (mp)
        libvlc_media_player_navigate(mp, (unsigned) nav);
}