Last active
August 29, 2015 14:18
-
-
Save fjenett/a8f9f85f88fd1f4f9d6b to your computer and use it in GitHub Desktop.
Example showing how to load a video from Piecemaker2 API
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
/** | |
* Motion Bank research, http://motionbank.org/ | |
* | |
* Example of how to load a video from a group and play it with VLCj | |
*/ | |
import org.piecemaker2.api.*; | |
import org.piecemaker2.models.*; | |
import processing.video.*; | |
PieceMakerApi api; | |
VLCPlayer player; | |
String s3_base_url = "http://d35vpnmjdsiejq.cloudfront.net/dh/piecemaker/"; | |
void setup () | |
{ | |
size( 640, 360 ); | |
// player needs to know where VLC is installed | |
File vlcLib = new File( "/Applications/VLC.app/Contents/MacOS/lib" ); | |
if ( !vlcLib.exists() ) | |
{ | |
exit(); return; | |
} | |
else | |
{ | |
VLCPlayer.initialize( vlcLib.getPath() ); | |
} | |
api = new PieceMakerApi( this, "https://piecemaker2-api-public.herokuapp.com", "0310XPC6JEeF0oCy" ); | |
api.listAllGroups( api.createCallback( "groupsLoaded" ) ); | |
} | |
void draw () | |
{ | |
if ( player != null && player.available() ) | |
{ | |
image( player.frame(), 0, 0 ); | |
} | |
} | |
void groupsLoaded ( Group[] groups ) | |
{ | |
for ( Group g : groups ) | |
{ | |
// println( g.title ); | |
if ( g.title.indexOf("fh-hof-2014-04") != -1 ) { | |
api.getGroup( g.id, api.createCallback("groupLoaded") ); | |
break; | |
} | |
} | |
} | |
void groupLoaded ( Group group ) | |
{ | |
// println( "Group '" + group.title + "' (#" + group.id + ") loaded" ); | |
api.listEventsOfType( group.id, "video", api.createCallback( "groupEventsLoaded", group ) ); | |
} | |
void groupEventsLoaded ( org.piecemaker2.models.Event[] groupEvents, Group group ) | |
{ | |
for ( org.piecemaker2.models.Event e : groupEvents ) | |
{ | |
println( e.utc_timestamp + " " + e.type + " " + e.fields.get("type") + " " + e.fields.get("title") ); | |
if ( e.fields.get("title").equals("D06T02_Janine_sync_AJA_1") ) | |
{ | |
println( s3_base_url + e.fields.get("title") + ".mp4" ); | |
player = new VLCPlayer( this ); | |
player.startMedia( s3_base_url + e.fields.get("title") + ".mp4" ); | |
} | |
} | |
} | |
void piecemakerError ( int status, String err ) | |
{ | |
println( err ); | |
} |
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
/** | |
* VLC player bindings | |
* | |
* https://github.com/caprica/vlcj | |
* http://capricasoftware.co.uk/#/projects/vlcj/tutorial/direct-rendering | |
* | |
* 2.2.0 did not work, using 2.1.5 on OS-X: | |
* http://download.videolan.org/pub/videolan/vlc/2.1.5/macosx/ | |
*/ | |
import uk.co.caprica.vlcj.runtime.x.LibXUtil; | |
import com.sun.jna.NativeLibrary; | |
import com.sun.jna.Memory; | |
import java.util.*; | |
import java.lang.reflect.*; | |
import uk.co.caprica.vlcj.player.MediaPlayerFactory; | |
import uk.co.caprica.vlcj.player.MediaPlayer; | |
import uk.co.caprica.vlcj.player.direct.DirectMediaPlayer; | |
import uk.co.caprica.vlcj.player.direct.RenderCallbackAdapter; | |
import uk.co.caprica.vlcj.player.direct.BufferFormatCallback; | |
import uk.co.caprica.vlcj.player.direct.BufferFormat; | |
import uk.co.caprica.vlcj.player.direct.format.RV32BufferFormat; | |
import uk.co.caprica.vlcj.player.direct.RenderCallback; | |
import uk.co.caprica.vlcj.component.DirectMediaPlayerComponent; | |
import uk.co.caprica.vlcj.binding.LibVlc; | |
import uk.co.caprica.vlcj.discovery.NativeDiscovery; | |
import uk.co.caprica.vlcj.runtime.RuntimeUtil; | |
import java.io.File; | |
import processing.core.*; | |
public class VLCPlayer | |
{ | |
static String vlcPathLocal = null; | |
static void initialize ( String vlcPath ) | |
{ | |
boolean found = new NativeDiscovery().discover(); | |
if ( !found ) | |
{ | |
System.out.println( "VLC was not autodetected. Please provide a path to your local installation." ); | |
System.out.println( "For example on Mac this is typically: /Applications/VLC.app/Contents/MacOS/lib" ); | |
} | |
if ( vlcPathLocal == null && vlcPath != null ) | |
{ | |
LibXUtil.initialise(); | |
NativeLibrary.addSearchPath( RuntimeUtil.getLibVlcLibraryName(), vlcPath ); | |
File plugins = new File( vlcPath + "/../plugins" ); | |
String pluginsPath = ""; | |
try { pluginsPath = plugins.getCanonicalPath(); } catch ( Exception e ) { e.printStackTrace(); }; | |
System.setProperty( "VLC_PLUGIN_PATH", pluginsPath ); | |
vlcPathLocal = vlcPath; | |
} | |
System.out.println( LibVlc.INSTANCE.libvlc_get_version() ); | |
} | |
private PApplet papplet; | |
private PImage frame; | |
private int width, height; | |
private boolean newFrameAvailable; | |
private MediaPlayerFactory factory; | |
private DirectMediaPlayer mediaPlayer; | |
private String currentMedia; | |
VLCPlayer ( PApplet p ) | |
{ | |
papplet = p; | |
papplet.registerDispose( this ); | |
initMediaPlayer(); | |
} | |
VLCPlayer ( PApplet p, int w, int h ) | |
{ | |
papplet = p; | |
papplet.registerDispose( this ); | |
initFrame(w,h); | |
initMediaPlayer(); | |
} | |
private void initFrame ( int w, int h ) | |
{ | |
width = w; | |
height = h; | |
frame = papplet.createImage( w, h, PConstants.RGB ); | |
} | |
private void initMediaPlayer () | |
{ | |
java.util.List<String> vlcArgs = new java.util.ArrayList<String>(); | |
//vlcArgs.add("--ffmpeg-hw"); | |
// vlcArgs.add("--no-plugins-cache"); | |
// vlcArgs.add("--no-video-title-show"); | |
// vlcArgs.add("--no-snapshot-preview"); | |
//vlcArgs.add("--quiet"); | |
//vlcArgs.add("--quiet-synchro"); | |
//vlcArgs.add("--intf"); | |
//vlcArgs.add("dummy"); | |
//vlcArgs.add("-Dvlcj.log=DEBUG"); | |
//factory = new MediaPlayerFactory(vlcArgs.toArray(new String[vlcArgs.size()])); | |
DirectMediaPlayerComponent mediaPlayerComponent = new DirectMediaPlayerComponent( new VLCPlayerBufferFormatCallback() ) { | |
protected RenderCallback onGetRenderCallback() { | |
return new VLCPlayerRenderCallback(); | |
} | |
}; | |
mediaPlayer = mediaPlayerComponent.getMediaPlayer(); | |
String[] standardMediaOptions = {"video-filter=logo", "logo-file=vlcj-logo.png", "logo-opacity=25"}; | |
mediaPlayer.setStandardMediaOptions(standardMediaOptions); | |
mediaPlayer.setPlaySubItems(true); | |
// mediaPlayer.addMediaPlayerEventListener( this ); | |
} | |
public void play () | |
{ | |
mediaPlayer.play(); | |
} | |
public void playMedia ( String mediaPath ) | |
{ | |
if ( currentMedia == null || !currentMedia.equals(mediaPath) ) | |
{ | |
mediaPlayer.playMedia( mediaPath ); | |
currentMedia = mediaPath; | |
} | |
} | |
// blocks | |
public void startMedia ( String mediaPath ) | |
{ | |
if ( currentMedia == null || !currentMedia.equals(mediaPath) ) | |
{ | |
mediaPlayer.startMedia( mediaPath ); | |
currentMedia = mediaPath; | |
} | |
} | |
public boolean isPlaying () | |
{ | |
return mediaPlayer.isPlaying(); | |
} | |
public void pause () | |
{ | |
if ( mediaPlayer.canPause() ) | |
mediaPlayer.setPause(true); | |
} | |
// blocks | |
public boolean start () | |
{ | |
return mediaPlayer.start(); | |
} | |
public void stop () | |
{ | |
mediaPlayer.stop(); | |
} | |
public long getTime () | |
{ | |
return mediaPlayer.getTime(); | |
} | |
public void setTime ( long delta ) | |
{ | |
mediaPlayer.setTime( delta ); | |
} | |
public float getPosition () | |
{ | |
return mediaPlayer.getPosition(); | |
} | |
public void setPosition ( float delta ) | |
{ | |
mediaPlayer.setPosition( delta ); | |
} | |
public long getLength () | |
{ | |
return mediaPlayer.getLength(); | |
} | |
public float getFps () | |
{ | |
return mediaPlayer.getFps(); | |
} | |
public float getRate () | |
{ | |
return mediaPlayer.getRate(); | |
} | |
public void mute () | |
{ | |
mediaPlayer.mute(); | |
} | |
public void unmute () | |
{ | |
mediaPlayer.mute(false); | |
} | |
public void read () | |
{ | |
newFrameAvailable = false; | |
} | |
public boolean available () | |
{ | |
return newFrameAvailable; | |
} | |
public int width () | |
{ | |
return width; | |
} | |
public int height () | |
{ | |
return height; | |
} | |
public PImage frame () | |
{ | |
return frame; | |
} | |
public uk.co.caprica.vlcj.player.MediaPlayer getMediaPlayer () | |
{ | |
return mediaPlayer; | |
} | |
/* PApplet shutdown hook */ | |
public void dispose () | |
{ | |
if ( mediaPlayer != null ) | |
mediaPlayer.release(); | |
if ( factory != null ) | |
factory.release(); | |
} | |
class VLCPlayerBufferFormatCallback | |
implements BufferFormatCallback | |
{ | |
@Override | |
public BufferFormat getBufferFormat ( int sourceWidth, int sourceHeight ) | |
{ | |
// reset frame to real image size | |
initFrame( sourceWidth, sourceHeight ); | |
return new uk.co.caprica.vlcj.player.direct.BufferFormat( "RGBA", sourceWidth, sourceHeight, new int[] { sourceWidth * 4 }, new int[] { sourceHeight }); | |
//return new RV32BufferFormat( sourceWidth, sourceHeight ); // use with VLCPlayerRenderCallbackAdapter below | |
} | |
} | |
private class VLCPlayerRenderCallbackAdapter | |
extends RenderCallbackAdapter | |
{ | |
private VLCPlayerRenderCallbackAdapter () | |
{ | |
super( new int[ width * height ] ); | |
} | |
protected void onDisplay( DirectMediaPlayer mediaPlayer, int[] rgbBuffer ) | |
{ | |
System.arraycopy( rgbBuffer, 0, frame.pixels, 0, rgbBuffer.length ); | |
frame.updatePixels(); | |
newFrameAvailable = true; | |
} | |
} | |
class VLCPlayerRenderCallback | |
implements uk.co.caprica.vlcj.player.direct.RenderCallback | |
{ | |
public void display( DirectMediaPlayer mediaPlayer, Memory[] nativeBuffers, BufferFormat bufferFormat ) | |
{ | |
java.nio.ByteBuffer buffer = nativeBuffers[0].getByteBuffer(0, (int) bufferFormat.getWidth() * (int) bufferFormat.getHeight() * 4); | |
for ( int i = 0, n = 0; i < frame.pixels.length; i++, n+=4 ) frame.pixels[i] = (buffer.get(n+3) << 24) + (buffer.get(n) << 16) + (buffer.get(n+1) << 8) + (buffer.get(n+2)); | |
frame.updatePixels(); | |
newFrameAvailable = true; | |
} | |
} | |
private ArrayList<Object> mediaChangedListeners; | |
public void addMediaChangedListener ( Object o ) | |
{ | |
if ( o == null ) return; | |
Method meth = null; | |
try { | |
meth = o.getClass().getDeclaredMethod( "mediaChanged", new Class[] { | |
VLCPlayer.class | |
} | |
); | |
} | |
catch ( NoSuchMethodException nsme ) { | |
nsme.printStackTrace(); | |
} | |
if ( meth == null ) return; | |
if ( mediaChangedListeners == null ) mediaChangedListeners = new ArrayList<Object>(); | |
mediaChangedListeners.add( o ); | |
} | |
private void handleMediaChanged () | |
{ | |
if ( mediaChangedListeners != null ) | |
{ | |
Method meth = null; | |
for ( Object o : mediaChangedListeners ) | |
{ | |
meth = null; | |
try { | |
meth = o.getClass().getDeclaredMethod( "mediaChanged", new Class[] { | |
VLCPlayer.class | |
} | |
); | |
} | |
catch ( NoSuchMethodException nsme ) { | |
nsme.printStackTrace(); | |
} | |
if ( meth != null ) { | |
try { | |
meth.invoke( o, this ); | |
} | |
catch ( IllegalAccessException iae ) { | |
iae.printStackTrace(); | |
} | |
catch ( InvocationTargetException ite ) { | |
ite.printStackTrace(); | |
} | |
} | |
} | |
} | |
} | |
class VLCPlayerEventAdapter | |
extends uk.co.caprica.vlcj.player.MediaPlayerEventAdapter | |
{ | |
public void backward(MediaPlayer mediaPlayer) { | |
} | |
public void buffering(MediaPlayer mediaPlayer) { | |
} | |
public void endOfSubItems(MediaPlayer mediaPlayer) { | |
} | |
public void error(MediaPlayer mediaPlayer) { | |
System.err.println( currentMedia ); | |
System.err.println( "An error occured!" ); | |
} | |
public void finished(MediaPlayer mediaPlayer) { | |
} | |
public void forward(MediaPlayer mediaPlayer) { | |
} | |
public void lengthChanged(MediaPlayer mediaPlayer, long newLength) { | |
} | |
public void mediaChanged(MediaPlayer mediaPlayer) { | |
// System.out.println( currentMedia ); | |
// System.out.println( "Media changed" ); | |
handleMediaChanged(); | |
} | |
public void mediaDurationChanged(MediaPlayer mediaPlayer, long newDuration) { | |
} | |
public void mediaFreed(MediaPlayer mediaPlayer) { | |
} | |
public void mediaMetaChanged(MediaPlayer mediaPlayer, int metaType) { | |
} | |
public void mediaParsedChanged(MediaPlayer mediaPlayer, int newStatus) { | |
} | |
public void mediaStateChanged(MediaPlayer mediaPlayer, int newState) { | |
} | |
public void mediaSubItemAdded(MediaPlayer mediaPlayer, uk.co.caprica.vlcj.binding.internal.libvlc_meta_t subItem) { | |
} | |
public void newMedia(MediaPlayer mediaPlayer) { | |
} | |
public void opening(MediaPlayer mediaPlayer) { | |
} | |
public void pausableChanged(MediaPlayer mediaPlayer, int newSeekable) { | |
} | |
public void paused(MediaPlayer mediaPlayer) { | |
} | |
public void playing(MediaPlayer mediaPlayer) { | |
} | |
public void positionChanged(MediaPlayer mediaPlayer, float newPosition) { | |
// System.out.println( currentMedia ); | |
// System.out.println( "Position changed: " + newPosition ); | |
} | |
public void seekableChanged(MediaPlayer mediaPlayer, int newSeekable) { | |
} | |
public void snapshotTaken(MediaPlayer mediaPlayer, java.lang.String filename) { | |
} | |
public void stopped(MediaPlayer mediaPlayer) { | |
} | |
public void subItemFinished(MediaPlayer mediaPlayer, int subItemIndex) { | |
} | |
public void subItemPlayed(MediaPlayer mediaPlayer, int subItemIndex) { | |
} | |
public void timeChanged(MediaPlayer mediaPlayer, long newTime) { | |
// System.out.println( currentMedia ); | |
// System.out.println( "Time changed: " + newTime ); | |
} | |
public void titleChanged(MediaPlayer mediaPlayer, int newTitle) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment