mirror of
https://github.com/sparrowwallet/sparrow.git
synced 2025-11-05 11:56:37 +00:00
remove possibility of task queueing in webcam service
This commit is contained in:
parent
d7d23f9b58
commit
b0d0514617
2 changed files with 19 additions and 3 deletions
|
|
@ -29,6 +29,7 @@ import java.util.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Semaphore;
|
import java.util.concurrent.Semaphore;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
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;
|
||||||
|
|
@ -36,7 +37,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 final Semaphore taskSemaphore = new Semaphore(1);
|
||||||
|
private final AtomicBoolean cancelRequested = new AtomicBoolean(false);
|
||||||
|
|
||||||
private List<CaptureDevice> devices;
|
private List<CaptureDevice> devices;
|
||||||
private List<CaptureDevice> availableDevices;
|
private List<CaptureDevice> availableDevices;
|
||||||
|
|
@ -110,7 +112,15 @@ 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();
|
if(cancelRequested.get() || isCancelled()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!taskSemaphore.tryAcquire()) {
|
||||||
|
log.warn("Skipped execution of webcam capture task, another task is running");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if(devices == null) {
|
if(devices == null) {
|
||||||
devices = capture.getDevices();
|
devices = capture.getDevices();
|
||||||
|
|
@ -222,11 +232,13 @@ public class WebcamService extends ScheduledService<Image> {
|
||||||
public void reset() {
|
public void reset() {
|
||||||
stream = null;
|
stream = null;
|
||||||
zoomLimits = null;
|
zoomLimits = null;
|
||||||
|
cancelRequested.set(false);
|
||||||
super.reset();
|
super.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean cancel() {
|
public boolean cancel() {
|
||||||
|
cancelRequested.set(true);
|
||||||
boolean cancelled = super.cancel();
|
boolean cancelled = super.cancel();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
@ -414,6 +426,10 @@ public class WebcamService extends ScheduledService<Image> {
|
||||||
return opened;
|
return opened;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getCancelRequested() {
|
||||||
|
return cancelRequested.get();
|
||||||
|
}
|
||||||
|
|
||||||
public static <T extends Enum<T>> T getNearestEnum(T target) {
|
public static <T extends Enum<T>> T getNearestEnum(T target) {
|
||||||
return getNearestEnum(target, target.getDeclaringClass().getEnumConstants());
|
return getNearestEnum(target, target.getDeclaringClass().getEnumConstants());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ public class WebcamView {
|
||||||
});
|
});
|
||||||
|
|
||||||
service.valueProperty().addListener((observable, oldValue, newValue) -> {
|
service.valueProperty().addListener((observable, oldValue, newValue) -> {
|
||||||
if(newValue != null) {
|
if(newValue != null && !service.getCancelRequested()) {
|
||||||
imageProperty.set(newValue);
|
imageProperty.set(newValue);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue