Skip to content

Commit

Permalink
Add unit tests for VLCj driver
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Jun 11, 2014
1 parent dc33350 commit 4eb1ed8
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 65 deletions.
28 changes: 22 additions & 6 deletions webcam-capture-drivers/driver-vlcj/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<artifactId>webcam-capture-driver-vlcj</artifactId>
<packaging>bundle</packaging>

<name>Webcam Capture - VLCj Driver</name>
<description>Webcam Capture driver using VLCj framework to grab frames from camera device</description>
<name>Webcam Capture - Vlcj Driver</name>
<description>Webcam Capture driver using vlcj library to grab frames from camera device</description>

<dependencies>
<dependency>
Expand All @@ -31,14 +31,30 @@
<artifactId>vlcj</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,9 @@
import com.github.sarxos.webcam.WebcamDevice;
import com.github.sarxos.webcam.WebcamException;
import com.github.sarxos.webcam.WebcamResolution;
import com.github.sarxos.webcam.ds.vlcj.impl.OS;


/**
* 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) {
os = WIN;
} else if (osp.indexOf("mac") >= 0) {
os = OSX;
} else if (osp.indexOf("nix") >= 0 || osp.indexOf("nux") >= 0) {
os = NIX;
} else {
throw new RuntimeException(osp + " is not supported");
}
}
return os;
}

}

/**
* Capture driver which use vlcj project API to fetch images from camera. It
* should not be used when you need performance since vlcj saves snapshot image
Expand All @@ -76,6 +29,9 @@ public static final OS getOS() {
*/
public class VlcjDevice implements WebcamDevice {

/**
* Logger.
*/
private static final Logger LOG = LoggerFactory.getLogger(VlcjDevice.class);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.medialist.MediaList;
Expand All @@ -13,6 +14,7 @@
import com.github.sarxos.webcam.WebcamDevice;
import com.github.sarxos.webcam.WebcamDiscoverySupport;
import com.github.sarxos.webcam.WebcamDriver;
import com.github.sarxos.webcam.ds.vlcj.impl.OS;
import com.sun.jna.Native;


Expand All @@ -25,28 +27,59 @@
*/
public class VlcjDriver implements WebcamDriver, WebcamDiscoverySupport {

static {
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}
/**
* Default webcam discovery scan interval in milliseconds.
*/
public static final long DEFAULT_SCAN_INTERVAL = 3000;

/**
* Are natives initialized.
*/
private static final AtomicBoolean initialized = new AtomicBoolean();

/**
* The scan interval.
*/
private long scanInterval = -1;

public VlcjDriver() {
if (OS.getOS() == OS.WIN) {
System.err.println(String.format("WARNING: %s does not support Windows platform", getClass().getSimpleName()));
}
initialize();
}

/**
* Initialize natives.
*/
protected static void initialize() {
initialize(true);
}

/**
* Initialize natives. If argument is true the natives are being loaded. In
* case of false this method do nothing. It's used mostly in unit tests.
*
* @param load the control to decide whether to load natives or ignore them
*/
protected static void initialize(boolean load) {
if (load && initialized.compareAndSet(false, true)) {
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}
}

@Override
public List<WebcamDevice> getDevices() {
public final List<WebcamDevice> getDevices() {

MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
MediaPlayerFactory mediaPlayerFactory = createMediaPlayerFactory();
MediaDiscoverer videoMediaDiscoverer = mediaPlayerFactory.newVideoMediaDiscoverer();
MediaList videoDeviceList = videoMediaDiscoverer.getMediaList();

List<WebcamDevice> devices = new ArrayList<WebcamDevice>();

List<MediaListItem> videoDevices = videoDeviceList.items();

for (MediaListItem item : videoDevices) {
devices.add(new VlcjDevice(item));
devices.add(mediaListItemToDevice(item));
}

videoDeviceList.release();
Expand All @@ -56,6 +89,25 @@ public List<WebcamDevice> getDevices() {
return devices;
}

/**
* Converts media list itemn into webcam device.
*
* @param item the item to be converted to webcam device instance
* @return Webcam device created from media list item
*/
protected WebcamDevice mediaListItemToDevice(MediaListItem item) {
return new VlcjDevice(item);
}

/**
* Creates media player factory.
*
* @return New media player factory
*/
protected MediaPlayerFactory createMediaPlayerFactory() {
return new MediaPlayerFactory();
}

@Override
public boolean isThreadSafe() {
return false;
Expand All @@ -68,7 +120,22 @@ public String toString() {

@Override
public long getScanInterval() {
return 3000;
if (scanInterval <= 0) {
return DEFAULT_SCAN_INTERVAL;
}
return scanInterval;
}

/**
* Set new scan interval. Value must be positive number. If negative or zero
* is used, then the corresponding getter will return default scan interval
* value.
*
* @param scanInterval the new scan interval in milliseconds
* @see VlcjDriver#DEFAULT_SCAN_INTERVAL
*/
public void setScanInterval(long scanInterval) {
this.scanInterval = scanInterval;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.sarxos.webcam.ds.vlcj.impl;

/**
* Just a simple enumeration with supported (not yet confirmed) operating
* systems.
*
* @author Bartosz Firyn (sarxos)
*/
public 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) {
os = WIN;
} else if (osp.indexOf("mac") >= 0) {
os = OSX;
} else if (osp.indexOf("nix") >= 0 || osp.indexOf("nux") >= 0) {
os = NIX;
} else {
throw new RuntimeException(osp + " is not supported");
}
}
return os;
}
}
Loading

0 comments on commit 4eb1ed8

Please sign in to comment.