Skip to content

Commit 8bef024

Browse files
i-n-g-oneilcsmith-net
authored andcommitted
add data probe (#103)
1 parent d7ab38f commit 8bef024

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

src/org/freedesktop/gstreamer/Pad.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,17 @@ public static interface EVENT_PROBE {
391391
public PadProbeReturn eventReceived(Pad pad, Event event);
392392
}
393393

394+
/**
395+
* Signal emitted when new data is available on the {@link Pad}
396+
*
397+
* @see #addDataProbe(DATA_PROBE)
398+
* @see #removeDataProbe(DATA_PROBE)
399+
*/
400+
public static interface DATA_PROBE {
401+
public PadProbeReturn dataReceived(Pad pad, Buffer buffer);
402+
}
403+
404+
394405
/**
395406
* Add a listener for the <code>have-data</code> signal on this {@link Pad}
396407
*
@@ -518,6 +529,36 @@ protected void disconnect() {
518529
public void removeEventProbe(EVENT_PROBE listener) {
519530
removeCallback(EVENT_PROBE.class, listener);
520531
}
532+
533+
534+
public synchronized void addDataProbe(final DATA_PROBE listener) {
535+
536+
final GstPadAPI.PadProbeCallback probe = new GstPadAPI.PadProbeCallback() {
537+
public PadProbeReturn callback(Pad pad, GstPadProbeInfo probeInfo, Pointer user_data) {
538+
if ((probeInfo.padProbeType & GstPadAPI.GST_PAD_PROBE_TYPE_BUFFER) != 0) {
539+
Buffer buffer = GSTPAD_API.gst_pad_probe_info_get_buffer(probeInfo);
540+
return listener.dataReceived(pad, buffer);
541+
}
542+
543+
//We have to negate the return value to keep consistency with gstreamer's API
544+
return PadProbeReturn.OK;
545+
}
546+
};
547+
548+
GCallback cb = new GCallback(GSTPAD_API.gst_pad_add_probe(this, GstPadAPI.GST_PAD_PROBE_TYPE_BUFFER, probe, null, null), probe) {
549+
@Override
550+
protected void disconnect() {
551+
GSTPAD_API.gst_pad_remove_probe(Pad.this, id);
552+
}
553+
};
554+
555+
addCallback(DATA_PROBE.class, listener, cb);
556+
}
557+
558+
public void removeDataProbe(DATA_PROBE listener) {
559+
removeCallback(DATA_PROBE.class, listener);
560+
}
561+
521562

522563
/**
523564
* Sends the event to this pad.
@@ -612,4 +653,29 @@ public FlowReturn getRange(long offset, int size, Buffer[] buffer) {
612653
public FlowReturn pullRange(long offset, int size, Buffer[] buffer) {
613654
return GSTPAD_API.gst_pad_pull_range(this, offset, size, buffer);
614655
}
656+
657+
658+
659+
/**
660+
* Pushes a buffer to the peer of pad .
661+
* This function will call installed block probes before triggering any
662+
* installed data probes.
663+
*
664+
* The function proceeds calling gst_pad_chain() on the peer pad and returns
665+
* the value from that function. If pad has no peer, GST_FLOW_NOT_LINKED
666+
* will be returned.
667+
*
668+
* In all cases, success or failure, the caller loses its reference to
669+
* buffer after calling this function.
670+
*
671+
* @param buffer
672+
* the GstBuffer to push returns GST_FLOW_ERROR if not. [transfer full]
673+
* @return
674+
* a GstFlowReturn from the peer pad.
675+
*
676+
* MT safe.
677+
*/
678+
public FlowReturn push(final Buffer buffer) {
679+
return GSTPAD_API.gst_pad_push(this, buffer);
680+
}
615681
}

src/org/freedesktop/gstreamer/lowlevel/GstPadAPI.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ NativeLong gst_pad_add_probe(Pad pad, int mask, PadProbeCallback callback,
158158

159159
Event gst_pad_probe_info_get_event(GstPadProbeInfo probeInfo);
160160

161+
Buffer gst_pad_probe_info_get_buffer(GstPadProbeInfo probeInfo);
162+
161163
// NativeLong /* gulong */ gst_pad_add_data_probe(Pad pad, PadDataProbe handler, Pointer data);
162164
//
163165
// void gst_pad_remove_data_probe(Pad pad, NativeLong handler_id);

test/org/freedesktop/gstreamer/PadTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,42 @@ public PadProbeReturn eventReceived(Pad pad, Event event) {
121121
sink.sendEvent(ev2);
122122
assertNotSame("event_prober.probeEvent() should not have been called", ev2, e.get());
123123
}
124+
125+
126+
@Test
127+
public void addDataProbe() {
128+
129+
Element elem = ElementFactory.make("identity", "src");
130+
TagList taglist = new TagList();
131+
Buffer buf = new Buffer(3);
132+
Buffer buf2 = new Buffer(2);
133+
final AtomicReference<Buffer> b = new AtomicReference<Buffer>();
134+
135+
Pad src = elem.getStaticPad("src");
136+
137+
Pad.DATA_PROBE data_probe = new Pad.DATA_PROBE() {
138+
139+
@Override
140+
public PadProbeReturn dataReceived(Pad pad, Buffer buffer) {
141+
b.set(buffer);
142+
return PadProbeReturn.OK;
143+
}
144+
};
145+
146+
elem.play();
147+
148+
// add a dataprobe
149+
src.addDataProbe(data_probe);
150+
151+
// push data
152+
FlowReturn res = src.push(buf);
153+
assertEquals("event_prober.probeEvent() was not called", buf, b.get());
154+
155+
// remove the dataprobe
156+
src.removeDataProbe(data_probe);
157+
158+
// push data
159+
res = src.push(buf2);
160+
assertNotSame("event_prober.probeEvent() should not have been called", buf2, b.get());
161+
}
124162
}

0 commit comments

Comments
 (0)