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

43 44
#define VOUT_ANDROID_SURFACE 0
#define VOUT_OPENGLES2       1
45
#define VOUT_ANDROID_WINDOW  2
46

47
#define LOG_TAG "VLC/JNI/main"
48 49
#include "log.h"

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

56 57
#define VLC_JNI_VERSION JNI_VERSION_1_2

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

63 64 65 66 67 68 69 70 71 72 73 74
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);
    }
}

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

85 86 87 88 89 90 91 92 93
    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);
94
        }
95
    }
96
    return p_md;
97 98
}

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

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

115 116 117 118
/* 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
119
static JavaVM *myVm;
120

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

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

127
    bool isAttached = false;
128

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

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

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

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

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

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

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

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

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

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

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

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
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)
{
248
    return (*myVm)->GetEnv(myVm, (void **)env, VLC_JNI_VERSION) == JNI_OK ? 0 : -1;
249 250
}

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

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

261
    methodId = (*env)->GetMethodID(env, cls, "getVout", "()I");
262
    int vout = (*env)->CallIntMethod(env, thiz, methodId);
263

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

352
    (*env)->ReleaseStringUTFChars(env, chroma, chromastr);
353 354
    (*env)->ReleaseStringUTFChars(env, subsencoding, subsencodingstr);

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

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

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

    init_native_crash_handler(env, thiz);
366 367
}

368
void Java_org_videolan_libvlc_LibVLC_nativeDestroy(JNIEnv *env, jobject thiz)
369
{
370 371
    destroy_native_crash_handler(env);

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

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

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

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

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

399
    eventHandlerInstance = getEventHandlerReference(env, thiz, eventHandler);
400 401
}

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

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

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

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

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

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

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

452 453
    (*env)->ReleaseStringUTFChars(env, mrl, p_mrl);

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

462 463
    libvlc_media_player_set_media(mp, p_md);
    libvlc_media_player_play(mp);
464 465
}

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

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

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

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

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

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

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

518 519 520 521 522 523 524 525
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;
}

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

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

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

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

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

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

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

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

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

591
jstring Java_org_videolan_libvlc_LibVLC_changeset(JNIEnv* env, jobject thiz)
592 593 594
{
    return (*env)->NewStringUTF(env, libvlc_get_changeset());
}
595

596 597 598 599 600 601 602 603 604 605 606 607 608
jstring Java_org_videolan_libvlc_LibVLC_getMeta(JNIEnv *env, jobject thiz, int meta)
{
    libvlc_media_player_t *mp = getMediaPlayer(env, thiz);
    if (!mp)
        return NULL;

    libvlc_media_t *p_mp = libvlc_media_player_get_media(mp);
    if (!p_mp)
        return NULL;

    return (*env)->NewStringUTF(env, libvlc_media_get_meta(p_mp, meta));
}

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
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
631 632 633 634 635 636 637 638

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
639 640 641 642 643 644 645 646 647

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);
}