Android Open Source - glvideoplayer-android G L Video Renderer






From Project

Back to project page glvideoplayer-android.

License

The source code is released under:

Apache License

If you think the Android project glvideoplayer-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.binzume.android.glvideoplayer;
/*  www  .j av a2  s . c o  m*/
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;

import net.binzume.android.glvideoplayer.R;

import android.content.Context;
import android.hardware.Camera;
import android.net.Uri;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.Log;

/**
 * OpenGL???????video???????????
 * 
 * @author kosuke_kawahira
 */
public class GLVideoRenderer implements GLSurfaceView.Renderer {

  private final Context context;
  private int screenWidth, screenHeight;
  private int width, height;
  public GLShaderProgram glProgram;
  private int frame = 0;
  
  public boolean commentVisible = false;
  public boolean cameraVisible = false;

  public OESTexture videoTexture;
  public NicoCommentPlane commentTexture;
  private ShortBuffer mBgIndex;

  public GLVideoRenderer(Context context) {
    this.context = context;
  }
  
  private Uri videoUri;
  
  public void setVideoUri(Uri uri) {
    videoUri = uri;
  }

  @Override
  public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    EGL10 egl = (EGL10) EGLContext.getEGL();
    EGLDisplay dpy = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);

    int[] version = new int[2];
    egl.eglInitialize(dpy, version);

    checkGlError("init0");

    glProgram = new GLShaderProgram(context, R.raw.plane_v, R.raw.oculus_f);

    GLES20.glClearColor(0, 0, 0.3f, 1.0f);
    //GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    //GLES20.glEnable(GLES20.GL_CULL_FACE);
    //GLES20.glDepthFunc(GLES20.GL_LEQUAL);
    //GLES20.glFrontFace(GLES20.GL_CW);

    GLES20.glDisable(GLES20.GL_DEPTH_TEST);
    GLES20.glEnable(GLES20.GL_BLEND);
    // GLES20.glEnable(GLES20.GL_TEXTURE_2D);
    GLES20.glDisable(GLES20.GL_DITHER);
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

    width = 1280;
    height = 720;

    checkGlError("init");
  }

  public void destroy() {
    if (videoTexture != null) {
      videoTexture.release();
      videoTexture = null;
    }
    if (commentTexture != null) {
      commentTexture.release();
      commentTexture = null;
    }
  }

  @Override
  public void onDrawFrame(GL10 gl) {
    // ????
    GLES20.glClear(GL10.GL_COLOR_BUFFER_BIT);
    // GLES20.glEnable(GL10.GL_TEXTURE_2D);
    // GLES20.glActiveTexture(GL10.GL_TEXTURE0);

    frame++;

    if (videoTexture != null) {
      videoTexture.update();
      commentTexture.update();
    }

    if (videoTexture == null && frame > 20 && cameraVisible) {
      DeviceCameraPlane cameraPlane = new DeviceCameraPlane();
      this.videoTexture = cameraPlane;
      Camera camera = cameraPlane.camera;
      if (camera != null) {
        camera.setErrorCallback(new Camera.ErrorCallback() {
          @Override
          public void onError(int error, Camera camera) {
            Log.d("CameraView", "onError: error=" + error);
          }
        });
        Camera.Parameters params = camera.getParameters();
        cameraPlane.setPreviewSize(params, 640, 480);
        Log.d("CameraView", "Start preview");
        camera.startPreview();
      }
    }

    if (videoTexture == null && videoUri != null) {
      this.videoTexture = new MediaPlayerPlane(context, videoUri);
      commentTexture = new NicoCommentPlane();
    }

    if (videoTexture != null && videoTexture.textureName > 0) {
      GLES20.glUseProgram(glProgram.program);

      

      if (mBgIndex == null) {
        ByteBuffer bb = ByteBuffer.allocateDirect(6 * 2);
        bb.order(ByteOrder.nativeOrder());
        mBgIndex = bb.asShortBuffer();

        mBgIndex.put((short) 3);
        mBgIndex.put((short) 1);
        mBgIndex.put((short) 0);

        mBgIndex.put((short) 3);
        mBgIndex.put((short) 2);
        mBgIndex.put((short) 1);
        mBgIndex.flip();
      }
      
      GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
      videoTexture.bindBuffer(glProgram);

      GLES20.glActiveTexture(GLES20.GL_TEXTURE2);
      commentTexture.bindBuffer(glProgram);
      
      float aspect = 4.0f / 3.0f;
      if (videoTexture instanceof MediaPlayerPlane) {
        aspect = ((MediaPlayerPlane) videoTexture).getAspectRatio();
      }

      glProgram.resetMatrix();
      glProgram.translate(0, height+ 40);
      glProgram.scale(640, -height);
      glProgram.glColor4f(2.0f, +0.1f, 1.0f, aspect);

      GLES20.glUniform1i(glProgram.texExtHandle, 1);
      GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, mBgIndex);

      if (commentVisible) {
        GLES20.glUniform1i(glProgram.texExtHandle, 2);
        GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, mBgIndex);
      }

      
      glProgram.resetMatrix();
      glProgram.translate(640, height + 40);
      glProgram.scale(640, -height);
      glProgram.glColor4f(2.0f, -0.1f, 1.0f, aspect);

      GLES20.glUniform1i(glProgram.texExtHandle, 1);
      GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, mBgIndex);
    
      if (commentVisible) {
        GLES20.glUniform1i(glProgram.texExtHandle, 2);
        GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, mBgIndex);
      }
      
    }

    checkGlError("drawFrame");
  }

  @Override
  public void onSurfaceChanged(GL10 gl, int width, int height) {
    this.screenWidth = width;
    this.screenHeight = height;
    setRenderSize(screenWidth, screenHeight);

    checkGlError("change");
  }

  private void setRenderSize(int screenWidth, int screenHeight) {

    float scale = (float) screenWidth / width;
    if (height * scale > screenHeight) {
      scale = (float) screenHeight / height;
    }

    // ????
    float[] mat = new float[16];
    android.opengl.Matrix.setIdentityM(mat, 0);
    android.opengl.Matrix.orthoM(mat, 0, 0, screenWidth, screenHeight, 0, -1, 1);
    // android.opengl.Matrix.translateM(mat, 0, 0, screenHeight - height * scale, 0);
    android.opengl.Matrix.scaleM(mat, 0, scale, scale, 1.0f);

    GLES20.glViewport(0, 0, screenWidth, screenHeight);
    glProgram.setProjectionMatrix(mat);

    android.opengl.Matrix.setIdentityM(mat, 0);
    GLES20.glUniformMatrix4fv(glProgram.texMatrixHandle, 1, false, mat, 0);

    // this.scale = scale;

  }

  private void checkGlError(String op) {
    int error;
    while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
      Log.e("opengl", op + ": glError " + error);
    }
  }

}




Java Source Code List

net.binzume.android.glvideoplayer.DeviceCameraPlane.java
net.binzume.android.glvideoplayer.FBOTexture.java
net.binzume.android.glvideoplayer.GLShaderProgram.java
net.binzume.android.glvideoplayer.GLVideoRenderer.java
net.binzume.android.glvideoplayer.MainActivity.java
net.binzume.android.glvideoplayer.MediaPlayerPlane.java
net.binzume.android.glvideoplayer.NicoCommentPlane.java
net.binzume.android.glvideoplayer.OESTexture.java
net.binzume.android.glvideoplayer.TextureFactory.java
net.binzume.android.glvideoplayer.Texture.java
net.binzume.android.glvideoplayer.VideoSurfaceView.java
net.binzume.android.nicoplayer.player.CommentController.java
net.binzume.android.nicoplayer.player.CommentLoadTask.java
net.binzume.android.nicoplayer.player.CommentSlot.java
net.binzume.android.nicovideo.ChannelCategoryInfo.java
net.binzume.android.nicovideo.ChannelInfo.java
net.binzume.android.nicovideo.Comment.java
net.binzume.android.nicovideo.MyList.java
net.binzume.android.nicovideo.NicoSession.java
net.binzume.android.nicovideo.ThreadInfo.java
net.binzume.android.nicovideo.VideoFileInfo.java
net.binzume.android.nicovideo.VideoInfo.java
net.binzume.android.nicovideo.util.HtmlUtil.java
net.binzume.android.nicovideo.util.HttpClient.java
net.binzume.android.nicovideo.webapi.ChannelAPI.java
net.binzume.android.nicovideo.webapi.CommentAPI.java
net.binzume.android.nicovideo.webapi.Constants.java
net.binzume.android.nicovideo.webapi.FriendListAPI.java
net.binzume.android.nicovideo.webapi.LoginAPI.java
net.binzume.android.nicovideo.webapi.MyListAPI.java
net.binzume.android.nicovideo.webapi.NotLoginException.java
net.binzume.android.nicovideo.webapi.PaymentRequiredException.java
net.binzume.android.nicovideo.webapi.VideoAPI.java
net.binzume.android.nicovideo.webapi.VideoRssParser.java
net.binzume.android.nicovideo.webapi.VideoSearchAPI.java
net.binzume.android.nicovideo.webapi.WatchItemAPI.java
net.binzume.android.nicovideo.webapi.WebApiException.java