Search code examples
androidlibvlc

How to play back a byte stream in libVLC for Android


I would like to play back a byte stream with the media player in libVLC for Android. But I don't find any Interface or Class where I could "inject" a byte stream. Only chance I have for play back is providing a file descriptor, a path to a file, or an URI.

Android's native media player provides the interface setDataSource(MediaDataSource dataSource) where a byte stream can be injected by extending the class MediaDataSource. Do I have similar possibility in libVLC for Android?


Solution

  • The libVLC API you are looking for is libvlc_media_new_callbacks.

    However, it seems it is not currently exposed to Java to be used with a Java stream parameter. This would need to be implemented by you in the libvlcjni bindings, I believe.

    You could get inspiration from this existing code making use of that API

    void
    Java_org_videolan_libvlc_Media_nativeNewFromFdWithOffsetLength(
        JNIEnv *env, jobject thiz, jobject libVlc, jobject jfd, jlong offset, jlong length)
    {
        vlcjni_object *p_obj;
        int fd = FDObject_getInt(env, jfd);
        if (fd == -1)
            return;
    
        p_obj = VLCJniObject_newFromJavaLibVlc(env, thiz, libVlc);
        if (!p_obj)
            return;
    
        p_obj->u.p_m =
            libvlc_media_new_callbacks(p_obj->p_libvlc,
                                       media_cb_open,
                                       media_cb_read,
                                       media_cb_seek,
                                       media_cb_close,
                                       p_obj);
    
    
        if (Media_nativeNewCommon(env, thiz, p_obj) == 0)
        {
            vlcjni_object_sys *p_sys = p_obj->p_sys;
            p_sys->media_cb.fd = fd;
            p_sys->media_cb.offset = offset;
            p_sys->media_cb.length = length >= 0 ? length : UINT64_MAX;
        }
    }
    

    https://github.com/videolan/vlc-android/blob/f05db3f9b51e64061ff73c794e6a7bfb44f34f65/libvlc/jni/libvlcjni-media.c#L284-L313

    libvlcsharp has this implemented, including for Android platforms, but it's .NET.