Created
May 24, 2014 20:43
-
-
Save macnibblet/5b12994fd8d81966ece3 to your computer and use it in GitHub Desktop.
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
package trickpit.model; | |
import com.sun.jna.Memory; | |
import javafx.application.Platform; | |
import uk.co.caprica.vlcj.component.DirectMediaPlayerComponent; | |
import uk.co.caprica.vlcj.player.direct.BufferFormat; | |
import uk.co.caprica.vlcj.player.direct.BufferFormatCallback; | |
import uk.co.caprica.vlcj.player.direct.DirectMediaPlayer; | |
import uk.co.caprica.vlcj.player.direct.format.RV32BufferFormat; | |
import java.nio.ByteBuffer; | |
import java.util.ArrayDeque; | |
/** | |
* Created by antoine on 24/05/14. | |
*/ | |
public class Camera { | |
private String name; | |
private String source; | |
private int width; | |
private int height; | |
private BufferFormat bufferFormat; | |
private BufferListener listener = new BufferListener(); | |
private ArrayDeque<ByteBuffer> frames = new ArrayDeque<ByteBuffer>(); | |
public Camera(String name, String source) { | |
this.name = name; | |
this.source = source; | |
} | |
public int getHeight() { | |
return height; | |
} | |
public void setHeight(int height) { | |
this.height = height; | |
} | |
public int getWidth() { | |
return width; | |
} | |
public void setWidth(int width) { | |
this.width = width; | |
} | |
public void initiateBuffer() { | |
listener.getMediaPlayer().playMedia(source); | |
} | |
public void terminateBuffer() | |
{ | |
// DO NOT RELEASE THE MEDIA PLAYER | |
// Doing so will cause the media play to generate a fatal error when attempting to restart the buffer | |
listener.getMediaPlayer().stop(); | |
// remove any remaining frames | |
frames.clear(); | |
} | |
public boolean isBufferEmpty() { | |
return frames.size() == 0; | |
} | |
public ByteBuffer getNextFrame() | |
{ | |
System.out.println("Buffer size: " + frames.size()); | |
return frames.pollFirst(); | |
} | |
public String getName() { | |
return name; | |
} | |
private class BufferListener extends DirectMediaPlayerComponent { | |
/** | |
* Construct a media player component. | |
*/ | |
public BufferListener() { | |
super(new SourceBufferFormat()); | |
} | |
@Override | |
public void display(DirectMediaPlayer mediaPlayer, final Memory[] nativeBuffers, final BufferFormat bufferFormat) { | |
Memory nativeBuffer = nativeBuffers[0]; | |
ByteBuffer original = nativeBuffer.getByteBuffer(0, nativeBuffer.size()); | |
// Create clone with same capacity as original. | |
final ByteBuffer clone = (original.isDirect()) ? | |
ByteBuffer.allocateDirect(original.capacity()) : | |
ByteBuffer.allocate(original.capacity()); | |
// Create a read-only copy of the original. | |
// This allows reading from the original without modifying it. | |
final ByteBuffer readOnlyCopy = original.asReadOnlyBuffer(); | |
// Flip and read from the original. | |
readOnlyCopy.flip(); | |
clone.put(readOnlyCopy); | |
frames.addLast(clone); | |
} | |
} | |
/** | |
* Callback to get the buffer format to use for video playback. | |
*/ | |
private class SourceBufferFormat implements BufferFormatCallback { | |
@Override | |
public BufferFormat getBufferFormat(final int width, final int height) { | |
setWidth(width); | |
setHeight(height); | |
return new RV32BufferFormat(width, height); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment