libvlcjni-medialist.c 13.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*****************************************************************************
 * libvlcjni-medialist.c
 *****************************************************************************
 * Copyright © 2013 VLC authors and VideoLAN
 * Copyright © 2013 Edward Wang
 *
 * 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 <jni.h>
#include <vlc/vlc.h>
#include <vlc/libvlc_media_list.h>
24
#include <stdlib.h>
Edward Wang's avatar
Edward Wang committed
25
#include <pthread.h>
26
27
28
29
30

#include "utils.h"
#define LOG_TAG "VLC/JNI/MediaList"
#include "log.h"

31
32
33
/** Unique Java VM instance, as defined in libvlcjni.c */
extern JavaVM *myVm;

Edward Wang's avatar
Edward Wang committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
struct stopped_monitor {
    pthread_mutex_t doneMutex;
    pthread_cond_t doneCondVar;
    bool stopped;
};

static void stopped_callback(const libvlc_event_t *ev, void *data)
{
    struct stopped_monitor* monitor = data;
    pthread_mutex_lock(&monitor->doneMutex);
    monitor->stopped = true;
    pthread_cond_signal(&monitor->doneCondVar);
    pthread_mutex_unlock(&monitor->doneMutex);
}

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// data is the MediaList Java object of the media list
static void vlc_media_list_event_callback(const libvlc_event_t *ev, void *data)
{
    jobject eventHandlerInstance = (jobject)(intptr_t)data;
    JNIEnv *env;

    bool isAttached = false;

    if (eventHandlerInstance == NULL)
        return;

    if ((*myVm)->GetEnv(myVm, (void**) &env, JNI_VERSION_1_2) < 0) {
        if ((*myVm)->AttachCurrentThread(myVm, &env, NULL) < 0)
            return;
        isAttached = true;
    }

    /* 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" );
    jmethodID putFloat = (*env)->GetMethodID(env, clsBundle, "putFloat", "(Ljava/lang/String;F)V" );
    jmethodID putString = (*env)->GetMethodID(env, clsBundle, "putString", "(Ljava/lang/String;Ljava/lang/String;)V" );

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

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

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

end:
Edward Wang's avatar
Edward Wang committed
117
    (*env)->DeleteLocalRef(env, bundle);
118
119
120
121
    if (isAttached)
        (*myVm)->DetachCurrentThread(myVm);
}

122
static int expand_media_internal(libvlc_instance_t* p_instance, libvlc_media_list_t* p_mlist, int position) {
123
124
125
126
127
128
129
130
131
132
133
134
    libvlc_media_t* p_md = libvlc_media_list_item_at_index(p_mlist, position);
    if(!p_md) {
        return -1;
    }
    libvlc_media_list_t* p_subitems = libvlc_media_subitems(p_md);
    libvlc_media_release(p_md);
    if(p_subitems) {
        // Expand any subitems if needed
        int subitem_count = libvlc_media_list_count(p_subitems);
        if(subitem_count > 0) {
            LOGD("Found %d subitems, expanding", subitem_count);
            for(int i = subitem_count - 1; i >= 0; i--) {
135
136
                libvlc_media_t* p_subitem_old = libvlc_media_list_item_at_index(p_subitems, i);
                libvlc_media_t* p_subitem = libvlc_media_new_location(p_instance, libvlc_media_get_mrl(p_subitem_old));
137
138
                libvlc_media_list_insert_media(p_mlist, p_subitem, position+1);
                libvlc_media_release(p_subitem);
139
                libvlc_media_release(p_subitem_old);
140
141
142
143
144
145
146
147
148
149
150
151
152
153
            }
            libvlc_media_list_remove_index(p_mlist, position);
        }
        libvlc_media_list_release(p_subitems);
        if(subitem_count > 0) {
            return 0;
        } else {
            return -1;
        }
    } else {
        return -1;
    }
}

154
155
156
157
158
159
160
jlong Java_org_videolan_libvlc_MediaList_init(JNIEnv *env, jobject thiz, jobject libvlcJava) {
    libvlc_media_list_t* p_ml = libvlc_media_list_new((libvlc_instance_t*)(intptr_t)getLong(env, libvlcJava, "mLibVlcInstance"));
    if(!p_ml) {
        jclass exc = (*env)->FindClass(env, "org/videolan/libvlc/LibVlcException");
        (*env)->ThrowNew(env, exc, "Unable to create LibVLC media list");
        return (jlong)(intptr_t)NULL;
    }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

    jclass cls = (*env)->GetObjectClass(env, thiz);
    jfieldID fieldID = (*env)->GetFieldID(env, cls, "mEventHandler", "Lorg/videolan/libvlc/EventHandler;");
    jobject eventHandler = (*env)->GetObjectField(env, thiz, fieldID);
    jobject globalRef = getEventHandlerReference(env, thiz, eventHandler);

    setLong(env, thiz, "mEventHanderGlobalRef", (jlong)(intptr_t)globalRef);

    /* Connect the event manager */
    libvlc_event_manager_t *ev = libvlc_media_list_event_manager(p_ml);
    static const libvlc_event_type_t mp_events[] = {
        libvlc_MediaListItemAdded,
        libvlc_MediaListItemDeleted,
    };
    for(int i = 0; i < (sizeof(mp_events) / sizeof(*mp_events)); i++)
        libvlc_event_attach(ev, mp_events[i], vlc_media_list_event_callback, globalRef);

178
179
180
181
    return (jlong)(intptr_t)p_ml;
}

void Java_org_videolan_libvlc_MediaList_nativeDestroy(JNIEnv *env, jobject thiz) {
182
    libvlc_media_list_t* p_ml = getMediaListFromJava(env, thiz);
183
    libvlc_media_list_release(p_ml);
184
    (*env)->DeleteGlobalRef(env, (jobject)(intptr_t)getLong(env, thiz, "mEventHanderGlobalRef"));
185
186
}

Edward Wang's avatar
Edward Wang committed
187
188
189
void Java_org_videolan_libvlc_MediaList_clear(JNIEnv *env, jobject thiz) {
    libvlc_media_list_t* p_ml = getMediaListFromJava(env, thiz);
    libvlc_media_list_lock(p_ml);
Sébastien Toque's avatar
Sébastien Toque committed
190
    while (libvlc_media_list_count(p_ml) > 0) {
Edward Wang's avatar
Edward Wang committed
191
192
193
194
195
        libvlc_media_list_remove_index(p_ml, 0);
    }
    libvlc_media_list_unlock(p_ml);
}

196
jint Java_org_videolan_libvlc_MediaList_expandMedia(JNIEnv *env, jobject thiz, jobject libvlcJava, jint position) {
197
198
    libvlc_media_list_t* p_ml = getMediaListFromJava(env, thiz);
    libvlc_media_list_lock(p_ml);
199
    jint ret = (jint)expand_media_internal((libvlc_instance_t*)(intptr_t)getLong(env, libvlcJava, "mLibVlcInstance"), p_ml, position);
200
201
202
203
    libvlc_media_list_unlock(p_ml);
    return ret;
}

Edward Wang's avatar
Edward Wang committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
void Java_org_videolan_libvlc_MediaList_loadPlaylist(JNIEnv *env, jobject thiz, jobject libvlcJava, jstring mrl) {
    libvlc_media_list_t* p_ml = getMediaListFromJava(env, thiz);
    const char* p_mrl = (*env)->GetStringUTFChars(env, mrl, NULL);

    libvlc_media_t *p_md = libvlc_media_new_location((libvlc_instance_t*)(intptr_t)getLong(env, libvlcJava, "mLibVlcInstance"), p_mrl);
    libvlc_media_add_option(p_md, ":demux=playlist,none");
    libvlc_media_add_option(p_md, ":run-time=1");

    struct stopped_monitor* monitor = malloc(sizeof(struct stopped_monitor));
    pthread_mutex_init(&monitor->doneMutex, NULL);
    pthread_cond_init(&monitor->doneCondVar, NULL);
    monitor->stopped = false;
    pthread_mutex_lock(&monitor->doneMutex);

    libvlc_media_player_t* p_mp = libvlc_media_player_new((libvlc_instance_t*)(intptr_t)getLong(env, libvlcJava, "mLibVlcInstance"));
    libvlc_event_manager_t* ev = libvlc_media_player_event_manager(p_mp);
    libvlc_event_attach(ev, libvlc_MediaPlayerEndReached, stopped_callback, monitor);
    libvlc_media_player_set_media(p_mp, p_md);
    libvlc_media_player_play(p_mp);

    struct timespec deadline;
    clock_gettime(CLOCK_REALTIME, &deadline);
    deadline.tv_sec += 2; /* If "VLC can't open the file", return */
    int mp_alive = 1;
    while(!(monitor->stopped) && mp_alive) {
        pthread_cond_timedwait(&monitor->doneCondVar, &monitor->doneMutex, &deadline);
        mp_alive = libvlc_media_player_will_play(p_mp);
    }
    pthread_mutex_unlock(&monitor->doneMutex);
    pthread_mutex_destroy(&monitor->doneMutex);
    pthread_cond_destroy(&monitor->doneCondVar);
    free(monitor);

    libvlc_media_player_release(p_mp);

    libvlc_media_list_lock(p_ml);
    int pos = libvlc_media_list_count(p_ml);
    libvlc_media_list_add_media(p_ml, p_md);
242
    expand_media_internal((libvlc_instance_t*)(intptr_t)getLong(env, libvlcJava, "mLibVlcInstance"), p_ml, pos);
Edward Wang's avatar
Edward Wang committed
243
244
245
246
247
    libvlc_media_list_unlock(p_ml);

    (*env)->ReleaseStringUTFChars(env, mrl, p_mrl);
}

248
void Java_org_videolan_libvlc_MediaList_remove(JNIEnv *env, jobject thiz, jint position) {
249
    libvlc_media_list_t* p_ml = getMediaListFromJava(env, thiz);
250
251
252
253
254
    libvlc_media_list_lock(p_ml);
    libvlc_media_list_remove_index(p_ml, position);
    libvlc_media_list_unlock(p_ml);
}

255
void Java_org_videolan_libvlc_MediaList_add(JNIEnv *env, jobject thiz, jobject libvlcInstance, jstring mrl, bool noVideo, bool noOmx) {
256
    libvlc_media_list_t* p_ml = getMediaListFromJava(env, thiz);
257
258
    const char* p_mrl = (*env)->GetStringUTFChars(env, mrl, NULL);
    libvlc_media_t *p_md = libvlc_media_new_location((libvlc_instance_t*)(intptr_t)getLong(env, libvlcInstance, "mLibVlcInstance"), p_mrl);
259
    if (!noOmx) {
Edward Wang's avatar
Edward Wang committed
260
        jclass cls = (*env)->GetObjectClass(env, libvlcInstance);
261
        jmethodID methodId = (*env)->GetMethodID(env, cls, "useIOMX", "()Z");
Edward Wang's avatar
Edward Wang committed
262
        if ((*env)->CallBooleanMethod(env, libvlcInstance, methodId)) {
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
            /*
             * Set higher caching values if using iomx decoding, since some omx
             * decoders have a very high latency, and if the preroll data isn't
             * enough to make the decoder output a frame, the playback timing gets
             * started too soon, and every decoded frame appears to be too late.
             * On Nexus One, the decoder latency seems to be 25 input packets
             * for 320x170 H.264, a few packets less on higher resolutions.
             * On Nexus S, the decoder latency seems to be about 7 packets.
             */
            libvlc_media_add_option(p_md, ":file-caching=1500");
            libvlc_media_add_option(p_md, ":network-caching=1500");
            libvlc_media_add_option(p_md, ":codec=mediacodec,iomx,all");
        }
        if (noVideo)
            libvlc_media_add_option(p_md, ":no-video");
    }
279
280
281
282
283
284
285
286
    libvlc_media_list_lock(p_ml);
    libvlc_media_list_add_media(p_ml, p_md);
    libvlc_media_list_unlock(p_ml);
    libvlc_media_release(p_md);
    (*env)->ReleaseStringUTFChars(env, mrl, p_mrl);
}

void Java_org_videolan_libvlc_MediaList_insert(JNIEnv *env, jobject thiz, jobject libvlcInstance, jint position, jstring mrl) {
287
    libvlc_media_list_t* p_ml = getMediaListFromJava(env, thiz);
288
289
290
291
292
293
294
295
296
297
    const char* p_mrl = (*env)->GetStringUTFChars(env, mrl, NULL);
    libvlc_media_t *p_md = libvlc_media_new_location((libvlc_instance_t*)(intptr_t)getLong(env, libvlcInstance, "mLibVlcInstance"), p_mrl);
    libvlc_media_list_lock(p_ml);
    libvlc_media_list_insert_media(p_ml, p_md, position);
    libvlc_media_list_unlock(p_ml);
    libvlc_media_release(p_md);
    (*env)->ReleaseStringUTFChars(env, mrl, p_mrl);
}

jint Java_org_videolan_libvlc_MediaList_size(JNIEnv *env, jobject thiz) {
298
    libvlc_media_list_t* p_ml = getMediaListFromJava(env, thiz);
299
300
301
302
303
304
305
    libvlc_media_list_lock(p_ml);
    int count = libvlc_media_list_count(p_ml);
    libvlc_media_list_unlock(p_ml);
    return count;
}

jstring Java_org_videolan_libvlc_MediaList_getMRL(JNIEnv *env, jobject thiz, jint position) {
306
    libvlc_media_list_t* p_ml = getMediaListFromJava(env, thiz);
307
308
309
310
311
312
313
314
315
316
    libvlc_media_list_lock(p_ml);
    libvlc_media_t* p_md = libvlc_media_list_item_at_index(p_ml, position);
    libvlc_media_list_unlock(p_ml);
    if(p_md) {
        char* p_mrl = libvlc_media_get_mrl(p_md);
        libvlc_media_release(p_md);
        return (*env)->NewStringUTF(env, p_mrl);
    } else
        return NULL;
}