@@ -23,13 +23,13 @@ public class Webcam {
23
23
24
24
private static final Logger LOG = LoggerFactory .getLogger (Webcam .class );
25
25
26
- //@formatter:off
26
+ // @formatter:off
27
27
private static final String [] DRIVERS_DEFAULT = new String [] {
28
28
"com.github.sarxos.webcam.ds.openimaj.OpenImajDriver" ,
29
29
"com.github.sarxos.webcam.ds.civil.LtiCivilDriver" ,
30
30
"com.github.sarxos.webcam.ds.jmf.JmfDriver" ,
31
31
};
32
- //@formatter:on
32
+ // @formatter:on
33
33
34
34
private static final List <String > DRIVERS_LIST = new ArrayList <String >(Arrays .asList (DRIVERS_DEFAULT ));
35
35
private static final List <Class <?>> DRIVERS_CLASS_LIST = new ArrayList <Class <?>>();
@@ -54,6 +54,11 @@ public void run() {
54
54
private static WebcamDriver driver = null ;
55
55
private static List <Webcam > webcams = null ;
56
56
57
+ /**
58
+ * Is automated deallocation on TERM signal enabled.
59
+ */
60
+ private static volatile boolean deallocOnTermSignal = false ;
61
+
57
62
/**
58
63
* Webcam listeners.
59
64
*/
@@ -144,7 +149,7 @@ public synchronized void close() {
144
149
}
145
150
146
151
/**
147
- * Close webcam.
152
+ * Close webcam (internal impl) .
148
153
*/
149
154
private void close0 () {
150
155
@@ -285,7 +290,14 @@ public static List<Webcam> getWebcams() {
285
290
driver = new WebcamDefaultDriver ();
286
291
}
287
292
288
- for (WebcamDevice device : driver .getDevices ()) {
293
+ List <WebcamDevice > devices = driver .getDevices ();
294
+
295
+ if (deallocOnTermSignal ) {
296
+ LOG .warn ("Automated deallocation on TERM signal is enabled!" );
297
+ WebcamDeallocator .store (devices .toArray (new WebcamDevice [devices .size ()]));
298
+ }
299
+
300
+ for (WebcamDevice device : devices ) {
289
301
webcams .add (new Webcam (device ));
290
302
}
291
303
@@ -462,4 +474,19 @@ protected void dispose() {
462
474
device .close ();
463
475
device .dispose ();
464
476
}
477
+
478
+ /**
479
+ * <b>CAUTION!!!</b><br>
480
+ * <br>
481
+ * This is experimental feature to be used mostly in in development phase.
482
+ * After you set handle term signal to true, and fetch capture devices,
483
+ * Webcam Capture API will listen for TERM signal and try to close all
484
+ * devices after it has been received. <b>This feature can be unstable on
485
+ * some systems!</b>
486
+ *
487
+ * @param on
488
+ */
489
+ public static void handleTermSignal (boolean on ) {
490
+ deallocOnTermSignal = on ;
491
+ }
465
492
}
0 commit comments