Skip to content

Commit 41cc7af

Browse files
Merge pull request #227 from codelerity/query-probe-test
Fix up ProbeTester and add probe test for queries.
2 parents 4f5c9e7 + 9afcabc commit 41cc7af

File tree

2 files changed

+60
-36
lines changed

2 files changed

+60
-36
lines changed

test/org/freedesktop/gstreamer/PadTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.freedesktop.gstreamer.event.FlushStopEvent;
3333

3434
import org.freedesktop.gstreamer.event.TagEvent;
35+
import org.freedesktop.gstreamer.query.AllocationQuery;
36+
import org.freedesktop.gstreamer.query.Query;
3537
import org.junit.After;
3638
import org.junit.AfterClass;
3739
import org.junit.Before;
@@ -324,4 +326,14 @@ public void addProbe_Idle() {
324326

325327
}
326328

329+
@Test
330+
public void addProbe_Query() {
331+
ProbeTester.test(PadProbeType.QUERY_BOTH, info -> {
332+
Query q = info.getQuery();
333+
// System.out.println(q.getStructure());
334+
return q instanceof AllocationQuery;
335+
});
336+
337+
}
338+
327339
}

test/org/freedesktop/gstreamer/ProbeTester.java

+48-36
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,73 @@
2828
import java.util.concurrent.CountDownLatch;
2929
import java.util.concurrent.TimeUnit;
3030
import java.util.concurrent.TimeoutException;
31-
import java.util.function.Consumer;
32-
import java.util.function.Function;
31+
import java.util.function.Predicate;
3332

3433
/**
3534
* Utility class for unit testing API that operates on a Probe.
3635
* <p>
3736
* Call {@link ProbeTester#test(Consumer)} and pass a callback which will
3837
* perform the test on a PadProbeInfo it is supplied. The callback runs in a Pad
39-
* probe. The buffer is produced by a simple, ephemeral pipeline that is fed by
40-
* a video test source.
38+
* probe. The test uses a simple, ephemeral pipeline that is fed by a video test
39+
* source (or custom pipeline).
40+
* <p>
41+
* The callback is a {@link Predicate} and should return false if the input info
42+
* doesn't match that required by the test. Test exceptions should be thrown as
43+
* normal. This is to allow the probe to be called repeatedly until the input
44+
* info matches that expected. If the probe never matches the test will time
45+
* out.
4146
*/
4247
public class ProbeTester {
4348

44-
public static void test(Set<PadProbeType> mask, Function<PadProbeInfo, Boolean> callback) {
45-
test("videotestsrc ! videoconvert ! fakesink name=sink", mask, callback);
49+
/**
50+
* Run a probe test on a simple test pipeline. The callback will be called
51+
* by the probe until it returns true, allowing for probe callbacks to be
52+
* ignored. If the callback never returns true the test will timeout and
53+
* fail.
54+
* <p>
55+
* The pipeline is <code>videotestsrc ! fakesink name=sink</code>. The probe
56+
* will be attached to the sink pad of the sink element.
57+
*
58+
* @param mask PadProbeType mask to use when attaching probe to sink pad
59+
* @param callback probe callback
60+
*/
61+
public static void test(Set<PadProbeType> mask, Predicate<PadProbeInfo> callback) {
62+
test("videotestsrc ! fakesink name=sink", mask, callback);
4663
}
4764

48-
public static void test(String pipeline, Set<PadProbeType> mask, Function<PadProbeInfo, Boolean> callback) {
65+
/**
66+
* Run a probe test on a simple test pipeline. The callback will be called
67+
* by the probe until it returns true, allowing for probe callbacks to be
68+
* ignored. If the callback never returns true the test will timeout and
69+
* fail.
70+
* <p>
71+
* The pipeline must have a sink element named sink. The probe will be
72+
* attached to the sink pad of the sink element.
73+
*
74+
* @param pipeline custom pipeline with named sink element
75+
* @param mask PadProbeType mask to use when attaching probe to sink pad
76+
* @param callback probe callback
77+
*/
78+
public static void test(String pipeline, Set<PadProbeType> mask, Predicate<PadProbeInfo> callback) {
4979
assertNotNull("Pipeline description can not be null", pipeline);
5080
assertFalse("Pipeline description can not be empty", pipeline.isEmpty());
5181
Pipeline pipe = (Pipeline) Gst.parseLaunch(pipeline);
5282
assertNotNull("Unable to create Pipeline from pipeline description: ", pipe);
5383

5484
Element sink = pipe.getElementByName("sink");
5585
Pad pad = sink.getStaticPad("sink");
56-
PadProbe probe = new PadProbe(callback, 0);
86+
PadProbe probe = new PadProbe(callback);
5787
pad.addProbe(mask, probe);
5888

5989
pipe.play();
6090

61-
// Wait for the sample to arrive and for the client supplied test function to
62-
// complete
91+
// Wait for the probe to complete
6392
try {
6493
probe.await(5000);
94+
} catch (TimeoutException ex) {
95+
fail("Timed out waiting for probe condition\n" + ex);
6596
} catch (Exception ex) {
66-
fail("Unexpected exception waiting for buffer\n" + ex);
97+
fail("Unexpected exception waiting for probe\n" + ex);
6798
} finally {
6899
pipe.stop();
69100
}
@@ -75,46 +106,27 @@ public static void test(String pipeline, Set<PadProbeType> mask, Function<PadPro
75106
}
76107
}
77108

78-
public static void test(Consumer<Buffer> callback, String pipelineDescription, int skipFrames) {
79-
80-
}
81-
82109
private static class PadProbe implements Pad.PROBE {
83110

84-
private final int skipFrames;
85111
private final CountDownLatch latch;
86-
private final Function<PadProbeInfo, Boolean> callback;
112+
private final Predicate<PadProbeInfo> callback;
87113

88114
private Throwable exception;
89-
private int counter = 0;
90115

91-
PadProbe(Function<PadProbeInfo, Boolean> callback) {
92-
this(callback, 0);
93-
}
94-
95-
PadProbe(Function<PadProbeInfo, Boolean> callback, int skip) {
116+
PadProbe(Predicate<PadProbeInfo> callback) {
96117
this.callback = callback;
97-
skipFrames = skip;
98118
latch = new CountDownLatch(1);
99119
}
100120

101121
@Override
102122
public PadProbeReturn probeCallback(Pad pad, PadProbeInfo info) {
103123
if (latch.getCount() > 0) {
104-
if (counter < skipFrames) {
105-
counter++;
106-
return PadProbeReturn.OK;
107-
}
108124
try {
109-
// Run the client's test logic on the buffer (only once)
110-
try {
111-
if (!callback.apply(info)) {
112-
return PadProbeReturn.OK;
113-
}
114-
} catch (Throwable exc) {
115-
exception = exc;
125+
if (callback.test(info)) {
126+
latch.countDown();
116127
}
117-
} finally {
128+
} catch (Throwable exc) {
129+
exception = exc;
118130
latch.countDown();
119131
}
120132
}

0 commit comments

Comments
 (0)