Skip to content

Commit 37267dd

Browse files
committed
Merge pull request #512 from JLLeitschuh/fix/cameraSourceNotReportingFrameRate
Fixes Camera Sources Not Setting Frame Rate
2 parents 119c866 + bbb6072 commit 37267dd

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

core/src/main/java/edu/wpi/grip/core/sources/CameraSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public FrameGrabber create(String addressProperty) throws MalformedURLException
198198
this.cameraService = new AutoRestartingService<>(() -> new GrabberService(name, grabberSupplier, new CameraSourceUpdater() {
199199
@Override
200200
public void setFrameRate(double value) {
201-
CameraSource.this.frameRate = frameRate;
201+
CameraSource.this.frameRate = value;
202202
isNewFrame.set(true);
203203
}
204204

core/src/test/java/edu/wpi/grip/core/sources/CameraSourceTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
import com.google.inject.Guice;
99
import com.google.inject.Injector;
1010
import edu.wpi.grip.core.events.UnexpectedThrowableEvent;
11+
import edu.wpi.grip.core.util.ImageLoadingUtility;
1112
import edu.wpi.grip.core.util.MockExceptionWitness;
13+
import edu.wpi.grip.util.Files;
1214
import edu.wpi.grip.util.GRIPCoreTestModule;
1315
import net.jodah.concurrentunit.Waiter;
1416
import org.bytedeco.javacpp.indexer.Indexer;
17+
import org.bytedeco.javacpp.opencv_core.Mat;
1518
import org.bytedeco.javacv.Frame;
1619
import org.bytedeco.javacv.FrameGrabber;
20+
import org.bytedeco.javacv.OpenCVFrameConverter;
1721
import org.junit.After;
1822
import org.junit.Before;
1923
import 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

Comments
 (0)