libvlcjni-mediaplayer.c 34.2 KB
Newer Older
1
/*****************************************************************************
2
 * libvlcjni-mediaplayer.c
3
 *****************************************************************************
4 5 6
 * Copyright © 2010-2015 VLC authors and VideoLAN
 *
 * Authors:     Jean-Baptiste Kempf
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
 * (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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * 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.
 *****************************************************************************/

#include <pthread.h>

25
#include "libvlcjni-vlcobject.h"
26 27 28 29

#define THREAD_NAME "libvlcjni"
JNIEnv *jni_get_env(const char *name);

30
static const libvlc_event_type_t mp_events[] = {
31
    libvlc_MediaPlayerMediaChanged,
32
    libvlc_MediaPlayerOpening,
33
    libvlc_MediaPlayerBuffering,
34 35 36 37 38 39 40 41 42 43
    libvlc_MediaPlayerPlaying,
    libvlc_MediaPlayerPaused,
    libvlc_MediaPlayerStopped,
    libvlc_MediaPlayerEndReached,
    libvlc_MediaPlayerEncounteredError,
    libvlc_MediaPlayerTimeChanged,
    libvlc_MediaPlayerPositionChanged,
    libvlc_MediaPlayerVout,
    libvlc_MediaPlayerESAdded,
    libvlc_MediaPlayerESDeleted,
44
    libvlc_MediaPlayerESSelected,
45 46
    libvlc_MediaPlayerSeekableChanged,
    libvlc_MediaPlayerPausableChanged,
47
    libvlc_MediaPlayerLengthChanged,
48 49
    -1,
};
50

51 52 53
struct vlcjni_object_sys
{
    jobject jwindow;
54
    libvlc_video_viewpoint_t *p_vp;
55 56
};

57 58 59 60 61 62 63
static libvlc_equalizer_t *
Equalizer_getInstance(JNIEnv *env, jobject thiz)
{
    intptr_t i_ptr = (intptr_t)
        (*env)->GetLongField(env, thiz,
                             fields.MediaPlayer.Equalizer.mInstanceID);
    if (!i_ptr)
64 65
        throw_Exception(env, VLCJNI_EX_ILLEGAL_STATE,
                        "can't get Equalizer instance");
66 67 68 69 70 71 72 73 74 75 76
    return (libvlc_equalizer_t*) i_ptr;
}

static void
VLCJniObject_setInstance(JNIEnv *env, jobject thiz, libvlc_equalizer_t *p_eq)
{
    (*env)->SetLongField(env, thiz,
                         fields.MediaPlayer.Equalizer.mInstanceID,
                         (jlong)(intptr_t)p_eq);
}

77 78 79
static bool
MediaPlayer_event_cb(vlcjni_object *p_obj, const libvlc_event_t *p_ev,
                     java_event *p_java_event)
80
{
81 82
    switch (p_ev->type)
    {
83
        case libvlc_MediaPlayerBuffering:
84
            p_java_event->argf1 = p_ev->u.media_player_buffering.new_cache;
85
            break;
86
        case libvlc_MediaPlayerPositionChanged:
87
            p_java_event->argf1 = p_ev->u.media_player_position_changed.new_position;
88 89 90 91 92 93 94 95 96
            break;
        case libvlc_MediaPlayerTimeChanged:
            p_java_event->arg1 = p_ev->u.media_player_time_changed.new_time;
            break;
        case libvlc_MediaPlayerVout:
            p_java_event->arg1 = p_ev->u.media_player_vout.new_count;
            break;
        case libvlc_MediaPlayerESAdded:
        case libvlc_MediaPlayerESDeleted:
97
        case libvlc_MediaPlayerESSelected:
98
            p_java_event->arg1 = p_ev->u.media_player_es_changed.i_type;
99
            p_java_event->arg2 = p_ev->u.media_player_es_changed.i_id;
100
            break;
101 102 103 104 105 106
        case libvlc_MediaPlayerSeekableChanged:
            p_java_event->arg1 = p_ev->u.media_player_seekable_changed.new_seekable;
            break;
        case libvlc_MediaPlayerPausableChanged:
            p_java_event->arg1 = p_ev->u.media_player_pausable_changed.new_pausable;
            break;
107 108 109
        case libvlc_MediaPlayerLengthChanged:
            p_java_event->arg1 = p_ev->u.media_player_length_changed.new_length;
            break;
110
    }
111 112
    p_java_event->type = p_ev->type;
    return true;
113 114
}

115
static void
116 117
MediaPlayer_newCommon(JNIEnv *env, jobject thiz, vlcjni_object *p_obj,
                      jobject jwindow)
118
{
119 120 121 122 123
    p_obj->p_sys = calloc(1, sizeof(vlcjni_object_sys));

    if (!p_obj->u.p_mp || !p_obj->p_sys)
    {
        VLCJniObject_release(env, thiz, p_obj);
124 125 126
        throw_Exception(env,
                        !p_obj->u.p_mp ? VLCJNI_EX_ILLEGAL_STATE : VLCJNI_EX_OUT_OF_MEMORY,
                        "can't create MediaPlayer instance");
127 128 129 130
        return;
    }
    p_obj->p_sys->jwindow = (*env)->NewGlobalRef(env, jwindow);
    if (!p_obj->p_sys->jwindow)
131 132
    {
        VLCJniObject_release(env, thiz, p_obj);
133 134
        throw_Exception(env, VLCJNI_EX_ILLEGAL_STATE,
                             "can't create MediaPlayer instance");
135 136
        return;
    }
137
    libvlc_media_player_set_android_context(p_obj->u.p_mp, p_obj->p_sys->jwindow);
138

139
    VLCJniObject_attachEvents(p_obj, MediaPlayer_event_cb,
140 141
                              libvlc_media_player_event_manager(p_obj->u.p_mp),
                              mp_events);
142
}
143

144

145
void
146 147
Java_org_videolan_libvlc_MediaPlayer_nativeNewFromLibVlc(JNIEnv *env,
                                                         jobject thiz,
148 149
                                                         jobject libvlc,
                                                         jobject jwindow)
150
{
151
    vlcjni_object *p_obj = VLCJniObject_newFromJavaLibVlc(env, thiz, libvlc);
152 153
    if (!p_obj)
        return;
154

155 156
    /* Create a media player playing environment */
    p_obj->u.p_mp = libvlc_media_player_new(p_obj->p_libvlc);
157
    MediaPlayer_newCommon(env, thiz, p_obj, jwindow);
158
}
159

160 161 162
void
Java_org_videolan_libvlc_MediaPlayer_nativeNewFromMedia(JNIEnv *env,
                                                        jobject thiz,
163 164
                                                        jobject jmedia,
                                                        jobject jwindow)
165 166 167
{
    vlcjni_object *p_obj;
    vlcjni_object *p_m_obj = VLCJniObject_getInstance(env, jmedia);
168

169 170
    if (!p_m_obj)
        return;
171

172 173 174 175
    p_obj = VLCJniObject_newFromLibVlc(env, thiz, p_m_obj->p_libvlc);
    if (!p_obj)
        return;
    p_obj->u.p_mp = libvlc_media_player_new_from_media(p_m_obj->u.p_m);
176
    MediaPlayer_newCommon(env, thiz, p_obj, jwindow);
177 178
}

179 180 181
void
Java_org_videolan_libvlc_MediaPlayer_nativeRelease(JNIEnv *env, jobject thiz)
{
182 183 184 185
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;
186 187 188

    libvlc_media_player_release(p_obj->u.p_mp);

189 190 191
    if (p_obj->p_sys && p_obj->p_sys->jwindow)
        (*env)->DeleteGlobalRef(env, p_obj->p_sys->jwindow);

192
    free(p_obj->p_sys->p_vp);
193 194
    free(p_obj->p_sys);

195
    VLCJniObject_release(env, thiz, p_obj);
196 197
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
void
Java_org_videolan_libvlc_MediaPlayer_nativeSetMedia(JNIEnv *env,
                                                    jobject thiz,
                                                    jobject jmedia)
{
    libvlc_media_t *p_m = NULL;
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;

    if (jmedia)
    {
        vlcjni_object *p_m_obj = VLCJniObject_getInstance(env, jmedia);

        if (!p_m_obj)
            return;
        p_m = p_m_obj->u.p_m;
    }

    libvlc_media_player_set_media(p_obj->u.p_mp, p_m);
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
jint
Java_org_videolan_libvlc_MediaPlayer_nativeSetRenderer(JNIEnv *env,
                                                       jobject thiz,
                                                       jobject jrenderer)
{
    libvlc_renderer_item_t *p_m = NULL;
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
    if (!p_obj)
        return -1;
    if (jrenderer)
    {
        vlcjni_object *p_m_obj = VLCJniObject_getInstance(env, jrenderer);

        if (!p_m_obj)
            return -1;
        p_m = p_m_obj->u.p_r;
    }
    return libvlc_media_player_set_renderer(p_obj->u.p_mp, p_m);
}

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
void
Java_org_videolan_libvlc_MediaPlayer_nativeSetVideoTitleDisplay(JNIEnv *env,
                                                                jobject thiz,
                                                                jint jposition,
                                                                jint jtimeout)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;

    libvlc_media_player_set_video_title_display(p_obj->u.p_mp, jposition,
                                                jtimeout);
}

256 257 258
jfloat
Java_org_videolan_libvlc_MediaPlayer_getRate(JNIEnv *env, jobject thiz)
{
259 260 261 262
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return 0.0f;
263 264

    return libvlc_media_player_get_rate(p_obj->u.p_mp);
265 266
}

267 268 269
void
Java_org_videolan_libvlc_MediaPlayer_setRate(JNIEnv *env, jobject thiz,
                                                  jfloat rate)
270
{
271 272 273 274
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;
275 276

    libvlc_media_player_set_rate(p_obj->u.p_mp, rate);
277 278
}

279 280
jboolean
Java_org_videolan_libvlc_MediaPlayer_isPlaying(JNIEnv *env, jobject thiz)
281
{
282 283 284 285
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return false;
286 287

    return !!libvlc_media_player_is_playing(p_obj->u.p_mp);
288 289
}

290 291
jboolean
Java_org_videolan_libvlc_MediaPlayer_isSeekable(JNIEnv *env, jobject thiz)
292
{
293 294 295 296
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return false;
297 298

    return !!libvlc_media_player_is_seekable(p_obj->u.p_mp);
299 300
}

301
void
302
Java_org_videolan_libvlc_MediaPlayer_nativePlay(JNIEnv *env, jobject thiz)
303
{
304 305 306 307
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;
308

309
    libvlc_media_player_play(p_obj->u.p_mp);
310 311 312
}

void
313
Java_org_videolan_libvlc_MediaPlayer_nativeStop(JNIEnv *env, jobject thiz)
314
{
315 316 317 318
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;
319 320

    libvlc_media_player_stop(p_obj->u.p_mp);
321 322
}

323 324 325 326 327 328 329 330
void
Java_org_videolan_libvlc_MediaPlayer_pause(JNIEnv *env, jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;

331
    libvlc_media_player_set_pause(p_obj->u.p_mp, 1);
332 333
}

334 335
jint
Java_org_videolan_libvlc_MediaPlayer_getPlayerState(JNIEnv *env, jobject thiz)
336
{
337 338 339 340
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return -1;
341 342

    return (jint) libvlc_media_player_get_state(p_obj->u.p_mp);
343 344
}

345 346
jint
Java_org_videolan_libvlc_MediaPlayer_getVolume(JNIEnv *env, jobject thiz)
347
{
348 349 350 351
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return -1;
352 353

    return (jint) libvlc_audio_get_volume(p_obj->u.p_mp);
354 355
}

356 357 358 359
/* Returns 0 if the volume was set, -1 if it was out of range or error */
jint
Java_org_videolan_libvlc_MediaPlayer_setVolume(JNIEnv *env, jobject thiz,
                                               jint volume)
360
{
361 362 363 364
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return -1;
365 366

    return (jint) libvlc_audio_set_volume(p_obj->u.p_mp, (int) volume);
367 368
}

369 370
jlong
Java_org_videolan_libvlc_MediaPlayer_getTime(JNIEnv *env, jobject thiz)
371
{
372 373 374 375
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return -1;
376 377

    return libvlc_media_player_get_time(p_obj->u.p_mp);
378 379
}

380 381 382
void
Java_org_videolan_libvlc_MediaPlayer_setTime(JNIEnv *env, jobject thiz,
                                             jlong time)
383
{
384 385 386 387
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;
388

389
#if defined(LIBVLC_VERSION_MAJOR) && LIBVLC_VERSION_MAJOR < 4
390
    libvlc_media_player_set_time(p_obj->u.p_mp, time);
391 392 393
#else
    libvlc_media_player_set_time(p_obj->u.p_mp, time, false);
#endif
394 395
}

396 397
jfloat
Java_org_videolan_libvlc_MediaPlayer_getPosition(JNIEnv *env, jobject thiz)
398
{
399 400 401 402
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return -1;
403 404

    return (jfloat) libvlc_media_player_get_position(p_obj->u.p_mp);
405 406
}

407 408 409
void
Java_org_videolan_libvlc_MediaPlayer_setPosition(JNIEnv *env, jobject thiz,
                                                 jfloat pos)
410
{
411 412 413 414
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;
415

416
#if defined(LIBVLC_VERSION_MAJOR) && LIBVLC_VERSION_MAJOR < 4
417
    libvlc_media_player_set_position(p_obj->u.p_mp, pos);
418 419 420 421
#else
    libvlc_media_player_set_position(p_obj->u.p_mp, pos, false);
#endif

422 423
}

424 425
jlong
Java_org_videolan_libvlc_MediaPlayer_getLength(JNIEnv *env, jobject thiz)
426
{
427 428 429 430
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return -1;
431 432

    return (jlong) libvlc_media_player_get_length(p_obj->u.p_mp);
433 434
}

435 436
jint
Java_org_videolan_libvlc_MediaPlayer_getTitle(JNIEnv *env, jobject thiz)
437
{
438 439 440 441
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return -1;
442 443

    return libvlc_media_player_get_title(p_obj->u.p_mp);
444 445
}

446 447 448
void
Java_org_videolan_libvlc_MediaPlayer_setTitle(JNIEnv *env, jobject thiz,
                                              jint title)
449
{
450 451 452 453
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;
454 455

    libvlc_media_player_set_title(p_obj->u.p_mp, title);
456 457
}

458 459
jint
Java_org_videolan_libvlc_MediaPlayer_getChapter(JNIEnv *env, jobject thiz)
460
{
461 462 463 464
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return -1;
465 466

    return libvlc_media_player_get_chapter(p_obj->u.p_mp);
467 468
}

469 470 471
void
Java_org_videolan_libvlc_MediaPlayer_setChapter(JNIEnv *env, jobject thiz,
                                                jint chapter)
472
{
473 474 475 476
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;
477 478

    libvlc_media_player_set_chapter(p_obj->u.p_mp, chapter);
479 480
}

481 482
void
Java_org_videolan_libvlc_MediaPlayer_previousChapter(JNIEnv *env, jobject thiz)
483
{
484 485 486 487
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;
488 489

    libvlc_media_player_previous_chapter(p_obj->u.p_mp);
490 491
}

492 493
void
Java_org_videolan_libvlc_MediaPlayer_nextChapter(JNIEnv *env, jobject thiz)
494
{
495 496 497 498
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;
499 500

    libvlc_media_player_next_chapter(p_obj->u.p_mp);
501 502
}

503
void
504
Java_org_videolan_libvlc_MediaPlayer_navigate(JNIEnv *env, jobject thiz,
505
                                                    jint navigate)
506
{
507 508 509 510
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;
511 512

    libvlc_media_player_navigate(p_obj->u.p_mp, (unsigned) navigate);
513
}
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

jboolean
Java_org_videolan_libvlc_MediaPlayer_nativeSetAudioOutput(JNIEnv *env,
                                                          jobject thiz,
                                                          jstring jaout)
{
    const char* psz_aout;
    int i_ret;
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return false;

    if (!jaout || !(psz_aout = (*env)->GetStringUTFChars(env, jaout, 0)))
    {
529
        throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "aout invalid");
530 531 532 533 534 535 536 537
        return false;
    }

    i_ret = libvlc_audio_output_set(p_obj->u.p_mp, psz_aout);
    (*env)->ReleaseStringUTFChars(env, jaout, psz_aout);

    return i_ret == 0 ? true : false;
}
538

539 540 541 542 543 544 545 546 547 548 549 550 551 552
jboolean
Java_org_videolan_libvlc_MediaPlayer_nativeSetAudioOutputDevice(JNIEnv *env,
                                                                jobject thiz,
                                                                jstring jid)
{
    const char* psz_id;
    int i_ret;
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return false;

    if (!jid || !(psz_id = (*env)->GetStringUTFChars(env, jid, 0)))
    {
553
        throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "aout invalid");
554 555 556 557 558 559 560 561
        return false;
    }

    libvlc_audio_output_device_set(p_obj->u.p_mp, NULL, psz_id);
    (*env)->ReleaseStringUTFChars(env, jid, psz_id);
    return true;
}

562 563 564 565 566 567 568 569 570 571 572
static jobject
mediaplayer_title_to_object(JNIEnv *env, libvlc_title_description_t *p_title)
{
    jstring jname = NULL;

    if (!p_title)
        return NULL;

    if (p_title->psz_name)
        jname = (*env)->NewStringUTF(env, p_title->psz_name);

573
    jobject jobj = (*env)->CallStaticObjectMethod(env, fields.MediaPlayer.clazz,
574 575 576
                        fields.MediaPlayer.createTitleFromNativeID,
                        p_title->i_duration,
                        jname,
577
                        p_title->i_flags);
578 579 580 581

    if (jname)
        (*env)->DeleteLocalRef(env, jname);
    return jobj;
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
}

jobject
Java_org_videolan_libvlc_MediaPlayer_nativeGetTitles(JNIEnv *env, jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
    libvlc_title_description_t **pp_titles = NULL;
    int i_nb_titles;
    jobjectArray array;

    if (!p_obj)
        return NULL;

    i_nb_titles = libvlc_media_player_get_full_title_descriptions(p_obj->u.p_mp,
                                                                  &pp_titles);
    if (i_nb_titles <= 0)
        return NULL;

    array = (*env)->NewObjectArray(env, i_nb_titles,
                                   fields.MediaPlayer.Title.clazz, NULL);
    if (!array)
        goto error;

    for (int i = 0; i < i_nb_titles; ++i)
    {
        jobject jtitle = mediaplayer_title_to_object(env, pp_titles[i]);

        if (jtitle)
            (*env)->SetObjectArrayElement(env, array, i, jtitle);
    }

error:
    if (pp_titles)
        libvlc_title_descriptions_release(pp_titles, i_nb_titles);
    return array;
}

static jobject
mediaplayer_chapter_to_object(JNIEnv *env,
                              libvlc_chapter_description_t *p_chapter)
{
    jstring jname = NULL;

    if (!p_chapter)
        return NULL;

    if (p_chapter->psz_name)
        jname = (*env)->NewStringUTF(env, p_chapter->psz_name);

631
    jobject jobj = (*env)->CallStaticObjectMethod(env, fields.MediaPlayer.clazz,
632 633 634 635
                        fields.MediaPlayer.createChapterFromNativeID,
                        p_chapter->i_time_offset,
                        p_chapter->i_duration,
                        jname);
636 637 638 639

    if (jname)
        (*env)->DeleteLocalRef(env, jname);
    return jobj;
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
}

jobject
Java_org_videolan_libvlc_MediaPlayer_nativeGetChapters(JNIEnv *env,
                                                       jobject thiz,
                                                       jint jtitle)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
    libvlc_chapter_description_t **pp_chapters = NULL;
    int i_nb_chapters;
    jobjectArray array;

    if (!p_obj)
        return NULL;

    i_nb_chapters =
        libvlc_media_player_get_full_chapter_descriptions(p_obj->u.p_mp,
                                                          jtitle, &pp_chapters);
    if (i_nb_chapters <= 0)
        return NULL;

    array = (*env)->NewObjectArray(env, i_nb_chapters,
                                   fields.MediaPlayer.Chapter.clazz, NULL);
    if (!array)
        goto error;

    for (int i = 0; i < i_nb_chapters; ++i)
    {
        jobject jchapter = mediaplayer_chapter_to_object(env, pp_chapters[i]);

        if (jchapter)
            (*env)->SetObjectArrayElement(env, array, i, jchapter);
    }

error:
    if (pp_chapters)
        libvlc_chapter_descriptions_release(pp_chapters, i_nb_chapters);
    return array;
}
679 680 681 682 683 684 685 686 687 688 689 690

static jobject
mediaplayer_track_to_object(JNIEnv *env, libvlc_track_description_t *p_track)
{
    jstring jname = NULL;

    if (!p_track)
        return NULL;

    if (p_track->psz_name)
        jname = (*env)->NewStringUTF(env, p_track->psz_name);

691
    jobject jobj = (*env)->CallStaticObjectMethod(env, fields.MediaPlayer.clazz,
692 693 694
                        fields.MediaPlayer.createTrackDescriptionFromNativeID,
                        p_track->i_id,
                        jname);
695 696 697 698

    if (jname)
        (*env)->DeleteLocalRef(env, jname);
    return jobj;
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
}

static jobject
mediaplayer_tracklist_to_object(JNIEnv *env,
                                libvlc_track_description_t *p_first)
{
    int i_idx = 0;
    int i_nb_tracks = 0;
    libvlc_track_description_t *p_track = p_first;
    jobjectArray array;

    if (!p_track)
        return NULL;

    do
        i_nb_tracks++;
    while ((p_track = p_track->p_next) != NULL);

    p_track = p_first;

    array = (*env)->NewObjectArray(env, i_nb_tracks,
                                   fields.MediaPlayer.TrackDescription.clazz,
                                   NULL);
    if (!array)
        goto error;

    do
    {
        jobject jtrack = mediaplayer_track_to_object(env, p_track);

        if (jtrack)
            (*env)->SetObjectArrayElement(env, array, i_idx++, jtrack);
    }
    while ((p_track = p_track->p_next) != NULL);

error:
    libvlc_track_description_list_release(p_first);
    return array;
}

jint
Java_org_videolan_libvlc_MediaPlayer_nativeGetVideoTracksCount(JNIEnv *env,
                                                              jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return 0;

    return libvlc_video_get_track_count(p_obj->u.p_mp);
}

jobject
Java_org_videolan_libvlc_MediaPlayer_nativeGetVideoTracks(JNIEnv *env,
                                                          jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return NULL;

    return mediaplayer_tracklist_to_object(env,
                libvlc_video_get_track_description(p_obj->u.p_mp));
}

jint
Java_org_videolan_libvlc_MediaPlayer_nativeGetVideoTrack(JNIEnv *env,
                                                         jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return -2;

    return libvlc_video_get_track(p_obj->u.p_mp);
}

jboolean
Java_org_videolan_libvlc_MediaPlayer_nativeSetVideoTrack(JNIEnv *env,
                                                         jobject thiz,
                                                         jint index)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return false;

    return libvlc_video_set_track(p_obj->u.p_mp, index) == 0 ? true : false;
}

jint
Java_org_videolan_libvlc_MediaPlayer_nativeGetAudioTracksCount(JNIEnv *env,
                                                               jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return 0;

    return libvlc_audio_get_track_count(p_obj->u.p_mp);
}

jobject
Java_org_videolan_libvlc_MediaPlayer_nativeGetAudioTracks(JNIEnv *env,
                                                          jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return NULL;

    return mediaplayer_tracklist_to_object(env,
                libvlc_audio_get_track_description(p_obj->u.p_mp));
}

jint
Java_org_videolan_libvlc_MediaPlayer_nativeGetAudioTrack(JNIEnv *env,
                                                         jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return -2;

    return libvlc_audio_get_track(p_obj->u.p_mp);
}

jboolean
Java_org_videolan_libvlc_MediaPlayer_nativeSetAudioTrack(JNIEnv *env,
                                                         jobject thiz,
                                                         jint index)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return false;

    return libvlc_audio_set_track(p_obj->u.p_mp, index) == 0 ? true : false;
}

jlong
Java_org_videolan_libvlc_MediaPlayer_nativeGetAudioDelay(JNIEnv *env,
                                                         jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return 0;

    return libvlc_audio_get_delay(p_obj->u.p_mp);
}

jboolean
Java_org_videolan_libvlc_MediaPlayer_nativeSetAudioDelay(JNIEnv *env,
                                                         jobject thiz,
                                                         jlong delay)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return false;

    return libvlc_audio_set_delay(p_obj->u.p_mp, delay) == 0 ? true : false;
}

jint
Java_org_videolan_libvlc_MediaPlayer_nativeGetSpuTracksCount(JNIEnv *env,
                                                             jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return 0;

    return libvlc_video_get_spu_count(p_obj->u.p_mp);
}

jobject
Java_org_videolan_libvlc_MediaPlayer_nativeGetSpuTracks(JNIEnv *env,
                                                        jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return NULL;

    return mediaplayer_tracklist_to_object(env,
                libvlc_video_get_spu_description(p_obj->u.p_mp));
}

jint
Java_org_videolan_libvlc_MediaPlayer_nativeGetSpuTrack(JNIEnv *env,
                                                       jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return -2;

    return libvlc_video_get_spu(p_obj->u.p_mp);
}

jboolean
Java_org_videolan_libvlc_MediaPlayer_nativeSetSpuTrack(JNIEnv *env,
                                                       jobject thiz, jint index)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return false;

    return libvlc_video_set_spu(p_obj->u.p_mp, index) == 0 ? true : false;
}

jlong
Java_org_videolan_libvlc_MediaPlayer_nativeGetSpuDelay(JNIEnv *env,
                                                       jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return 0;

    return libvlc_video_get_spu_delay(p_obj->u.p_mp);
}

jboolean
Java_org_videolan_libvlc_MediaPlayer_nativeSetSpuDelay(JNIEnv *env,
                                                       jobject thiz,
                                                       jlong delay)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
       return false;

    return libvlc_video_set_spu_delay(p_obj->u.p_mp, delay) == 0 ? true : false;
}

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
float
Java_org_videolan_libvlc_MediaPlayer_nativeGetScale(JNIEnv *env, jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return 0.f;

    return libvlc_video_get_scale(p_obj->u.p_mp);
}

void
Java_org_videolan_libvlc_MediaPlayer_nativeSetScale(JNIEnv *env, jobject thiz,
                                                    jfloat factor)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;

    libvlc_video_set_scale(p_obj->u.p_mp, factor);
}

961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
jstring
Java_org_videolan_libvlc_MediaPlayer_nativeGetAspectRatio(JNIEnv *env,
                                                          jobject thiz)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return NULL;

    char *psz_aspect = libvlc_video_get_aspect_ratio(p_obj->u.p_mp);
    jstring jaspect = psz_aspect ? (*env)->NewStringUTF(env, psz_aspect) : NULL;
    free(psz_aspect);
    return jaspect;
}

void
Java_org_videolan_libvlc_MediaPlayer_nativeSetAspectRatio(JNIEnv *env,
                                                          jobject thiz,
                                                          jstring jaspect)
{
    const char* psz_aspect;
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);

    if (!p_obj)
        return;

    if (!jaspect)
    {
        libvlc_video_set_aspect_ratio(p_obj->u.p_mp, NULL);
        return;
    }
    if (!(psz_aspect = (*env)->GetStringUTFChars(env, jaspect, 0)))
    {
994
        throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "aspect invalid");
995 996 997 998 999 1000 1001
        return;
    }

    libvlc_video_set_aspect_ratio(p_obj->u.p_mp, psz_aspect);
    (*env)->ReleaseStringUTFChars(env, jaspect, psz_aspect);
}

1002
jboolean
Geoffrey Métais's avatar
Geoffrey Métais committed
1003
Java_org_videolan_libvlc_MediaPlayer_nativeUpdateViewpoint(JNIEnv *env,
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
                                                           jobject thiz,
                                                           jfloat yaw,
                                                           jfloat pitch,
                                                           jfloat roll,
                                                           jfloat fov,
                                                           jboolean absolute)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
    if (!p_obj)
        return false;

    if (p_obj->p_sys->p_vp == NULL)
    {
        p_obj->p_sys->p_vp = libvlc_video_new_viewpoint();
        if (p_obj->p_sys->p_vp == NULL)
            return false;
    }
    p_obj->p_sys->p_vp->f_yaw = yaw;
    p_obj->p_sys->p_vp->f_pitch = pitch;
    p_obj->p_sys->p_vp->f_roll = roll;
    p_obj->p_sys->p_vp->f_field_of_view = fov;

    return libvlc_video_update_viewpoint(p_obj->u.p_mp, p_obj->p_sys->p_vp,
                                         absolute) == 0 ? true : false;
}

1030
jboolean
1031 1032
Java_org_videolan_libvlc_MediaPlayer_nativeAddSlave(JNIEnv *env,
                                                    jobject thiz, jint type,
1033
                                                    jstring jmrl, jboolean select)
1034 1035
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
1036
    const char* psz_mrl;
1037

1038
    if (!p_obj)
1039 1040
        return false;

1041 1042
    if (!jmrl || !(psz_mrl = (*env)->GetStringUTFChars(env, jmrl, 0)))
    {
1043
        throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "mrl invalid");
1044
        return false;
1045
    }
1046

1047
    jboolean ret = libvlc_media_player_add_slave(p_obj->u.p_mp, type, psz_mrl, select) == 0;
1048

1049
    (*env)->ReleaseStringUTFChars(env, jmrl, psz_mrl);
1050 1051
    return ret;
}
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089

jboolean
Java_org_videolan_libvlc_MediaPlayer_nativeSetEqualizer(JNIEnv *env,
                                                        jobject thiz,
                                                        jobject jequalizer)
{
    vlcjni_object *p_obj = VLCJniObject_getInstance(env, thiz);
    libvlc_equalizer_t *p_eq = NULL;

    if (!p_obj)
       return false;

    if (jequalizer)
    {
        p_eq = Equalizer_getInstance(env, jequalizer);
        if (!p_eq)
            return false;
    }

    return libvlc_media_player_set_equalizer(p_obj->u.p_mp, p_eq) == 0 ? true: false;
}

jint
Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetPresetCount(JNIEnv *env,
                                                                         jobject thiz)
{
    return libvlc_audio_equalizer_get_preset_count();
}

jstring
Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetPresetName(JNIEnv *env,
                                                                        jobject thiz,
                                                                        jint index)
{
    const char *psz_name;

    if (index < 0)
    {
1090
        throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "index invalid");
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
        return NULL;
    }

    psz_name = libvlc_audio_equalizer_get_preset_name(index);

    return psz_name ? (*env)->NewStringUTF(env, psz_name) : NULL;
}

jint
Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetBandCount(JNIEnv *env,
                                                                       jobject thiz)
{
    return libvlc_audio_equalizer_get_band_count();
}

jfloat
Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetBandFrequency(JNIEnv *env,
                                                                           jobject thiz,
                                                                           jint index)
{
    if (index < 0)
    {
1113
        throw_Exception(env, VLCJNI_EX_ILLEGAL_ARGUMENT, "index invalid");
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
        return 0.0;
    }

    return libvlc_audio_equalizer_get_band_frequency(index);
}

void
Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeNew(JNIEnv *env,
                                                              jobject thiz)
{
    libvlc_equalizer_t *p_eq = libvlc_audio_equalizer_new();
    if (!p_eq)
1126
        throw_Exception(env, VLCJNI_EX_OUT_OF_MEMORY, "Equalizer");
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137

    VLCJniObject_setInstance(env, thiz, p_eq);
}

void
Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeNewFromPreset(JNIEnv *env,
                                                                        jobject thiz,
                                                                        jint index)
{
    libvlc_equalizer_t *p_eq = libvlc_audio_equalizer_new_from_preset(index);
    if (!p_eq)
1138
        throw_Exception(env, VLCJNI_EX_OUT_OF_MEMORY, "Equalizer");
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201

    VLCJniObject_setInstance(env, thiz, p_eq);
}

void
Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeRelease(JNIEnv *env,
                                                                  jobject thiz)
{
    libvlc_equalizer_t *p_eq = Equalizer_getInstance(env, thiz);
    if (!p_eq)
        return;

    libvlc_audio_equalizer_release(p_eq);
    VLCJniObject_setInstance(env, thiz, NULL);
}

jfloat
Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetPreAmp(JNIEnv *env,
                                                                    jobject thiz)
{
    libvlc_equalizer_t *p_eq = Equalizer_getInstance(env, thiz);
    if (!p_eq)
        return 0.0;

    return libvlc_audio_equalizer_get_preamp(p_eq);
}

jboolean
Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeSetPreAmp(JNIEnv *env,
                                                                    jobject thiz,
                                                                    jfloat preamp)
{
    libvlc_equalizer_t *p_eq = Equalizer_getInstance(env, thiz);
    if (!p_eq)
        return false;

    return libvlc_audio_equalizer_set_preamp(p_eq, preamp) == 0 ? true : false;
}

jfloat
Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeGetAmp(JNIEnv *env,
                                                                 jobject thiz,
                                                                 jint index)
{
    libvlc_equalizer_t *p_eq = Equalizer_getInstance(env, thiz);
    if (!p_eq)
        return 0.0;

    return libvlc_audio_equalizer_get_amp_at_index(p_eq, index);
}

jboolean
Java_org_videolan_libvlc_MediaPlayer_00024Equalizer_nativeSetAmp(JNIEnv *env,
                                                                 jobject thiz,
                                                                 jint index,
                                                                 jfloat amp)
{
    libvlc_equalizer_t *p_eq = Equalizer_getInstance(env, thiz);
    if (!p_eq)
        return false;

    return libvlc_audio_equalizer_set_amp_at_index(p_eq, amp, index) == 0 ? true : false;
}