Skip to content

Commit

Permalink
Merge 640b80f into 5874815
Browse files Browse the repository at this point in the history
  • Loading branch information
tirz committed Jul 20, 2019
2 parents 5874815 + 640b80f commit 2a5d203
Show file tree
Hide file tree
Showing 104 changed files with 429 additions and 640 deletions.
Expand Up @@ -30,8 +30,8 @@ public class FFmpegCliDevice implements WebcamDevice, WebcamDevice.BufferAccess
private Dimension[] resolutions = null;
private Dimension resolution = null;

private AtomicBoolean open = new AtomicBoolean(false);
private AtomicBoolean disposed = new AtomicBoolean(false);
private final AtomicBoolean open = new AtomicBoolean(false);
private final AtomicBoolean disposed = new AtomicBoolean(false);

protected FFmpegCliDevice(String path, File vfile, String resolutions) {
this(path, vfile.getAbsolutePath(), resolutions);
Expand Down Expand Up @@ -108,7 +108,7 @@ private Dimension[] readResolutions(String res) {
resolutions.add(new Dimension(Integer.parseInt(xy[0]), Integer.parseInt(xy[1])));
}

return resolutions.toArray(new Dimension[resolutions.size()]);
return resolutions.toArray(new Dimension[0]);
}

@Override
Expand Down
Expand Up @@ -3,9 +3,7 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -48,9 +46,6 @@ private List<WebcamDevice> getUnixDevices() {

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

String line = null;
BufferedReader br = null;

for (File vfile : vfiles) {

String[] cmd = new String[] {
Expand All @@ -63,36 +58,28 @@ private List<WebcamDevice> getUnixDevices() {

Process process = startProcess(cmd);

InputStream is = process.getInputStream();
br = new BufferedReader(new InputStreamReader(is));

final String STARTER = "[" + FFmpegCliDriver.getCaptureDriver();
final String MARKER = "] Raw";

try {
String line = null;
try (final BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
while ((line = br.readLine()) != null) {
if (line.startsWith(STARTER) && line.indexOf(MARKER) != -1) {
if (line.startsWith(STARTER) && line.contains(MARKER)) {
LOG.debug("Command stdout line: {}", line);
String resolutions = line.split(" : ")[3].trim();
devices.add(new FFmpegCliDevice(path, vfile, resolutions));
break;
}
}

} catch (IOException e) {
throw new WebcamException(e);
} finally {
try {
is.close();
} catch (IOException e) {
throw new WebcamException(e);
}
process.destroy();
try {
process.waitFor();
} catch (InterruptedException e) {
throw new WebcamException(e);
}
}

process.destroy();
try {
process.waitFor();
} catch (InterruptedException e) {
throw new WebcamException(e);
}
}
return devices;
Expand Down Expand Up @@ -123,29 +110,17 @@ private WebcamDevice buildWindowsDevice(String deviceName) {

Set<String> resolutions = new LinkedHashSet<>();

InputStream is = null;
BufferedReader br;
String line;
try {
is = listDevicesProcess.getInputStream();
br = new BufferedReader(new InputStreamReader(is));

try (final BufferedReader br = new BufferedReader(new InputStreamReader(listDevicesProcess.getInputStream()))) {
while ((line = br.readLine()) != null) {
if (line.startsWith(STARTER) && line.indexOf(MARKER) != -1) {
if (line.startsWith(STARTER) && line.contains(MARKER)) {
int begin = line.indexOf(MARKER) + MARKER.length();
String resolution = line.substring(begin, line.indexOf(" ", begin));
resolutions.add(resolution);
}
}

} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

StringBuilder vinfo = new StringBuilder();
Expand All @@ -169,40 +144,28 @@ private List<String> getWindowsDevicesNames() {

boolean startDevices = false;

InputStream is = null;
BufferedReader br;
String line;
try {
is = listDevicesProcess.getInputStream();
br = new BufferedReader(new InputStreamReader(is));

try (final BufferedReader br = new BufferedReader(new InputStreamReader(listDevicesProcess.getInputStream()))) {
while ((line = br.readLine()) != null) {
if (line.startsWith(STARTER) && line.indexOf(VIDEO_MARKER) != -1) {
if (line.startsWith(STARTER) && line.contains(VIDEO_MARKER)) {
startDevices = true;
continue;
}
if (startDevices) {
if (line.startsWith(STARTER) && line.indexOf(NAME_MARKER) != -1) {
if (line.startsWith(STARTER) && line.contains(NAME_MARKER)) {
String deviceName = line.substring(line.indexOf(NAME_MARKER) + NAME_MARKER.length());
// Remove final double quotes
deviceName = deviceName.substring(0, deviceName.length() - 1);
devicesNames.add(deviceName);
continue;
}
if (line.startsWith(STARTER) && line.indexOf(AUDIO_MARKER) != -1) {
if (line.startsWith(STARTER) && line.contains(AUDIO_MARKER)) {
break;
}
}
}

} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

return devicesNames;
Expand All @@ -211,7 +174,6 @@ private List<String> getWindowsDevicesNames() {
private Process startProcess(String[] cmd) {
Process process = null;

OutputStream os;
if (LOG.isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
for (String c : cmd) {
Expand All @@ -224,18 +186,11 @@ private Process startProcess(String[] cmd) {
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
process = builder.start();
process.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
}

os = process.getOutputStream();

try {
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}

return process;
}

Expand Down
Expand Up @@ -15,6 +15,7 @@ public boolean accept(File dir, String name) {

public File[] getVideoFiles() {
String[] names = DEV.list(this);
assert names != null;
File[] files = new File[names.length];

for (int i = 0; i < names.length; i++) {
Expand Down
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
Expand Down Expand Up @@ -105,11 +106,11 @@ public void run() {
private Dimension resolution = null;
private Process process = null;
private File pipe = null;
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
private DataInputStream dis = null;

private AtomicBoolean open = new AtomicBoolean(false);
private AtomicBoolean disposed = new AtomicBoolean(false);
private final AtomicBoolean open = new AtomicBoolean(false);
private final AtomicBoolean disposed = new AtomicBoolean(false);

private String logFilePathString;
private int frames = 1;
Expand Down Expand Up @@ -212,18 +213,11 @@ public BufferedImage getImage() {
throw new RuntimeException(e);
}

ByteArrayInputStream bais = new ByteArrayInputStream(readBytes());
try {
try (final ByteArrayInputStream bais = new ByteArrayInputStream(Objects.requireNonNull(readBytes()))) {
image = ImageIO.read(bais);
} catch (IOException e) {
process.destroy();
throw new RuntimeException(e);
} finally {
try {
bais.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

process.waitFor();
Expand Down Expand Up @@ -288,7 +282,7 @@ private void executeFsWebcamProcess() throws IOException {
c.add(pipe.getAbsolutePath()); // output file (pipe)
//@formatter:on

String[] cmd = c.toArray(new String[c.size()]);
String[] cmd = c.toArray(new String[0]);

if (LOG.isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -340,7 +334,9 @@ public synchronized void open() {
} catch (InterruptedException e) {
return;
} finally {
p.destroy();
if (p != null) {
p.destroy();
}
}
}

Expand All @@ -361,12 +357,11 @@ public synchronized void close() {

if (process != null) {
process.destroy();
}

try {
process.waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
try {
process.waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

if (!pipe.delete()) {
Expand Down
Expand Up @@ -16,6 +16,7 @@ public boolean accept(File dir, String name) {
public File[] getVideoFiles() {

String[] names = DEV.list(this);
assert names != null;
File[] files = new File[names.length];

for (int i = 0; i < names.length; i++) {
Expand Down
Expand Up @@ -111,11 +111,10 @@ public void open() {

final String name = getName();
final Dimension resolution = getResolution();
final String str = new StringBuilder(MIME_VIDEO_X_RAW)
.append(",")
.append("width=").append(resolution.width).append(",")
.append("height").append(resolution.height)
.toString();
final String str = MIME_VIDEO_X_RAW +
"," +
"width=" + resolution.width + "," +
"height" + resolution.height;
final Caps caps = Caps.fromString(str);

LOG.debug("Opening device {} with caps {}", name, caps);
Expand Down
Expand Up @@ -56,7 +56,7 @@ public Gst1Driver() {
}
}

private static final void init() {
private static void init() {
String[] args = new String[] {};
Gst.init(Gst1Driver.class.getSimpleName(), args);
Runtime.getRuntime().addShutdownHook(new GStreamerShutdownHook());
Expand Down
Expand Up @@ -10,8 +10,10 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import org.bridj.Platform;
import org.gstreamer.Caps;
Expand Down Expand Up @@ -75,10 +77,10 @@ public class GStreamerDevice implements WebcamDevice, RGBDataSink.Listener, Webc

/* 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 final AtomicBoolean open = new AtomicBoolean(false);
private final AtomicBoolean disposed = new AtomicBoolean(false);
private final AtomicBoolean starting = new AtomicBoolean(false);
private final AtomicBoolean initialized = new AtomicBoolean(false);
private Dimension resolution = WebcamResolution.VGA.getSize();
private BufferedImage image = null;

Expand All @@ -87,7 +89,7 @@ public class GStreamerDevice implements WebcamDevice, RGBDataSink.Listener, Webc
private long t1 = -1;
private long t2 = -1;

private volatile double fps = 0;
private final AtomicReference<Double> fps = new AtomicReference<Double>(0.0);

/**
* Create GStreamer webcam device.
Expand Down Expand Up @@ -123,7 +125,7 @@ private synchronized void init() {
if (Platform.isWindows()) {
source.set("device-index", deviceIndex);
} else if (Platform.isLinux()) {
source.set("device", videoFile.getAbsolutePath());
source.set("device", Objects.requireNonNull(videoFile).getAbsolutePath());
} else if (Platform.isMacOSX()) {
throw new IllegalStateException("not yet implemented");
}
Expand Down Expand Up @@ -228,7 +230,7 @@ public String getName() {
if (Platform.isWindows()) {
return Integer.toString(deviceIndex);
} else if (Platform.isLinux()) {
return videoFile.getAbsolutePath();
return Objects.requireNonNull(videoFile).getAbsolutePath();
} else {
throw new RuntimeException("Platform not supported by GStreamer capture driver");
}
Expand Down Expand Up @@ -409,12 +411,12 @@ public void rgbFrame(boolean preroll, int width, int height, IntBuffer rgb) {
t1 = t2;
t2 = System.currentTimeMillis();

fps = (4 * fps + 1000 / (t2 - t1 + 1)) / 5;
fps.set((4 * fps.get() + 1000 / (t2 - t1 + 1)) / 5);
}

@Override
public double getFPS() {
return fps;
return fps.get();
}

public Pipeline getPipe() {
Expand Down

0 comments on commit 2a5d203

Please sign in to comment.