Advertisement
Guest User

Untitled

a guest
Jun 28th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. package main;
  2.  
  3. import java.io.File;
  4. import java.nio.IntBuffer;
  5.  
  6. import static org.lwjgl.opengl.GL11.*;
  7. import static org.lwjgl.opengl.GL12.GL_CLAMP_TO_EDGE;
  8.  
  9. import org.gstreamer.Gst;
  10. import org.gstreamer.State;
  11. import org.gstreamer.elements.PlayBin2;
  12. import org.gstreamer.elements.RGBDataSink;
  13. import org.lwjgl.opengl.Display;
  14.  
  15. public class GStreamer_TestPlayer
  16. {
  17.    private int WIDTH, HEIGHT;
  18.    private int texture;
  19.    private IntBuffer buffer = null;
  20.    private long before, msLength;
  21.    private boolean done = false;
  22.    private PlayBin2 playbin;
  23.    
  24.    public GStreamer_TestPlayer(int _w, int _h, String strToPlay, long _msLength)
  25.    {
  26.       WIDTH = _w;
  27.       HEIGHT = _h;
  28.       msLength = _msLength;
  29.      
  30.       texture = glGenTextures();
  31.       glBindTexture(GL_TEXTURE_2D, texture);
  32.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  33.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  34.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  35.       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  36.       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, (IntBuffer)null);
  37.      
  38.      
  39.       //Gstreamer stuff
  40.       try   {   LibraryLoaderImpl.getInstance().load(); } catch (Exception e){e.printStackTrace();}
  41.    
  42.       Gst.init("VideoPlayer", null);
  43.       playbin = new PlayBin2("VideoPlayer");
  44.      
  45.       RGBListener listener = new RGBListener();
  46.       playbin.setInputFile(new File(strToPlay));
  47.      
  48.       RGBDataSink sink = new RGBDataSink(("sink"), listener);
  49.       sink.setPassDirectBuffer(true);
  50.       playbin.setVideoSink(sink);
  51.      
  52.       Gst.getExecutor().execute(new Runnable()
  53.       {
  54.           public void run()
  55.           {
  56. //              playbin.setState(State.PLAYING)
  57.             playbin.play();
  58.           }
  59.       });
  60.        
  61.       before = System.currentTimeMillis();
  62.    }
  63.    
  64.    public boolean isDone() { return done; }
  65.  
  66.    public void update()
  67.    {
  68.        float scaleFactor = 1.0f;
  69.        if (Display.getWidth() >= Display.getHeight())
  70.        {
  71.            scaleFactor = (float)Display.getHeight()/(float)HEIGHT;
  72.        }
  73.        else
  74.        {
  75.            scaleFactor = (float)Display.getWidth()/(float)WIDTH;
  76.        }
  77.        
  78.        glClear(GL_COLOR_BUFFER_BIT);
  79.        
  80.        glBindTexture(GL_TEXTURE_2D, texture);
  81.        if(buffer != null){
  82.           glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, WIDTH, HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  83.        }
  84.        
  85.        float x1, x2, y1, y2;
  86.        y1 = (Display.getHeight()-(HEIGHT*scaleFactor))/2.0f;
  87.        y2 = y1 + HEIGHT*scaleFactor;
  88.        
  89.        x1 = (Display.getWidth()-(WIDTH*scaleFactor))/2.0f;
  90.        x2 = x1 + WIDTH*scaleFactor;
  91.        
  92.        glBegin(GL_QUADS);
  93.        {
  94.           glTexCoord2f(0, 0);
  95.           glVertex2f(x1, y1);
  96.          
  97.           glTexCoord2f(1, 0);
  98.           glVertex2f(x2, y1);
  99.          
  100.           glTexCoord2f(1, 1);
  101.           glVertex2f(x2, y2);
  102.          
  103.           glTexCoord2f(0, 1);
  104.           glVertex2f(x1, y2);
  105.        }
  106.        glEnd();
  107.        
  108.        if (System.currentTimeMillis() >= before+msLength)
  109.        {
  110.            done = true;
  111.        }
  112.    }
  113.    
  114.    public void destroy()
  115.    {
  116.        
  117.    }
  118.    
  119.    private class RGBListener implements RGBDataSink.Listener
  120.    {
  121.        public void rgbFrame(boolean preroll, int width, int height, IntBuffer rgb)
  122.        {
  123.             buffer = rgb;
  124.        }
  125.    }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement