dzav
dzav

Reputation: 555

Correct using VLCj

I try to use VLCj to get access to web-cameras. I am using this code:

public static void main(String[] args) {
    // Create player.
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
    DirectMediaPlayer mediaPlayer = mediaPlayerFactory.newDirectMediaPlayer(
            320, 240, 
            new RenderCallback() {
                @Override
                public void display(Memory arg0) {
                    // Do nothing.
                }
         });

    // Options setup.
    String[] options = new String[]{};
    String mrl = "v4l2:///dev/video0"; // Linux

    // Start preocessing.
    mediaPlayer.startMedia(mrl, options);

    BufferedImage bufImg;
    for (int i = 0; i < 1000; ++i) {
        bufImg = mediaPlayer.getSnapshot();

        // Do something with BufferedImage...
        // ...
    }

    // Stop precessing.
    mediaPlayer.stop();
    mediaPlayer = null;

    System.out.println("Finish!");
}

And this code partially works -- I can get and work with BufferedImage, but:

UPD: I am using openSUSE 12.2 x64, VLC 2.0.3 installed and working properly for all video files, library VLCj 2.1.0.

Upvotes: 2

Views: 8705

Answers (2)

dzav
dzav

Reputation: 555

This code working properly:

public static void main(String[] args) {
    // Configure player factory.
    String[] VLC_ARGS = {
            "--intf", "dummy",          // no interface
            "--vout", "dummy",          // we don't want video (output)
            "--no-audio",               // we don't want audio (decoding)
            "--no-video-title-show",    // nor the filename displayed
            "--no-stats",               // no stats
            "--no-sub-autodetect-file", // we don't want subtitles
            "--no-inhibit",             // we don't want interfaces
            "--no-disable-screensaver", // we don't want interfaces
            "--no-snapshot-preview",    // no blending in dummy vout
    };
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(VLC_ARGS);

    // Create player.
    HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer();

    // Select input device.
    String mrl = "v4l2:///dev/video0";  // Linux

    // Start processing.
    mediaPlayer.startMedia(mrl);

    BufferedImage bufImg;
    for (int i = 0; i < 1000; ++i) {
        bufImg = mediaPlayer.getSnapshot();

        // Do something with BufferedImage...
        // ...
    }

    // Stop processing.
    mediaPlayer.stop();

    // Finish program.
    mediaPlayer.release();
    mediaPlayerFactory.release();
}

Upvotes: 3

Re your native window: VLCj opens a shared instance to the VLC library.

A headless media palyer is NOT intended to have a video or audio output!

In fact, if you need anything to play (and not to stream to anywhere else) you need to create either an output window or use a direct media player (may be much more complicated) So, if a headless player needs to play something it opens a native window to perform the playback!

Source: http://www.capricasoftware.co.uk/wiki/index.php?title=Vlcj_Media_Players

Re the error: the video display component MUST be the top component of the panel, window or whereever it is added to. Otherwise it will throw the error

main vout display error: Failed to set on top

Furthermore, if you put anything over the component it will destroy the video output which won't work anymore!

Anyway, I don't know how the DirectMediaPlayer works in detail but VLCj has some weird behaviour... Maybe getSnapshot() needs a video display component but I'm not sure.

Re your not finishing program: you join to finish your own thread. This can't work because your thread "sleeps" until the other thread who is waited for has been terminated but as this is your own thread it "sleeps" and won't terminate. You can test this behaviour with this short code in a main method:

System.out.println("Test start");
Thread.currentThread().join();
System.out.println("Test stop");

You will NEVER reach the "Test stop" statement.

Upvotes: 2

Related Questions