88import com .google .inject .Guice ;
99import com .google .inject .Injector ;
1010import edu .wpi .grip .core .events .UnexpectedThrowableEvent ;
11+ import edu .wpi .grip .core .util .ImageLoadingUtility ;
1112import edu .wpi .grip .core .util .MockExceptionWitness ;
13+ import edu .wpi .grip .util .Files ;
1214import edu .wpi .grip .util .GRIPCoreTestModule ;
1315import net .jodah .concurrentunit .Waiter ;
1416import org .bytedeco .javacpp .indexer .Indexer ;
17+ import org .bytedeco .javacpp .opencv_core .Mat ;
1518import org .bytedeco .javacv .Frame ;
1619import org .bytedeco .javacv .FrameGrabber ;
20+ import org .bytedeco .javacv .OpenCVFrameConverter ;
1721import org .junit .After ;
1822import org .junit .Before ;
1923import org .junit .Rule ;
@@ -246,4 +250,54 @@ public FrameGrabber create(String addressProperty) throws MalformedURLException
246250 }
247251 }
248252
253+ @ Test
254+ public void testFrameRateUpdatesWithGrabSpeed () throws IOException , InterruptedException , TimeoutException {
255+ Waiter waiter1 = new Waiter ();
256+ Waiter waiter2 = new Waiter ();
257+ Queue <Waiter > waiterQueue = new LinkedList <>(Arrays .asList (waiter1 , waiter2 ));
258+
259+ Mat image = new Mat ();
260+ ImageLoadingUtility .loadImage (Files .gompeiJpegFile .file .getPath (), image );
261+ CameraSource source = new CameraSource (new EventBus (), new CameraSource .FrameGrabberFactory () {
262+ @ Override
263+ public FrameGrabber create (int deviceNumber ) {
264+ return new SimpleMockFrameGrabber () {
265+ private OpenCVFrameConverter .ToMat converter = new OpenCVFrameConverter .ToMat ();
266+ @ Override
267+ public Frame grab () throws Exception {
268+ try {
269+ Thread .sleep (3 );
270+ } catch (InterruptedException e ) {
271+ Thread .currentThread ().interrupt ();
272+ throw new FrameGrabber .Exception ("Thread interrupted" , e );
273+ }
274+ if (!waiterQueue .isEmpty ()){
275+ waiterQueue .poll ().resume ();
276+ }
277+ return converter .convert (image );
278+ }
279+ };
280+ }
281+
282+ @ Override
283+ public FrameGrabber create (String addressProperty ) throws MalformedURLException {
284+ throw new AssertionError ("This should not be called" );
285+ }
286+ }, MockExceptionWitness .MOCK_FACTORY , 0 );
287+
288+ source .startAsync ().awaitRunning ();
289+
290+ waiter2 .await ();
291+ // Move the value over to the socket.
292+ source .updateOutputSockets ();
293+
294+ assertNotEquals ("The frame rate was not updated when the camera was running" ,
295+ Double .valueOf (0 ), source .createOutputSockets ()[1 ].getValue ().get ());
296+
297+ try {
298+ source .stopAndAwait ();
299+ } catch (IllegalStateException e ) {
300+ // This could happen if the thread is interrupted.
301+ }
302+ }
249303}
0 commit comments