How can I load a memory stream into LibVLC?

4.6k Views Asked by At

I want to play a media file from a memory stream using LibVLC like so:

//Ideally it would go like this:
LibVLC.MediaFromStream = new MemoryStream(File.ReadAllBytes(File_Path));

Of course this is a very oversimplified version of what I want but hopefully it conveys what I am looking for.

The reason being that I want there to be a good amount of portability for what I'm doing without having to track file locations and such. I'd rather have a massive clump of data in a single file that can be read from than have to track the locations of one or many more files.

I know this has something to do with the LibVLC IMEM Access module. However, looking at what information I've been able to find on that, I feel like I've been tossed from a plane and have just a few minutes to learn how to fly before I hit the ground.

2

There are 2 best solutions below

1
caprica On

There is no LibVLC API for imem, at least not presently.

You can however still use imem in your LibVLC application, but it's not straightforward...

If you do vlc -H | grep imem you will see something like this (this is just some of the options, there are others too):

  --imem-get <string>        Get function
  --imem-release <string>    Release function
  --imem-cookie <string>     Callback cookie string
  --imem-data <string>       Callback data

You can pass values for these switches either when you create your libvlc instance via libvlc_new(), or when you prepare media via libvlc_media_add_option().

Getting the needed values for these switches is a bit trickier, since you need to pass the actual in-memory address (pointer) to the callback functions you declare in your own application. You end up passing something like "--imem-get 812911313", for example.

There are downsides to doing it this way, e.g. you may not be able to seek backwards/forwards in the stream.

I've done this successfully in Java, but not C# (never tried).

An alternative to consider if you want to play the media data stored in a file, is to store your media in a zip or rar since vlc has plugins to play media from directly inside such archives.

3
Flakker On

See my answer to a similar question here: https://stackoverflow.com/a/31316867/2202445

In summary, the API:

libvlc_media_t* libvlc_media_new_callbacks  (libvlc_instance_t * instance,
                                             libvlc_media_open_cb   open_cb,
                                             libvlc_media_read_cb   read_cb,
                                             libvlc_media_seek_cb   seek_cb,
                                             libvlc_media_close_cb  close_cb,
                                             void *     opaque)

allows just this. The four callbacks must be implemented, although the documentation states the seek callback is not always necessary, see the libVlc documentation. I give an example of a partial implementation in the above answer.