mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2025-11-05 11:56:37 +00:00
use semaphore to ensure last webcam service task has completed before closing stream
This commit is contained in:
parent
74c298fd93
commit
3fdf093a26
1 changed files with 19 additions and 1 deletions
|
|
@ -27,6 +27,8 @@ import java.awt.image.BufferedImage;
|
||||||
import java.awt.image.WritableRaster;
|
import java.awt.image.WritableRaster;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
@ -34,6 +36,8 @@ import java.util.stream.Stream;
|
||||||
public class WebcamService extends ScheduledService<Image> {
|
public class WebcamService extends ScheduledService<Image> {
|
||||||
private static final Logger log = LoggerFactory.getLogger(WebcamService.class);
|
private static final Logger log = LoggerFactory.getLogger(WebcamService.class);
|
||||||
|
|
||||||
|
private final Semaphore taskSemaphore = new Semaphore(1, true);
|
||||||
|
|
||||||
private List<CaptureDevice> devices;
|
private List<CaptureDevice> devices;
|
||||||
private List<CaptureDevice> availableDevices;
|
private List<CaptureDevice> availableDevices;
|
||||||
private Set<WebcamResolution> resolutions;
|
private Set<WebcamResolution> resolutions;
|
||||||
|
|
@ -106,6 +110,7 @@ public class WebcamService extends ScheduledService<Image> {
|
||||||
return new Task<>() {
|
return new Task<>() {
|
||||||
@Override
|
@Override
|
||||||
protected Image call() throws Exception {
|
protected Image call() throws Exception {
|
||||||
|
taskSemaphore.acquire();
|
||||||
try {
|
try {
|
||||||
if(devices == null) {
|
if(devices == null) {
|
||||||
devices = capture.getDevices();
|
devices = capture.getDevices();
|
||||||
|
|
@ -207,6 +212,7 @@ public class WebcamService extends ScheduledService<Image> {
|
||||||
return image;
|
return image;
|
||||||
} finally {
|
} finally {
|
||||||
opening.set(false);
|
opening.set(false);
|
||||||
|
taskSemaphore.release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -221,12 +227,24 @@ public class WebcamService extends ScheduledService<Image> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean cancel() {
|
public boolean cancel() {
|
||||||
|
boolean cancelled = super.cancel();
|
||||||
|
|
||||||
|
try {
|
||||||
|
if(taskSemaphore.tryAcquire(1, TimeUnit.SECONDS)) {
|
||||||
|
taskSemaphore.release();
|
||||||
|
} else {
|
||||||
|
log.error("Timed out waiting for task semaphore to be available to cancel, cancelling anyway");
|
||||||
|
}
|
||||||
|
} catch(InterruptedException e) {
|
||||||
|
log.error("Interrupted while waiting for task semaphore to be available to cancel, cancelling anyway");
|
||||||
|
}
|
||||||
|
|
||||||
if(stream != null) {
|
if(stream != null) {
|
||||||
stream.close();
|
stream.close();
|
||||||
opened.set(false);
|
opened.set(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
return super.cancel();
|
return cancelled;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue