Skip to content

Commit

Permalink
Continous GStreamer driver enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed May 9, 2013
1 parent 2915081 commit 259e602
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 18 deletions.
30 changes: 30 additions & 0 deletions webcam-capture-drivers/webcam-capture-driver-gstreamer/README.md
@@ -0,0 +1,30 @@
# webcam-capture-driver-gstreamer

This is GStreamer driver for Webcam Capture project. It allows Webcam Capture to
handle pictures from build-in or USB-connected webcams. It has been designed to
work with Windows **only**. To make use of it user have to
[download](http://code.google.com/p/ossbuild/) and install GStreamer application.

Currently supported GStreamer version is 0.10.x, so make sure you are installing
the correct one! It is **not** compatible with GStreamer 1.0 and above!


## Download

Below is the newest stable version ZIP containing main project
JAR with additional documents, examples and all required 3rd-party
dependencies:

* **Latest stable version** - [nothing here yet]


## License

Copyright (C) 2012 - 2013 Bartosz Firyn

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Expand Up @@ -29,41 +29,77 @@ public class GStreamerDevice implements WebcamDevice, RGBDataSink.Listener {
*/
private static final long LATENESS = 20; // ms

/**
* Video format to capture.
*/
private static final String FORMAT = "video/x-raw-yuv";

/**
* All possible resolutions - populated while initialization phase.
*/
private Dimension[] resolutions = null;

/**
* Device name, immutable.
*/
private final String name;
private final Dimension[] resolutions;

/* gstreamer stuff */

private Pipeline pipe = null;
private Element source = null;
private Element filter = null;
private RGBDataSink sink = null;
private BufferedImage image = null;
private Caps caps = null;

/* logic */

private AtomicBoolean open = new AtomicBoolean(false);
private AtomicBoolean disposed = new AtomicBoolean(false);
private AtomicBoolean starting = new AtomicBoolean(false);
private AtomicBoolean initialized = new AtomicBoolean(false);
private Dimension resolution = WebcamResolution.VGA.getSize();
private BufferedImage image = null;

/**
* Create GStreamer webcam device.
*
* @param name the name of webcam device
*/
protected GStreamerDevice(String name) {

this.name = name;
}

/**
* Initialize webcam device.
*/
private synchronized void init() {

if (!initialized.compareAndSet(false, true)) {
return;
}

pipe = new Pipeline(name);

source = ElementFactory.make("dshowvideosrc", "source");
source.set("device-name", name);

sink = new RGBDataSink(name, this);
sink.setPassDirectBuffer(true);
sink.getSinkElement().setMaximumLateness(LATENESS, TimeUnit.MILLISECONDS);
sink.getSinkElement().setQOSEnabled(true);

source = ElementFactory.make("dshowvideosrc", "source");
source.set("device-name", name);

filter = ElementFactory.make("capsfilter", "filter");

resolutions = parseResolutions(source.getPads().get(0));
}

/**
* Use GStreamer to get all possible resolutions.
*
* @param pad the pad to get resolutions from
* @return Array of resolutions supported by device connected with pad
*/
private static final Dimension[] parseResolutions(Pad pad) {

List<Dimension> dimensions = new ArrayList<Dimension>();
Expand Down Expand Up @@ -102,6 +138,7 @@ public String getName() {

@Override
public Dimension[] getResolutions() {
init();
return resolutions;
}

Expand All @@ -127,6 +164,10 @@ public void open() {
return;
}

init();

starting.set(true);

Dimension size = getResolution();

image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Expand All @@ -144,6 +185,15 @@ public void open() {
pipe.addMany(source, filter, sink);
Element.linkMany(source, filter, sink);
pipe.setState(State.PLAYING);

// wait max 20s for image to appear
synchronized (this) {
try {
this.wait(20000);
} catch (InterruptedException e) {
return;
}
}
}

@Override
Expand All @@ -153,6 +203,8 @@ public void close() {
return;
}

image = null;

pipe.setState(State.NULL);
Element.unlinkMany(source, filter, sink);
pipe.removeMany(source, filter, sink);
Expand Down Expand Up @@ -189,5 +241,11 @@ public void rgbFrame(boolean preroll, int width, int height, IntBuffer rgb) {
rgb.get(((DataBufferInt) tmp.getRaster().getDataBuffer()).getData(), 0, width * height);

image = tmp;

if (starting.compareAndSet(true, false)) {
synchronized (this) {
this.notifyAll();
}
}
}
}
Expand Up @@ -2,6 +2,7 @@

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

import javax.swing.JFrame;

Expand All @@ -15,18 +16,16 @@
import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamDevice;
import com.github.sarxos.webcam.WebcamDriver;
import com.github.sarxos.webcam.WebcamException;
import com.github.sarxos.webcam.WebcamPanel;
import com.sun.jna.NativeLibrary;
import com.sun.jna.Platform;


public class GStreamerDriver implements WebcamDriver {

private static final Logger LOG = LoggerFactory.getLogger(GStreamerDriver.class);

static {
init();
}

private static final class GStreamerShutdownHook extends Thread {

public GStreamerShutdownHook() {
Expand All @@ -40,9 +39,39 @@ public void run() {
}
}

private static final AtomicBoolean INITIALIZED = new AtomicBoolean(false);

public GStreamerDriver() {
if (INITIALIZED.compareAndSet(false, true)) {
init();
}
}

private static final void init() {

if (!Platform.isWindows()) {
throw new WebcamException(String.format("%s has been designed to work only on Windows platforms", GStreamerDriver.class.getSimpleName()));
}

LOG.debug("GStreamer initialization");
NativeLibrary.addSearchPath("gstreamer-0.10", "C:\\Program Files\\OSSBuild\\GStreamer\\v0.10.6\\bin");

String gpath = null;

for (String path : System.getenv("PATH").split(";")) {
LOG.trace("Search %PATH% for gstreamer bin {}", path);
if (path.indexOf("GStreamer\\v0.10.") != -1) {
gpath = path;
break;
}
}

if (gpath != null) {
LOG.debug("Add bin directory to JNA search paths {}", gpath);
NativeLibrary.addSearchPath("gstreamer-0.10", gpath);
} else {
throw new WebcamException("GStreamer has not been installed or not in %PATH%");
}

Gst.init(GStreamerDriver.class.getSimpleName(), new String[0]);
Runtime.getRuntime().addShutdownHook(new GStreamerShutdownHook());
}
Expand All @@ -53,14 +82,17 @@ public List<WebcamDevice> getDevices() {
List<WebcamDevice> devices = new ArrayList<WebcamDevice>();

Element dshowsrc = ElementFactory.make("dshowvideosrc", "source");
PropertyProbe probe = PropertyProbe.wrap(dshowsrc);

for (Object name : probe.getValues("device-name")) {
devices.add(new GStreamerDevice(name.toString()));
try {
PropertyProbe probe = PropertyProbe.wrap(dshowsrc);
for (Object name : probe.getValues("device-name")) {
devices.add(new GStreamerDevice(name.toString()));
}
} finally {
if (dshowsrc != null) {
dshowsrc.dispose();
}
}

dshowsrc.dispose();

return devices;
}

Expand All @@ -70,6 +102,7 @@ public boolean isThreadSafe() {
}

public static void main(String[] args) {

// WebcamDriver driver = new GStreamerDriver();
// for (WebcamDevice device : driver.getDevices()) {
// System.out.println(device.getName());
Expand All @@ -80,9 +113,12 @@ public static void main(String[] args) {

Webcam.setDriver(new GStreamerDriver());
JFrame frame = new JFrame();
frame.add(new WebcamPanel(Webcam.getWebcams().get(1)));
WebcamPanel panel = new WebcamPanel(Webcam.getWebcams().get(1));
panel.setFPSDisplayed(true);
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
}

0 comments on commit 259e602

Please sign in to comment.