-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
cscore/src/test/java/edu/wpi/first/cscore/LastFrameTimeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package edu.wpi.first.cscore; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import edu.wpi.first.util.PixelFormat; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import org.junit.jupiter.api.Test; | ||
import org.opencv.core.CvType; | ||
import org.opencv.core.Mat; | ||
|
||
class LastFrameTimeTest { | ||
@Test | ||
void testLastFrameTimeBehavior() throws InterruptedException { | ||
CvSource source = new CvSource("source1", new VideoMode(PixelFormat.kBGR, 320, 240, 30)); | ||
|
||
CvSink sink = new CvSink("sink1"); | ||
sink.setSource(source); | ||
|
||
Mat cameraFrame = Mat.zeros(320, 240, CvType.CV_8UC3); | ||
|
||
// Make sure no frame was grabbed yet (paranoia) | ||
Mat img = new Mat(); | ||
sink.grabFrame(img); | ||
assertTrue(img.empty()); | ||
|
||
// Add a single frame | ||
source.putFrame(cameraFrame); | ||
var lastTime = source.getLastFrameTime(); | ||
assertNotEquals(0, lastTime); | ||
|
||
// And if we set lastTime to something different from the frame we just added, it should be | ||
// grabbed right away successfully | ||
assertNotEquals(0, sink.grabFrame(img, lastTime - 1)); | ||
assertEquals(cameraFrame.size(), img.size()); | ||
|
||
// Wait for a "new" frame to show up - this should time out and return 0 | ||
assertEquals(0, sink.grabFrame(img, lastTime)); | ||
|
||
// And another new frame for fun. | ||
source.putFrame(cameraFrame); | ||
var newSourceTime = source.getLastFrameTime(); | ||
var newSinkTime = sink.grabFrameNoTimeout(img, lastTime); | ||
assertNotEquals(0, newSinkTime); | ||
assertEquals(newSourceTime, newSinkTime); | ||
|
||
{ | ||
// Given a thread waiting for a new frame from our sink via the current time | ||
final AtomicLong newNewSinkTime = new AtomicLong(0); | ||
var sinkThread = | ||
new Thread( | ||
() -> { | ||
var t = sink.grabFrameNoTimeout(img); | ||
newNewSinkTime.set(t); | ||
}); | ||
sinkThread.start(); | ||
|
||
// When we put a new frame with a timestamp != lastTime into the source | ||
Thread.sleep(10); | ||
source.putFrame(cameraFrame); | ||
var newNewSourceTime = source.getLastFrameTime(); | ||
sinkThread.join(); | ||
|
||
// Then we see the same image present at the source | ||
assertEquals(newNewSourceTime, newNewSinkTime.get()); | ||
} | ||
|
||
{ | ||
// Given a thread waiting for a new frame from our sink via the last frame time | ||
final long lastFrameTime = source.getLastFrameTime(); | ||
final AtomicLong newNewSinkTime = new AtomicLong(0); | ||
var sinkThread = | ||
new Thread( | ||
() -> { | ||
var t = sink.grabFrame(img, lastFrameTime); | ||
newNewSinkTime.set(t); | ||
}); | ||
sinkThread.start(); | ||
|
||
// When we put a new frame with a timestamp != lastTime into the source | ||
Thread.sleep(10); | ||
source.putFrame(cameraFrame); | ||
var newNewSourceTime = source.getLastFrameTime(); | ||
sinkThread.join(); | ||
|
||
// Then we see the same image present at the source | ||
assertEquals(newNewSourceTime, newNewSinkTime.get()); | ||
} | ||
|
||
{ | ||
// Given a thread waiting for a new frame from our sink via the last frame time | ||
final long lastFrameTime = source.getLastFrameTime(); | ||
final AtomicLong newNewSinkTime = new AtomicLong(0); | ||
var sinkThread = | ||
new Thread( | ||
() -> { | ||
var t = sink.grabFrame(img, lastFrameTime); | ||
newNewSinkTime.set(t); | ||
}); | ||
sinkThread.start(); | ||
|
||
// When we do not put new frames into the sink | ||
sinkThread.join(); | ||
|
||
// Then the source reports an error | ||
assertEquals(0, newNewSinkTime.get()); | ||
} | ||
|
||
sink.close(); | ||
source.close(); | ||
img.release(); | ||
cameraFrame.release(); | ||
} | ||
} |