Caprica Software

vlcj 4.x Tutorial

Multiple Media Players

LibVLC, and consequently vlcj, are best suited to applications that embed a single native media player.

Some applications want to include multiple video players in the same application - for example, a CCTV application might want to display multiple camera feeds in the same window.

You can use vlcj to embed multiple video players in your application, but there can be significant impediments to doing so successfully and it really is not recommended.

There are two approaches: use multiple in-process video players, where each native video player runs in the same Java process (i.e. the same Java application) as your application, or; use multiple out-of-process media players, where each native media player actually runs in a separate process (i.e. in its own Java application) and communicates with your application using some sort of remote procedure call.


In-Process

An in-process native media player runs in the same Java process as your application. Usually this is not a concern because when you only use one such media player, and you use vlcj properly, you will never encounter any problems due to the native media player.

Nevertheless, it is possible that something goes wrong in the native code, it may be a bug, a glitch that causes a crash, or a crash due to some sort of resource leak. The biggest problem is actually due to the same application "re-entering" the native code - LibVLC is claimed to be re-entrant, but there is no guarantee that all of the third party native libraries VLC or its plugins use are also re-entrant, this can for example cause corruption of shared global state that may exist in one or more of those libraries.

Whatever it is if a crash occurs in the native media player then your Java application will be terminated immediately and there is nothing you can do about it.

The other significant issue when using multiple in-process media players is that they all share the same volume controls - changing the volume, or mute, on one player changes it for all.

Using multiple in-process media players is much simpler than using out-of-process media players, but it simply is not as robust.


Out-Of-Process

An out-of-process native media player runs in a separate process, i.e. a separate Java application, independent of your main application.

Any crash in the native media player in the external process will terminate only that process - your main application can keep going and recover, possibly creating a new media player process and continuing.

Out-of-process media players give you independant volume controls - changing the volume or mute on one player has no effect on any other.

Using multiple out-of-process media players is much more difficult and complicated when compared with using multiple in-process media players, but it is much more robust and reliable.

You can read more information about out-of-process media players, and you may like to consider vlcj-pro.


What Does vlcj Provide?

With vlcj, out-of-the-box you can try and use multiple in-process media players.

Either you create multiple EmbeddedMediaPlayerComponent instances, or you create a single instance of a MediaPlayerFactory and use that factory to create multiple instances of an EmbeddedMediaPlayer, or you use one new MediaPlayerFactory for each new EmbeddedMediaPlayer that you need.

This might work well enough for you, but it really is not a supported mode of operating and a fatal crash could occur at any time, and perhaps only after some significant period of running.


Anecdotal is a Type of Evidence

If you decide to go ahead and try multiple in-process media players in the same application that are some things you can do, learned from hard experience, to mitigate (but not completely eliminate) fatal crashes:

  • Access one media player instance at a time: be careful with your threading; be careful with which of your threads are executing code that calls back into your media player and when (especially in event handlers); implement a locking protocol so you only access one media player at a time.
  • "Warm-up" your media player instances before you use them: this means playing a zero-length (or minimal-length) video and waiting for it to end immediately when you first create each of your media players and make sure you do this one at a time.

Other observations from practical experience of trying this:

  • This is one circumstance where Windows actually performs better than Linux, you are less likely to encounter problems with multiple in-process media players on Windows.

Summary

Using multiple in-process media players in the same application is generally something to be avoided and is not really officially supported with vlcj.

The only reliable solution is to use out-of-process media players, but you must either build that yourself or use something like vlcj-pro.