|
2 | 2 |
|
3 | 3 |
|
4 | 4 | import com.google.common.base.StandardSystemProperty; |
5 | | -import com.google.common.base.Stopwatch; |
6 | 5 | import com.google.common.eventbus.EventBus; |
7 | 6 | import com.google.common.eventbus.Subscribe; |
8 | | -import com.google.common.math.IntMath; |
9 | | -import com.google.common.util.concurrent.AbstractExecutionThreadService; |
| 7 | +import com.google.common.util.concurrent.MoreExecutors; |
10 | 8 | import com.google.inject.assistedinject.Assisted; |
11 | 9 | import com.google.inject.assistedinject.AssistedInject; |
12 | 10 | import com.thoughtworks.xstream.annotations.XStreamAlias; |
|
18 | 16 | import edu.wpi.grip.core.events.SourceRemovedEvent; |
19 | 17 | import edu.wpi.grip.core.util.ExceptionWitness; |
20 | 18 | import edu.wpi.grip.core.util.service.AutoRestartingService; |
| 19 | +import edu.wpi.grip.core.util.service.LoggingListener; |
21 | 20 | import edu.wpi.grip.core.util.service.RestartableService; |
22 | 21 | import org.bytedeco.javacpp.opencv_core.Mat; |
23 | | -import org.bytedeco.javacv.*; |
| 22 | +import org.bytedeco.javacv.FrameGrabber; |
| 23 | +import org.bytedeco.javacv.OpenCVFrameGrabber; |
| 24 | +import org.bytedeco.javacv.VideoInputFrameGrabber; |
24 | 25 |
|
25 | 26 | import java.io.IOException; |
26 | | -import java.math.RoundingMode; |
27 | 27 | import java.net.MalformedURLException; |
28 | 28 | import java.net.URL; |
29 | 29 | import java.util.Optional; |
30 | 30 | import java.util.Properties; |
31 | 31 | import java.util.concurrent.Executor; |
32 | | - |
33 | 32 | import java.util.concurrent.TimeUnit; |
34 | 33 | import java.util.concurrent.TimeoutException; |
35 | 34 | import java.util.concurrent.atomic.AtomicBoolean; |
36 | 35 | import java.util.function.Supplier; |
37 | | -import java.util.logging.Level; |
38 | 36 | import java.util.logging.Logger; |
39 | 37 |
|
40 | 38 | /** |
@@ -94,6 +92,9 @@ public interface Factory { |
94 | 92 | CameraSource create(Properties properties) throws IOException; |
95 | 93 | } |
96 | 94 |
|
| 95 | + /** |
| 96 | + * Allows for the creation of a frame grabber using either a device number or URL string address. |
| 97 | + */ |
97 | 98 | public interface FrameGrabberFactory { |
98 | 99 | FrameGrabber create(int deviceNumber); |
99 | 100 |
|
@@ -194,90 +195,40 @@ public FrameGrabber create(String addressProperty) throws MalformedURLException |
194 | 195 | } |
195 | 196 |
|
196 | 197 | /* This must be initialized in the constructor otherwise the grabber supplier won't be present */ |
197 | | - this.cameraService = new AutoRestartingService<>(() -> new AbstractExecutionThreadService() { |
198 | | - private final FrameGrabber frameGrabber = grabberSupplier.get(); |
199 | | - private boolean failedStartup = false; |
200 | | - |
201 | | - /** |
202 | | - * Keep a reference to the thread around so that it can be interrupted when stop is called. |
203 | | - */ |
204 | | - private Optional<Thread> serviceThread = Optional.empty(); |
205 | | - |
| 198 | + this.cameraService = new AutoRestartingService<>(() -> new GrabberService(name, grabberSupplier, new CameraSourceUpdater() { |
206 | 199 | @Override |
207 | | - protected void startUp() { |
208 | | - serviceThread = Optional.of(Thread.currentThread()); |
209 | | - try { |
210 | | - frameGrabber.start(); |
211 | | - } catch (FrameGrabber.Exception e) { |
212 | | - failedStartup = true; |
213 | | - getExceptionWitness().flagException(e, "Failed to start"); |
214 | | - } |
| 200 | + public void setFrameRate(double value) { |
| 201 | + CameraSource.this.frameRate = frameRate; |
| 202 | + isNewFrame.set(true); |
215 | 203 | } |
216 | 204 |
|
217 | 205 | @Override |
218 | | - protected void run() throws InterruptedException { |
219 | | - // If we failed to startup don't even try to run. Just give up as soon as possible. |
220 | | - if (failedStartup) return; |
221 | | - |
222 | | - final OpenCVFrameConverter.ToMat convertToMat = new OpenCVFrameConverter.ToMat(); |
223 | | - final Stopwatch stopwatch = Stopwatch.createStarted(); |
224 | | - while (super.isRunning()) { |
225 | | - final Frame videoFrame; |
226 | | - try { |
227 | | - videoFrame = frameGrabber.grab(); |
228 | | - } catch (FrameGrabber.Exception e) { |
229 | | - getExceptionWitness().flagException(e, "Failed to grab image"); |
230 | | - logger.log(Level.WARNING, "Failed to grab image", e); |
231 | | - break; // We failed on a grab, something went wrong. Bail out and let the service restart. |
232 | | - } |
233 | | - |
234 | | - final Mat frameMat = convertToMat.convert(videoFrame); |
235 | | - |
236 | | - if (frameMat == null || frameMat.isNull()) { |
237 | | - final String errMsg = "The camera returned a null frame Mat"; |
238 | | - getExceptionWitness().flagWarning(errMsg); |
239 | | - logger.log(Level.WARNING, errMsg); |
240 | | - break; // We have a null frame, something external has gone wrong. Bail out and let the service restart. |
241 | | - } |
242 | | - |
243 | | - synchronized (currentFrameTransferMat) { |
244 | | - frameMat.copyTo(currentFrameTransferMat); |
245 | | - } |
246 | | - |
247 | | - stopwatch.stop(); |
248 | | - final long elapsedTime = stopwatch.elapsed(TimeUnit.MILLISECONDS); |
249 | | - stopwatch.reset(); |
250 | | - stopwatch.start(); |
251 | | - if (elapsedTime != 0) frameRate = IntMath.divide(1000, Math.toIntExact(elapsedTime), RoundingMode.DOWN); |
252 | | - getExceptionWitness().clearException(); |
253 | | - isNewFrame.set(true); |
254 | | - eventBus.post(new SourceHasPendingUpdateEvent(CameraSource.this)); |
| 206 | + public void copyNewMat(Mat matToCopy) { |
| 207 | + synchronized (CameraSource.this.currentFrameTransferMat) { |
| 208 | + matToCopy.copyTo(CameraSource.this.currentFrameTransferMat); |
255 | 209 | } |
| 210 | + isNewFrame.set(true); |
256 | 211 | } |
257 | 212 |
|
258 | 213 | @Override |
259 | | - protected void shutDown() throws FrameGrabber.Exception { |
260 | | - if (failedStartup) return; |
261 | | - frameRate = 0; |
262 | | - isNewFrame.set(true); |
| 214 | + public void updatesComplete() { |
263 | 215 | eventBus.post(new SourceHasPendingUpdateEvent(CameraSource.this)); |
264 | | - frameGrabber.stop(); |
265 | 216 | } |
266 | | - |
| 217 | + }, getExceptionWitness()::clearException)); |
| 218 | + this.cameraService.addListener(new Listener() { |
267 | 219 | @Override |
268 | | - protected void triggerShutdown() { |
269 | | - serviceThread.ifPresent(Thread::interrupt); |
270 | | - } |
271 | | - |
272 | | - /* |
273 | | - * Allows us to set our own service name |
274 | | - */ |
275 | | - @Override |
276 | | - protected String serviceName() { |
277 | | - return name + " Service"; |
| 220 | + public void failed(State from, Throwable failure) { |
| 221 | + if (failure instanceof GrabberService.GrabberServiceException) { |
| 222 | + // These are expected exceptions. Handle them by flagging an exception |
| 223 | + getExceptionWitness().flagException((GrabberService.GrabberServiceException) failure, "Camera service crashed"); |
| 224 | + } else { |
| 225 | + // Rethrow as an uncaught exception if this is not an exception we expected. |
| 226 | + Optional.ofNullable(Thread.getDefaultUncaughtExceptionHandler()) |
| 227 | + .ifPresent(handler -> handler.uncaughtException(Thread.currentThread(), failure)); |
| 228 | + } |
278 | 229 | } |
279 | | - |
280 | | - }); |
| 230 | + }, MoreExecutors.directExecutor()); |
| 231 | + this.cameraService.addListener(new LoggingListener(logger, CameraSource.class), MoreExecutors.directExecutor()); |
281 | 232 | } |
282 | 233 |
|
283 | 234 | @Override |
|
0 commit comments