Skip to content

Commit

Permalink
GStreamer 1.0 capture driver, continuation 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Dec 4, 2016
1 parent 4750d89 commit 44e7b2e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ public BufferedImage getImage() {

initializer.initialize();

LOG.debug("Device {} get image", getName());
LOG.trace("Device {} get image", getName());

try {
return exchanger.exchange(null);
Expand Down Expand Up @@ -419,6 +419,8 @@ public void dispose() {

close();

LOG.debug("Teardowning device {}", getName());

initializer.teardown();

GsUtils.dispose(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public List<WebcamDevice> getDevices() {

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

final String factory = GsUtils.getCompatibleSourceName();
final String factory = GsUtils.getCompatibleSourceFactory();
final Element source = ElementFactory.make(factory, "source");

try {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public AppSinkNewSampleListener(Exchanger<BufferedImage> exchanger) {

public void rgbFrame(boolean isPrerollFrame, int width, int height, IntBuffer rgb) {

LOG.debug("RGB frame ({}x{}), preroll is {}", width, height, isPrerollFrame);
LOG.trace("RGB frame ({}x{}), preroll is {}", width, height, isPrerollFrame);

final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
final int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
Expand All @@ -54,7 +54,7 @@ public void rgbFrame(boolean isPrerollFrame, int width, int height, IntBuffer rg
@Override
public FlowReturn newSample(AppSink elem) {

LOG.debug("New sample ready in {}", elem);
LOG.trace("New sample ready in {}", elem);

final Sample sample = elem.pullSample();
final Structure capsStruct = sample.getCaps().getStructure(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.sarxos.webcam.ds.gst1.impl;

import java.io.File;


public class GsPlatform {

private static final String OS_NAME = System.getProperty("os.name", "");

public enum OS {
WINDOWS,
LINUX,
MACOS,
}

private static OS os;
static {
if (GsPlatform.isLinux()) {
os = OS.LINUX;
}
if (GsPlatform.isWindows()) {
os = OS.WINDOWS;
}
if (GsPlatform.isMacOSX()) {
os = OS.MACOS;
}
}

public static boolean isUnix() {
return File.separatorChar == '/';
}

public static boolean isWindows() {
return File.separatorChar == '\\';
}

public static boolean isLinux() {
return isUnix() && OS_NAME.toLowerCase().contains("linux");
}

public static boolean isMacOSX() {
return isUnix() && (OS_NAME.startsWith("Mac") || OS_NAME.startsWith("Darwin"));
}

public static boolean isSolaris() {
return isUnix() && (OS_NAME.startsWith("SunOS") || OS_NAME.startsWith("Solaris"));
}

public static OS getOs() {
return os;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,100 +3,62 @@
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.bridj.Platform;
import org.freedesktop.gstreamer.Caps;
import org.freedesktop.gstreamer.Element;
import org.freedesktop.gstreamer.ElementFactory;
import org.freedesktop.gstreamer.State;
import org.freedesktop.gstreamer.StateChangeReturn;
import org.freedesktop.gstreamer.Structure;

import com.github.sarxos.webcam.WebcamException;


public class GsUtils {

public enum OS {
WINDOWS,
LINUX,
MACOS,
}

private static OS os;
static {
if (Platform.isLinux()) {
os = OS.LINUX;
}
if (Platform.isWindows()) {
os = OS.WINDOWS;
}
if (Platform.isMacOSX()) {
os = OS.MACOS;
}
}

public static OS getOs() {
return os;
public static Element getCompatibleSource(String name) {
final Element source = ElementFactory.make(getCompatibleSourceFactory(), name + "-source");
source.set(getCompatibleSourceFactory(), name);
return source;
}

public static Element createCompatibleSource(String name) {
switch (getOs()) {
public static String getCompatibleSourceFactory() {
switch (GsPlatform.getOs()) {
case LINUX:
return createLinuxSource(name);
return "v4l2src";
case WINDOWS:
return createWindowsSource(name);
return "ksvideosrc";
case MACOS:
return createMacOsSource(name);
return "qtkitvideosrc";
default:
throw new WebcamException("This operating system is not supported");
}
}

public static String getCompatibleSourceName() {
switch (getOs()) {
public static String getCompatibleSourceProperty() {
switch (GsPlatform.getOs()) {
case LINUX:
return "v4l2src";
return "device";
case WINDOWS:
return "dshowvideosrc";
case MACOS:
return "qtkitvideosrc";
return "device-index";
default:
throw new WebcamException("This operating system is not supported");
}
}

private static Element createLinuxSource(String name) {
final Element source = createSource(name);
source.set("device", name);
return source;
}

private static Element createWindowsSource(String name) {
final Element source = createSource(name);
source.set("device-name", name);
return source;
}

private static Element createMacOsSource(String name) {
final Element source = createSource(name);
source.set("device-index", name);
return source;
}

private static Element createSource(String name) {
return ElementFactory.make(getCompatibleSourceName(), name + "-source");
}

public static Dimension capsStructureToResolution(Structure structure) {

int w = -1;
int h = -1;

if (Platform.isWindows()) {
if (GsPlatform.isWindows()) {
w = structure.getRange("width").getMinInt();
h = structure.getRange("height").getMinInt();
} else if (Platform.isLinux()) {
} else if (GsPlatform.isLinux()) {
w = structure.getInteger("width");
h = structure.getInteger("height");
}
Expand Down Expand Up @@ -136,4 +98,42 @@ public static void dispose(Element element) {
element.dispose();
}
}

private String getCompatibleSourceIdentifier(int id) {
switch (GsPlatform.getOs()) {
case LINUX:
return "/dev/video" + id;
case WINDOWS:
case MACOS:
return Integer.toString(id);
default:
throw new WebcamException("This operating system is not supported");
}
}

public List<String> getVideoIdentifiers() {

final List<String> ids = new ArrayList<>();

for (int i = 0; i < 50; i++) {

final String id = getCompatibleSourceIdentifier(i);
final String property = getCompatibleSourceProperty();
final Element source = getCompatibleSource(id);

source.set(property, id);
source.setState(State.NULL);

try {
if (source.setState(State.READY) != StateChangeReturn.FAILURE) {
ids.add(id);
}
} finally {
source.setState(State.NULL);
source.dispose();
}
}

return ids;
}
}

0 comments on commit 44e7b2e

Please sign in to comment.