Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deallocate devices on TERM signal (experimental feature)
closes #10
- Loading branch information
sarxos
committed
Jan 7, 2013
1 parent
064efa2
commit 9c8acf3
Showing
8 changed files
with
225 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>webcam-capture</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
</natures> | ||
</projectDescription> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>webcam-capture-parent</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.m2e.core.maven2Builder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.m2e.core.maven2Nature</nature> | ||
</natures> | ||
</projectDescription> |
57 changes: 26 additions & 31 deletions
57
webcam-capture-examples/webcam-capture-manycams/.classpath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" output="target/classes" path="src/main/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="src" output="target/test-classes" path="src/test/java"> | ||
<attributes> | ||
<attribute name="optional" value="true"/> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | ||
<attributes> | ||
<attribute name="maven.pomderived" value="true"/> | ||
</attributes> | ||
</classpathentry> | ||
<classpathentry kind="output" path="target/classes"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamDeallocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.github.sarxos.webcam; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.PrintStream; | ||
import java.util.Observable; | ||
import java.util.Observer; | ||
|
||
|
||
|
||
/** | ||
* Deallocator which goal is to release all devices resources when SIGTERM | ||
* signal is detected. | ||
* | ||
* @author Bartosz Firyn (SarXos) | ||
*/ | ||
class WebcamDeallocator implements Observer { | ||
|
||
private WebcamDevice[] devices = null; | ||
private WebcamSignalHandler handler = new WebcamSignalHandler(); | ||
|
||
/** | ||
* This constructor is used internally to create new deallocator for the | ||
* given devices array. | ||
* | ||
* @param devices the devices to be stored in deallocator | ||
*/ | ||
private WebcamDeallocator(WebcamDevice[] devices) { | ||
if (devices != null && devices.length > 0) { | ||
this.devices = devices; | ||
this.handler.listen("TERM", this); | ||
} | ||
} | ||
|
||
/** | ||
* Store devices to be deallocated when TERM signal has been received. | ||
* | ||
* @param devices the devices array to be stored by deallocator | ||
*/ | ||
protected static final void store(WebcamDevice[] devices) { | ||
new WebcamDeallocator(devices); | ||
} | ||
|
||
@Override | ||
public void update(Observable observable, Object object) { | ||
for (WebcamDevice device : devices) { | ||
try { | ||
device.close(); | ||
} catch (Throwable t) { | ||
caugh(t); | ||
} | ||
} | ||
} | ||
|
||
public void caugh(Throwable e) { | ||
File f = new File(String.format("webcam-capture-hs-%s", System.currentTimeMillis())); | ||
PrintStream ps = null; | ||
try { | ||
e.printStackTrace(ps = new PrintStream(f)); | ||
} catch (FileNotFoundException e2) { | ||
// ignore, stdout is not working, cannot do anything more | ||
} finally { | ||
if (ps != null) { | ||
ps.close(); | ||
} | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
webcam-capture/src/main/java/com/github/sarxos/webcam/WebcamSignalHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.github.sarxos.webcam; | ||
|
||
import java.util.Observable; | ||
import java.util.Observer; | ||
|
||
import sun.misc.Signal; | ||
import sun.misc.SignalHandler; | ||
|
||
|
||
/** | ||
* Primitive signal handler. This class is using undocumented classes from | ||
* sun.misc.* and therefore should be used with caution. | ||
* | ||
* @author Bartosz Firyn (SarXos) | ||
*/ | ||
@SuppressWarnings("restriction") | ||
class WebcamSignalHandler extends Observable implements SignalHandler { | ||
|
||
private SignalHandler handler = null; | ||
|
||
public void listen(String signal, Observer observer) throws IllegalArgumentException { | ||
addObserver(observer); | ||
handler = Signal.handle(new Signal(signal), this); | ||
} | ||
|
||
@Override | ||
public void handle(final sun.misc.Signal signal) { | ||
|
||
// do nothing on "signal default" or "signal ignore" | ||
if (handler == SIG_DFL || handler == SIG_IGN) { | ||
return; | ||
} | ||
|
||
setChanged(); | ||
|
||
try { | ||
notifyObservers(signal); | ||
} finally { | ||
handler.handle(signal); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters