diff --git a/webcam-capture-drivers/webcam-capture-driver-vlcj/.classpath b/webcam-capture-drivers/webcam-capture-driver-vlcj/.classpath index c9464c0f..c2ce1e7b 100644 --- a/webcam-capture-drivers/webcam-capture-driver-vlcj/.classpath +++ b/webcam-capture-drivers/webcam-capture-driver-vlcj/.classpath @@ -1,26 +1,27 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/webcam-capture-drivers/webcam-capture-driver-vlcj/pom.xml b/webcam-capture-drivers/webcam-capture-driver-vlcj/pom.xml index 19025441..769ce222 100644 --- a/webcam-capture-drivers/webcam-capture-driver-vlcj/pom.xml +++ b/webcam-capture-drivers/webcam-capture-driver-vlcj/pom.xml @@ -13,14 +13,6 @@ Webcam Capture - VLCj Driver Webcam Capture driver using VLCj framework to grab frames from camera - - - - caprica - Caprica Software - http://www.capricasoftware.co.uk/repo - - @@ -31,7 +23,7 @@ uk.co.caprica vlcj - 2.2.0-SNAPSHOT + 2.4.1 @@ -41,13 +33,6 @@ org.apache.maven.plugins maven-assembly-plugin - - org.apache.maven.plugins - maven-deploy-plugin - - true - - diff --git a/webcam-capture-drivers/webcam-capture-driver-vlcj/src/example/java/ListPureDevicesExample.java b/webcam-capture-drivers/webcam-capture-driver-vlcj/src/example/java/ListPureDevicesExample.java new file mode 100644 index 00000000..cd60bd00 --- /dev/null +++ b/webcam-capture-drivers/webcam-capture-driver-vlcj/src/example/java/ListPureDevicesExample.java @@ -0,0 +1,19 @@ +import com.github.sarxos.webcam.WebcamDevice; +import com.github.sarxos.webcam.WebcamDriver; +import com.github.sarxos.webcam.ds.vlcj.VlcjDriver; + + +/** + * This class intends to be used only for VLCj Webcam Driver test purpose! + * + * @author Bartosz Firyn (sarxos) + */ +public class ListPureDevicesExample { + + public static void main(String[] args) { + WebcamDriver driver = new VlcjDriver(); + for (WebcamDevice device : driver.getDevices()) { + System.out.println(device); + } + } +} diff --git a/webcam-capture-drivers/webcam-capture-driver-vlcj/src/example/java/ListWebcamsExample.java b/webcam-capture-drivers/webcam-capture-driver-vlcj/src/example/java/ListWebcamsExample.java new file mode 100644 index 00000000..7bcff7d3 --- /dev/null +++ b/webcam-capture-drivers/webcam-capture-driver-vlcj/src/example/java/ListWebcamsExample.java @@ -0,0 +1,33 @@ +import java.util.List; + +import com.github.sarxos.webcam.Webcam; +import com.github.sarxos.webcam.ds.vlcj.VlcjDriver; + + +/** + * This class provides a simple example of how to use VLCj driver to list + * webcams available in the system.
+ *
+ * + * WARNING: It works correctly only in case when used on Linux box. Windows VLCj + * implementation does not support webcam discovery!!! + * + * @author Bartosz Firyn (sarxos) + */ +public class ListWebcamsExample { + + static { + Webcam.setDriver(new VlcjDriver()); + } + + public static void main(String[] args) { + + List webcams = Webcam.getWebcams(); + + System.out.format("Webcams detected: %d \n", webcams.size()); + + for (int i = 0; i < webcams.size(); i++) { + System.out.format("%d: %s \n", i + 1, webcams.get(i)); + } + } +} diff --git a/webcam-capture-drivers/webcam-capture-driver-vlcj/src/example/java/WebcamPanelExample.java b/webcam-capture-drivers/webcam-capture-driver-vlcj/src/example/java/WebcamPanelExample.java new file mode 100644 index 00000000..4200bd57 --- /dev/null +++ b/webcam-capture-drivers/webcam-capture-driver-vlcj/src/example/java/WebcamPanelExample.java @@ -0,0 +1,30 @@ +import javax.swing.JFrame; + +import com.github.sarxos.webcam.Webcam; +import com.github.sarxos.webcam.WebcamPanel; +import com.github.sarxos.webcam.WebcamResolution; +import com.github.sarxos.webcam.ds.vlcj.VlcjDriver; + + +public class WebcamPanelExample { + + static { + Webcam.setDriver(new VlcjDriver()); + } + + public static void main(String[] args) throws InterruptedException { + + Webcam webcam = Webcam.getWebcams().get(0); + webcam.setViewSize(WebcamResolution.VGA.getSize()); + + WebcamPanel panel = new WebcamPanel(webcam); + panel.setFPSDisplayed(true); + + JFrame window = new JFrame("Webcam Panel using VLCj"); + window.add(panel); + window.setResizable(false); + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.pack(); + window.setVisible(true); + } +} \ No newline at end of file diff --git a/webcam-capture-drivers/webcam-capture-driver-vlcj/src/main/java/com/github/sarxos/webcam/ds/vlcj/VlcjDevice.java b/webcam-capture-drivers/webcam-capture-driver-vlcj/src/main/java/com/github/sarxos/webcam/ds/vlcj/VlcjDevice.java index 13d2fc38..63a5a96d 100644 --- a/webcam-capture-drivers/webcam-capture-driver-vlcj/src/main/java/com/github/sarxos/webcam/ds/vlcj/VlcjDevice.java +++ b/webcam-capture-drivers/webcam-capture-driver-vlcj/src/main/java/com/github/sarxos/webcam/ds/vlcj/VlcjDevice.java @@ -2,6 +2,7 @@ import java.awt.Dimension; import java.awt.image.BufferedImage; +import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,8 +13,57 @@ import com.github.sarxos.webcam.WebcamDevice; import com.github.sarxos.webcam.WebcamException; +import com.github.sarxos.webcam.WebcamResolution; +/** + * Just a simple enumeration with supported (not yet confirmed) operating + * systems. + * + * @author Bartosz Firyn (sarxos) + */ +enum OS { + + /** + * Microsoft Windows + */ + WIN, + + /** + * Linux or UNIX. + */ + NIX, + + /** + * Mac OS X + */ + OSX; + + private static OS os = null; + + /** + * Get operating system. + * + * @return OS + */ + public static final OS getOS() { + if (os == null) { + String osp = System.getProperty("os.name").toLowerCase(); + if (osp.indexOf("win") >= 0) { + return WIN; + } else if (osp.indexOf("mac") >= 0) { + return OSX; + } else if (osp.indexOf("nix") >= 0 || osp.indexOf("nux") >= 0) { + return NIX; + } else { + throw new RuntimeException(osp + " is not supported"); + } + } + return os; + } + +} + /** * NOT STABLE, EXPERIMENTAL STUFF!!! * @@ -28,7 +78,9 @@ public class VlcjDevice implements WebcamDevice { */ //@formatter:off private final static Dimension[] DIMENSIONS = new Dimension[] { - new Dimension(320, 240), + WebcamResolution.QQVGA.getSize(), + WebcamResolution.QVGA.getSize(), + WebcamResolution.VGA.getSize(), }; //@formatter:on @@ -75,34 +127,66 @@ public class VlcjDevice implements WebcamDevice { private Dimension size = null; private MediaListItem item = null; + private MediaListItem sub = null; private MediaPlayerFactory factory = null; private MediaPlayer player = null; private volatile boolean open = false; private volatile boolean disposed = false; - public VlcjDevice(MediaListItem item) { + protected VlcjDevice(MediaListItem item) { + + if (item == null) { + throw new IllegalArgumentException("Media list item cannot be null!"); + } + + List subs = item.subItems(); + + if (subs.isEmpty()) { + throw new RuntimeException("Implementation does not support media list items which are empty!"); + } + this.item = item; + this.sub = subs.get(0); } - private String getCapDevice() { + public String getCaptureDevice() { + switch (OS.getOS()) { + case WIN: + return "dshow://"; + case OSX: + return "qtcapture://"; + case NIX: + return "v4l2://"; + default: + throw new RuntimeException("Capture device not supported on " + OS.getOS()); + } + } - String os = System.getProperty("os.name").toLowerCase(); + public MediaListItem getMediaListItem() { + return item; + } - if (os.indexOf("win") >= 0) { - return "dshow://"; - } else if (os.indexOf("mac") >= 0) { - return "qtcapture://"; - } else if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0) { - return "v4l2://"; - } else { - throw new RuntimeException("Not implemented"); - } + public MediaListItem getMediaListItemSub() { + return sub; } @Override public String getName() { - return item.name(); + return sub.name(); + } + + public String getMRL() { + return sub.mrl(); + } + + public String getVDevice() { + return getMRL().replace(getCaptureDevice(), ""); + } + + @Override + public String toString() { + return String.format("%s[%s (%s)]", getClass().getSimpleName(), getName(), getMRL()); } @Override @@ -123,7 +207,7 @@ public void setResolution(Dimension size) { @Override public BufferedImage getImage() { if (!open) { - throw new WebcamException("Cannot get image, player is not open"); + throw new WebcamException("Cannot get image, webcam device is not open"); } return player.getSnapshot(); } @@ -147,13 +231,37 @@ public synchronized void open() { // for nix systems this should be changed dshow -> ... !! - String[] options = { - ":dshow-vdev=" + item.name(), - ":dshow-size=" + size.width + "x" + size.height, - ":dshow-adev=none", // no audio device - }; + String[] options = null; + + switch (OS.getOS()) { + case WIN: + options = new String[] { + ":dshow-vdev=" + getName(), + ":dshow-size=" + size.width + "x" + size.height, + ":dshow-adev=none", // no audio device + }; + break; + case NIX: + options = new String[] { + ":v4l-vdev=" + getVDevice(), + ":v4l-width=" + size.width, + ":v4l-height=" + size.height, + ":v4l-fps=30", + ":v4l-quality=20", + ":v4l-adev=none", // no audio device + }; + break; + case OSX: + options = new String[] { + ":qtcapture-vdev=" + getVDevice(), + ":qtcapture-width=" + size.width, + ":qtcapture-height=" + size.height, + ":qtcapture-adev=none", // no audio device + }; + break; + } - player.startMedia(getCapDevice(), options); + player.startMedia(getMRL(), options); // wait for images @@ -204,4 +312,12 @@ public synchronized void dispose() { public boolean isOpen() { return open; } + + public MediaPlayer getPlayer() { + return player; + } + + public MediaPlayerFactory getFactory() { + return factory; + } } diff --git a/webcam-capture-drivers/webcam-capture-driver-vlcj/src/main/java/com/github/sarxos/webcam/ds/vlcj/VlcjDriver.java b/webcam-capture-drivers/webcam-capture-driver-vlcj/src/main/java/com/github/sarxos/webcam/ds/vlcj/VlcjDriver.java index 64f97f98..be8b99a6 100644 --- a/webcam-capture-drivers/webcam-capture-driver-vlcj/src/main/java/com/github/sarxos/webcam/ds/vlcj/VlcjDriver.java +++ b/webcam-capture-drivers/webcam-capture-driver-vlcj/src/main/java/com/github/sarxos/webcam/ds/vlcj/VlcjDriver.java @@ -39,34 +39,30 @@ public class VlcjDriver implements WebcamDriver { Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class); } - private static List devices = null; - - private static boolean loaded = false; - - private static MediaPlayerFactory mediaPlayerFactory = null; - private static MediaDiscoverer videoMediaDiscoverer = null; - private static MediaList videoDeviceList = null; + public VlcjDriver() { + if (OS.getOS() == OS.WIN) { + System.err.println(String.format("WARNING: %s does not support Windows platform", getClass().getSimpleName())); + } + } @Override public List getDevices() { - if (!loaded) { - mediaPlayerFactory = new MediaPlayerFactory(); - videoMediaDiscoverer = mediaPlayerFactory.newVideoMediaDiscoverer(); - videoDeviceList = videoMediaDiscoverer.getMediaList(); - loaded = true; - } - - if (devices == null) { + MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(); + MediaDiscoverer videoMediaDiscoverer = mediaPlayerFactory.newVideoMediaDiscoverer(); + MediaList videoDeviceList = videoMediaDiscoverer.getMediaList(); - devices = new ArrayList(); + List devices = new ArrayList(); - List videoDevices = videoDeviceList.items(); - for (MediaListItem item : videoDevices) { - devices.add(new VlcjDevice(item)); - } + List videoDevices = videoDeviceList.items(); + for (MediaListItem item : videoDevices) { + devices.add(new VlcjDevice(item)); } + videoDeviceList.release(); + videoMediaDiscoverer.release(); + mediaPlayerFactory.release(); + return devices; }