Skip to content

Commit

Permalink
gl buffer copy working again
Browse files Browse the repository at this point in the history
  • Loading branch information
codeanticode committed Jun 10, 2019
1 parent 6c7c716 commit a7f9549
Showing 1 changed file with 127 additions and 66 deletions.
193 changes: 127 additions & 66 deletions src/processing/video/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public class Movie extends PImage implements PConstants {
protected Method sinkDisposeMethod;
protected Method sinkGetMethod;
protected String copyMask;
protected Buffer natBuffer = null;
// protected Buffer natBuffer = null;
// protected BufferDataAppSink natSink = null;

NewSampleListener newSampleListener;
// NewPrerollListener newPrerollListener;
NewPrerollListener newPrerollListener;
private final Lock bufferLock = new ReentrantLock();


Expand Down Expand Up @@ -135,7 +135,7 @@ public void dispose() {
pixels = null;

rgbSink.disconnect(newSampleListener);
// sink.disconnect(newPrerollListener);
rgbSink.disconnect(newPrerollListener);
rgbSink.dispose();
playbin.setState(org.freedesktop.gstreamer.State.NULL);
playbin.getState();
Expand Down Expand Up @@ -547,10 +547,26 @@ public synchronized void read() {
firstFrame = false;
}

int[] temp = pixels;
pixels = copyPixels;
updatePixels();
copyPixels = temp;
if (useBufferSink) {

if (bufferSink == null) {
Object cache = parent.g.getCache(Movie.this);
if (cache != null) {
setBufferSink(cache);
getSinkMethods();
}
}

} else {
int[] temp = pixels;
pixels = copyPixels;
updatePixels();
copyPixels = temp;
}





/*
if (useBufferSink) { // The native buffer from gstreamer is copied to the buffer sink.
Expand Down Expand Up @@ -620,7 +636,7 @@ public void volume(float v) {
public synchronized void loadPixels() {
super.loadPixels();

if (useBufferSink) {
if (useBufferSink && bufferSink != null) {
/*
if (natBuffer != null) {
// This means that the OpenGL texture hasn't been created so far (the
Expand Down Expand Up @@ -792,9 +808,9 @@ protected void initSink() {
rgbSink = new AppSink("sink");
rgbSink.set("emit-signals", true);
newSampleListener = new NewSampleListener();
// newPrerollListener = new NewPrerollListener();
newPrerollListener = new NewPrerollListener();
rgbSink.connect(newSampleListener);
// rgbSink.connect(newPrerollListener);
rgbSink.connect(newPrerollListener);
if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
rgbSink.setCaps(Caps.fromString("video/x-raw, format=BGRx"));
} else {
Expand All @@ -805,52 +821,47 @@ protected void initSink() {
makeBusConnections(playbin.getBus());
playbin.setState(org.freedesktop.gstreamer.State.READY);

useBufferSink = Video.useGLBufferSink && parent.g.isGL();




sinkReady = true;
newFrame = false;
}


private void makeBusConnections(Bus bus) {
bus.connect(new Bus.ERROR() {

public void errorMessage(GstObject arg0, int arg1, String arg2) {
System.err.println(arg0 + " : " + arg2);
}
public void errorMessage(GstObject arg0, int arg1, String arg2) {
System.err.println(arg0 + " : " + arg2);
}
});
bus.connect(new Bus.EOS() {

public void endOfStream(GstObject arg0) {
if (repeat) {
if (0 < rate) {
// Playing forward, so we return to the beginning
jump(0);
} else {
// Playing backwards, so we go to the end.
jump(duration());
}

// The rate is set automatically to 1 when restarting the
// stream, so we need to call frameRate in order to reset
// to the latest fps rate.
frameRate(frameRate);
public void endOfStream(GstObject arg0) {
if (repeat) {
if (0 < rate) {
// Playing forward, so we return to the beginning
jump(0);
} else {
playing = false;
// Playing backwards, so we go to the end.
jump(duration());
}

// The rate is set automatically to 1 when restarting the
// stream, so we need to call frameRate in order to reset
// to the latest fps rate.
frameRate(frameRate);
} else {
playing = false;
}
}
});
}



////////////////////////////////////////////////////////////

// Stream event handling.


/*
protected synchronized void invokeEvent(int w, int h, IntBuffer buffer) {
available = true;
bufWidth = w;
Expand Down Expand Up @@ -879,7 +890,7 @@ protected synchronized void invokeEvent(int w, int h, Buffer buffer) {
bufHeight = h;
if (natBuffer != null) {
// To handle the situation where read() is not called in the sketch, so
// that the native buffers are not being sent to the sinke, and therefore, not disposed
// that the native buffers are not being sent to the sink, and therefore, not disposed
// by it.
natBuffer.dispose();
}
Expand All @@ -889,7 +900,8 @@ protected synchronized void invokeEvent(int w, int h, Buffer buffer) {
fireMovieEvent();
}
}

*/

private void fireMovieEvent() {
// Creates a movieEvent.
if (movieEventMethod != null) {
Expand Down Expand Up @@ -1080,6 +1092,79 @@ public FlowReturn newSample(AppSink sink) {
frameRate = nativeFrameRate;
}

Buffer buffer = sample.getBuffer();
ByteBuffer bb = buffer.map(false);
if (bb != null) {

// If the EDT is still copying data from the buffer, just drop this frame
if (!bufferLock.tryLock()) {
return FlowReturn.OK;
}

available = true;
bufWidth = nativeWidth;
bufHeight = nativeHeight;

if (useBufferSink && bufferSink != null) { // The native buffer from gstreamer is copied to the buffer sink.

try {
sinkCopyMethod.invoke(bufferSink, new Object[] { buffer, bb, bufWidth, bufHeight });
if (playing) {
fireMovieEvent();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
bufferLock.unlock();
}

} else {

IntBuffer rgb = bb.asIntBuffer();

if (copyPixels == null) {
copyPixels = new int[nativeWidth * nativeHeight];
}

try {
rgb.get(copyPixels, 0, width * height);
if (playing) {
fireMovieEvent();
}
} finally {
bufferLock.unlock();
}

}

buffer.unmap();
}
sample.dispose();
return FlowReturn.OK;
}
}


private class NewPrerollListener implements AppSink.NEW_PREROLL {
@Override
public FlowReturn newPreroll(AppSink sink) {
// Sample sample = sink.pullSample();
Sample sample = sink.pullPreroll();

// pull out metadata from caps
Structure capsStruct = sample.getCaps().getStructure(0);
nativeWidth = capsStruct.getInteger("width");
nativeHeight = capsStruct.getInteger("height");
Fraction fps = capsStruct.getFraction("framerate");
nativeFrameRate = (float)fps.numerator / fps.denominator;

// set the playback rate to the file's native framerate
// unless the user has already set a custom one
if (frameRate == -1.0) {
frameRate = nativeFrameRate;
}

/*
Buffer buffer = sample.getBuffer();
ByteBuffer bb = buffer.map(false);
if (bb != null) {
Expand All @@ -1105,40 +1190,16 @@ public FlowReturn newSample(AppSink sink) {
} finally {
bufferLock.unlock();
}
buffer.unmap();
}
*/

sample.dispose();
return FlowReturn.OK;
}
}

/*
private class NewPrerollListener implements AppSink.NEW_PREROLL {
@Override
public void newPreroll(AppSink sink) {
surfaceLock.lock();
Sample sample = sink.pullPreroll();
Structure capsStruct = sample.getCaps().getStructure(0);
int width = capsStruct.getInteger("width");
int height = capsStruct.getInteger("height");
try {
if (surface == null || surface.getWidth() != width || surface.getHeight() != height) {
if (surface != null && surface.sample != null) {
surface.sample.dispose();
}
surface = new GStreamerSurface(width, height);
} else {
if (surface.sample != null) {
surface.sample.dispose();
}
}
surface.sample = sample;
surface.modCount++;
} finally {
surfaceLock.unlock();
}
}
}
*/

}

0 comments on commit a7f9549

Please sign in to comment.