libvlcjni.c 8.46 KB
Newer Older
1
#include <stdio.h>
2
#include <string.h>
3
#include <assert.h>
4

5
6
#include <jni.h>

7
#include <vlc/vlc.h>
8

9
#include "libvlcjni.h"
ivoire's avatar
ivoire committed
10
#include "aout.h"
11
#include "vout.h"
12
13

#define LOG_TAG "VLC/JNI/main"
14
15
#include "log.h"

16
17
18
19
20
21
22
23
jint getMediaPlayer(JNIEnv *env, jobject thiz)
{
    jclass clazz = (*env)->GetObjectClass(env, thiz);
    jfieldID fieldMP = (*env)->GetFieldID(env, clazz,
                                          "mMediaPlayerInstance", "I");
    return (*env)->GetIntField(env, thiz, fieldMP);
}

24
25
26
27
28
/* Pointer to the Java virtual machine
 * Note: It's okay to use a static variable for the VM pointer since there
 * can only be one instance of this shared library in a single VM
 */
JavaVM *myVm;
29
30


31
jint JNI_OnLoad(JavaVM *vm, void *reserved)
32
{
33
34
35
    // Keep a reference on the Java VM.
    myVm = vm;

36
    LOGD("JNI interface loaded.");
37
    return JNI_VERSION_1_2;
38
39
}

40

41
void Java_vlc_android_LibVLC_nativeInit(JNIEnv *env, jobject thiz)
42
43
{
    const char *argv[] = {"-I", "dummy", "-vvv", "--no-plugins-cache",
44
                          "--no-drop-late-frames"};
45

46
47
48
49
50
51
52
53
    libvlc_instance_t *instance =
            libvlc_new_with_builtins(sizeof(argv) / sizeof(*argv),
                                     argv, vlc_builtins_modules);

    jclass clazz = (*env)->GetObjectClass(env, thiz);
    jfieldID field = (*env)->GetFieldID(env, clazz,
                                        "mLibVlcInstance", "I");
    (*env)->SetIntField(env, thiz, field, (jint) instance);
54

55
56
57
58
59
60
61
62
    if (!instance)
    {
        jclass exc = (*env)->FindClass(env, "vlc/android/LibVlcException");
        (*env)->ThrowNew(env, exc, "Unable to instantiate LibVLC");
    }

    LOGI("LibVLC initialized: %p", instance);
    return;
63
64
}

65

66
void Java_vlc_android_LibVLC_nativeDestroy(JNIEnv *env, jobject thiz)
67
{
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    jclass clazz = (*env)->GetObjectClass(env, thiz);

    jfieldID fieldMP = (*env)->GetFieldID(env, clazz,
                                          "mMediaPlayerInstance", "I");
    jint mediaPlayer = (*env)->GetIntField(env, thiz, fieldMP);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        libvlc_media_player_stop(mp);
        libvlc_media_player_release(mp);
        (*env)->SetIntField(env, thiz, fieldMP, 0);
    }

    jfieldID field = (*env)->GetFieldID(env, clazz, "mLibVlcInstance", "I");
    jint libVlcInstance = (*env)->GetIntField(env, thiz, field);
    if (!libVlcInstance)
        return; // Already destroyed

    libvlc_instance_t *instance = (libvlc_instance_t*) libVlcInstance;
    libvlc_release(instance);

    (*env)->SetIntField(env, thiz, field, 0);
90
91
}

92

93
94
void Java_vlc_android_LibVLC_readMedia(JNIEnv *env, jobject thiz,
                                       jint instance, jstring mrl)
95
{
96
97
98
    jboolean isCopy;
    const char *psz_mrl = (*env)->GetStringUTFChars(env, mrl, &isCopy);

99
    /* Create a new item */
100
101
    libvlc_media_t *m = libvlc_media_new_path((libvlc_instance_t*)instance,
                                              psz_mrl);
102

103
    /* Create a media player playing environment */
104
105
    libvlc_media_player_t *mp = libvlc_media_player_new((libvlc_instance_t*)instance);

106
107
    jobject myJavaLibVLC = (*env)->NewGlobalRef(env, thiz);

108
109
    libvlc_media_player_set_media(mp, m);
    libvlc_video_set_format_callbacks(mp, vout_format, vout_cleanup);
110
111
    libvlc_video_set_callbacks(mp, vout_lock, vout_unlock, vout_display,
                               (void*) myJavaLibVLC);
112

113
114
    libvlc_audio_set_callbacks(mp, aout_open, aout_play, aout_close,
                               (void*) myJavaLibVLC);
ivoire's avatar
ivoire committed
115

116
117
118
    /* No need to keep the media now */
    libvlc_media_release(m);

119
120
121
122
123
124
    /* Keep a pointer to this media player */
    jclass clazz = (*env)->GetObjectClass(env, thiz);
    jfieldID field = (*env)->GetFieldID(env, clazz,
                                        "mMediaPlayerInstance", "I");
    (*env)->SetIntField(env, thiz, field, (jint) mp);

125
    /* Play the media. */
126
127
    libvlc_media_player_play(mp);

128
    //libvlc_media_player_release(mp);
129

130
    (*env)->ReleaseStringUTFChars(env, mrl, psz_mrl);
131
132
}

133

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
jint Java_vlc_android_LibVLC_getHeight(JNIEnv *env, jobject thiz)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        return libvlc_video_get_height( mp );
    }
}

jint Java_vlc_android_LibVLC_getWidth(JNIEnv *env, jobject thiz)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        return libvlc_video_get_width( mp );
    }
}

jboolean Java_vlc_android_LibVLC_isPlaying(JNIEnv *env, jobject thiz)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        return ( libvlc_media_player_is_playing( mp ) == 1 );
    }
}

jboolean Java_vlc_android_LibVLC_isSeekable(JNIEnv *env, jobject thiz)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        return ( libvlc_media_player_is_seekable( mp ) == 1 );
    }
}

void Java_vlc_android_LibVLC_play(JNIEnv *env, jobject thiz)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        libvlc_media_player_play( mp );
    }
}

void Java_vlc_android_LibVLC_pause(JNIEnv *env, jobject thiz)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        libvlc_media_player_pause( mp );
    }
}

void Java_vlc_android_LibVLC_stop(JNIEnv *env, jobject thiz)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        libvlc_media_player_stop( mp );
    }
}

jint Java_vlc_android_LibVLC_getVolume(JNIEnv *env, jobject thiz)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        return (jint) libvlc_audio_get_volume( mp );
    }
    return -1;
}

jint Java_vlc_android_LibVLC_setVolume(JNIEnv *env, jobject thiz, jint volume)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        //Returns 0 if the volume was set, -1 if it was out of range or error
        return (jint) libvlc_audio_set_volume( mp, (int) volume );
    }
    return -1;
}

jlong Java_vlc_android_LibVLC_getTime(JNIEnv *env, jobject thiz)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        return libvlc_media_player_get_time( mp );
    }
    return -1;
}

void Java_vlc_android_LibVLC_setTime(JNIEnv *env, jobject thiz, jlong time)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        libvlc_media_player_set_time( mp, time );
    }
}

jfloat Java_vlc_android_LibVLC_getPosition(JNIEnv *env, jobject thiz)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        return (jfloat) libvlc_media_player_get_position( mp );
    }
    return -1;
}

void Java_vlc_android_LibVLC_setPosition(JNIEnv *env, jobject thiz, jfloat pos)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        libvlc_media_player_set_position( mp, pos );
    }
}

jlong Java_vlc_android_LibVLC_getLength(JNIEnv *env, jobject thiz)
{
    jint mediaPlayer = getMediaPlayer(env, thiz);
    if (mediaPlayer != 0)
    {
        libvlc_media_player_t *mp = (libvlc_media_player_t*) mediaPlayer;
        return (jlong) libvlc_media_player_get_length( mp );
    }
    return -1;
}

280
jstring Java_vlc_android_LibVLC_version(JNIEnv* env, jobject thiz)
281
{
282
283
    return (*env)->NewStringUTF(env, libvlc_get_version());
}
284

285
jstring Java_vlc_android_LibVLC_compiler(JNIEnv* env, jobject thiz)
286
287
288
289
{
    return (*env)->NewStringUTF(env, libvlc_get_compiler());
}

290
jstring Java_vlc_android_LibVLC_changeset(JNIEnv* env, jobject thiz)
291
292
293
{
    return (*env)->NewStringUTF(env, libvlc_get_changeset());
}