1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.mediadump; 18 19 import java.io.IOException; 20 import java.io.BufferedOutputStream; 21 import java.io.BufferedWriter; 22 import java.io.File; 23 import java.io.FileWriter; 24 import java.io.FilenameFilter; 25 import java.io.FileOutputStream; 26 import java.io.File; 27 28 import java.lang.Integer; 29 import java.lang.Math; 30 import java.nio.ByteBuffer; 31 import java.nio.ByteOrder; 32 import java.nio.FloatBuffer; 33 import java.nio.channels.FileChannel; 34 import java.nio.IntBuffer; 35 import java.util.Properties; 36 37 import javax.microedition.khronos.egl.EGLConfig; 38 import javax.microedition.khronos.opengles.GL10; 39 40 import android.app.Activity; 41 import android.content.Context; 42 import android.content.pm.ActivityInfo; 43 import android.graphics.SurfaceTexture; 44 import android.media.MediaPlayer; 45 import android.opengl.GLES20; 46 import android.opengl.GLSurfaceView; 47 import android.opengl.GLUtils; 48 import android.opengl.Matrix; 49 import android.os.Bundle; 50 import android.util.Log; 51 import android.view.MotionEvent; 52 import android.view.Surface; 53 import android.view.SurfaceHolder; 54 import android.view.View; 55 import android.widget.MediaController; 56 import android.widget.MediaController.MediaPlayerControl; 57 58 /** 59 * A view to play a video, specified by VideoDumpConfig.VIDEO_URI, and dump the screen 60 * into raw RGB files. 61 * It uses a renderer to display each video frame over a surface texture, read pixels, 62 * and writes the pixels into a rgb file on sdcard. 63 * Those raw rgb files will be used to compare the quality distortion against 64 * the original video. They can be viewed with the RgbPlayer app for debugging. 65 */ 66 class VideoDumpView extends GLSurfaceView implements MediaPlayerControl { 67 private static final String TAG = "VideoDumpView"; 68 VideoDumpRenderer mRenderer; 69 private MediaController mMediaController; 70 private boolean mMediaControllerAttached = false; 71 private MediaPlayer mMediaPlayer = null; 72 private BufferedWriter mImageListWriter = null; 73 74 // A serials of configuration constants. 75 class VideoDumpConfig { 76 // Currently we are running with a local copy of the video. 77 // It should work with a "http://" sort of streaming url as well. 78 public static final String VIDEO_URI = "/sdcard/mediadump/sample.mp4"; 79 public static final String ROOT_DIR = "/sdcard/mediadump/"; 80 public static final String IMAGES_LIST = "images.lst"; 81 public static final String IMAGE_PREFIX = "img"; 82 public static final String IMAGE_SUFFIX = ".rgb"; 83 public static final String PROPERTY_FILE = "prop.xml"; 84 85 // So far, glReadPixels only supports two (format, type) combinations 86 // GL_RGB GL_UNSIGNED_SHORT_5_6_5 16 bits per pixel (default) 87 // GL_RGBA GL_UNSIGNED_BYTE 32 bits per pixel 88 public static final int PIXEL_FORMAT = GLES20.GL_RGB; 89 public static final int PIXEL_TYPE = PIXEL_FORMAT == GLES20.GL_RGBA 90 ? GLES20.GL_UNSIGNED_BYTE : GLES20.GL_UNSIGNED_SHORT_5_6_5; 91 public static final int BYTES_PER_PIXEL = 92 PIXEL_FORMAT == GLES20.GL_RGBA ? 4 : 2; 93 public static final boolean SET_CHOOSER 94 = PIXEL_FORMAT == GLES20.GL_RGBA ? true : false; 95 96 // On Motorola Xoom, it takes 100ms to read pixels and 180ms to write to a file 97 // to dump a complete 720p(1280*720) video frame. It's much slower than the frame 98 // playback interval (40ms). So we only dump a center block and it should be able 99 // to catch all the e2e distortion. A reasonable size of the block is 256x256, 100 // which takes 4ms to read pixels and 25 ms to write to a file. 101 public static final int MAX_DUMP_WIDTH = 256; 102 public static final int MAX_DUMP_HEIGHT = 256; 103 104 // TODO: MediaPlayer doesn't give back the video frame rate and we'll need to 105 // figure it by dividing the total number of frames by the duration. 106 public static final int FRAME_RATE = 25; 107 } 108 109 public VideoDumpView(Context context) { 110 super(context); 111 setEGLContextClientVersion(2); 112 // GLSurfaceView uses RGB_5_6_5 by default. 113 if (VideoDumpConfig.SET_CHOOSER) { 114 setEGLConfigChooser(8, 8, 8, 8, 8, 8); 115 } 116 mRenderer = new VideoDumpRenderer(context); 117 setRenderer(mRenderer); 118 } 119 120 @Override 121 public void onPause() { 122 stopPlayback(); 123 super.onPause(); 124 } 125 126 @Override 127 public void onResume() { 128 Log.d(TAG, "onResume"); 129 130 mMediaPlayer = new MediaPlayer(); 131 try { 132 mMediaPlayer.setDataSource(VideoDumpConfig.VIDEO_URI); 133 134 class RGBFilter implements FilenameFilter { 135 public boolean accept(File dir, String name) { 136 return (name.endsWith(VideoDumpConfig.IMAGE_SUFFIX)); 137 } 138 } 139 File dump_dir = new File(VideoDumpConfig.ROOT_DIR); 140 File[] dump_files = dump_dir.listFiles(new RGBFilter()); 141 for (File dump_file :dump_files) { 142 dump_file.delete(); 143 } 144 145 File image_list = new File(VideoDumpConfig.ROOT_DIR 146 + VideoDumpConfig.IMAGES_LIST); 147 image_list.delete(); 148 mImageListWriter = new BufferedWriter(new FileWriter(image_list)); 149 } catch (java.io.IOException e) { 150 Log.e(TAG, e.getMessage(), e); 151 } 152 153 queueEvent(new Runnable(){ 154 public void run() { 155 mRenderer.setMediaPlayer(mMediaPlayer); 156 mRenderer.setImageListWriter(mImageListWriter); 157 }}); 158 159 super.onResume(); 160 } 161 162 public void start() { 163 mMediaPlayer.start(); 164 } 165 166 public void pause() { 167 mMediaPlayer.pause(); 168 try { 169 mImageListWriter.flush(); 170 } catch (java.io.IOException e) { 171 Log.e(TAG, e.getMessage(), e); 172 } 173 } 174 175 public void stopPlayback() { 176 Log.d(TAG, "stopPlayback"); 177 178 if (mMediaPlayer != null) { 179 mMediaPlayer.stop(); 180 mMediaPlayer.release(); 181 mMediaPlayer = null; 182 } 183 if (mImageListWriter != null) { 184 try { 185 mImageListWriter.flush(); 186 mImageListWriter.close(); 187 } catch (java.io.IOException e) { 188 Log.e(TAG, e.getMessage(), e); 189 } 190 } else { 191 Log.d(TAG, "image list file was not written successfully."); 192 } 193 } 194 195 public void setMediaController(MediaController controller) { 196 if (mMediaController != null) { 197 mMediaController.hide(); 198 } 199 mMediaController = controller; 200 } 201 202 private void attachMediaController() { 203 if (mMediaPlayer != null && mMediaController != null) { 204 if (!mMediaControllerAttached) { 205 mMediaController.setMediaPlayer(this); 206 View anchorView = this.getParent() instanceof View ? 207 (View)this.getParent() : this; 208 mMediaController.setAnchorView(anchorView); 209 mMediaController.setEnabled(true); 210 mMediaControllerAttached = true; 211 } 212 mMediaController.show(); 213 } 214 } 215 216 private boolean isInPlaybackState() { 217 return (mMediaPlayer != null && mMediaPlayer.isPlaying()); 218 } 219 220 public boolean canPause () { 221 return true; 222 } 223 224 public boolean canSeekBackward () { 225 return true; 226 } 227 228 public boolean canSeekForward () { 229 return true; 230 } 231 232 public int getBufferPercentage () { 233 return 1; 234 } 235 236 public int getCurrentPosition () { 237 if (isInPlaybackState()) { 238 return mMediaPlayer.getCurrentPosition(); 239 } 240 return 0; 241 } 242 243 public int getDuration () { 244 return mMediaPlayer.getDuration(); 245 } 246 247 public boolean isPlaying () { 248 return isInPlaybackState() && mMediaPlayer.isPlaying(); 249 } 250 251 public void seekTo (int pos) { 252 mMediaPlayer.seekTo(pos); 253 } 254 255 @Override 256 public boolean onTouchEvent(MotionEvent ev) { 257 attachMediaController(); 258 return true; 259 } 260 261 /** 262 * A renderer to read each video frame from a media player, draw it over a surface 263 * texture, dump the on-screen pixels into a buffer, and writes the pixels into 264 * a rgb file on sdcard. 265 */ 266 private static class VideoDumpRenderer 267 implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener { 268 private static String TAG = "VideoDumpRenderer"; 269 270 /* All GL related fields from 271 * http://developer.android.com/resources/samples/ApiDemos/src/com/example 272 * /android/apis/graphics/GLES20TriangleRenderer.html 273 */ 274 private static final int FLOAT_SIZE_BYTES = 4; 275 private static final int TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES; 276 private static final int TRIANGLE_VERTICES_DATA_POS_OFFSET = 0; 277 private static final int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3; 278 private final float[] mTriangleVerticesData = { 279 // X, Y, Z, U, V 280 -1.0f, -1.0f, 0, 0.f, 0.f, 281 1.0f, -1.0f, 0, 1.f, 0.f, 282 -1.0f, 1.0f, 0, 0.f, 1.f, 283 1.0f, 1.0f, 0, 1.f, 1.f, 284 }; 285 286 private FloatBuffer mTriangleVertices; 287 288 private final String mVertexShader = 289 "uniform mat4 uMVPMatrix;\n" + 290 "uniform mat4 uSTMatrix;\n" + 291 "attribute vec4 aPosition;\n" + 292 "attribute vec4 aTextureCoord;\n" + 293 "varying vec2 vTextureCoord;\n" + 294 "void main() {\n" + 295 " gl_Position = uMVPMatrix * aPosition;\n" + 296 " vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n" + 297 "}\n"; 298 299 private final String mFragmentShader = 300 "#extension GL_OES_EGL_image_external : require\n" + 301 "precision mediump float;\n" + 302 "varying vec2 vTextureCoord;\n" + 303 "uniform samplerExternalOES sTexture;\n" + 304 "void main() {\n" + 305 " gl_FragColor = texture2D(sTexture, vTextureCoord);\n" + 306 "}\n"; 307 308 private float[] mMVPMatrix = new float[16]; 309 private float[] mSTMatrix = new float[16]; 310 311 private int mProgram; 312 private int mTextureID; 313 private int muMVPMatrixHandle; 314 private int muSTMatrixHandle; 315 private int maPositionHandle; 316 private int maTextureHandle; 317 318 private SurfaceTexture mSurface; 319 private boolean updateSurface = false; 320 321 // Magic key 322 private static int GL_TEXTURE_EXTERNAL_OES = 0x8D65; 323 324 325 /** 326 * Fields that reads video source and dumps to file. 327 */ 328 // The media player that loads and decodes the video. 329 // Not owned by this class. 330 private MediaPlayer mMediaPlayer; 331 // The frame number from media player. 332 private int mFrameNumber = 0; 333 // The frame number that is drawing on screen. 334 private int mDrawNumber = 0; 335 // The width and height of dumping block. 336 private int mWidth = 0; 337 private int mHeight = 0; 338 // The offset of the dumping block. 339 private int mStartX = 0; 340 private int mStartY = 0; 341 // A buffer to hold the dumping pixels. 342 private ByteBuffer mBuffer = null; 343 // A file writer to write the filenames of images. 344 private BufferedWriter mImageListWriter; 345 346 public VideoDumpRenderer(Context context) { 347 mTriangleVertices = ByteBuffer.allocateDirect( 348 mTriangleVerticesData.length * FLOAT_SIZE_BYTES) 349 .order(ByteOrder.nativeOrder()).asFloatBuffer(); 350 mTriangleVertices.put(mTriangleVerticesData).position(0); 351 352 Matrix.setIdentityM(mSTMatrix, 0); 353 } 354 355 public void setMediaPlayer(MediaPlayer player) { 356 mMediaPlayer = player; 357 } 358 359 public void setImageListWriter(BufferedWriter imageListWriter) { 360 mImageListWriter = imageListWriter; 361 } 362 363 /** 364 * Called to draw the current frame. 365 * This method is responsible for drawing the current frame. 366 */ 367 public void onDrawFrame(GL10 glUnused) { 368 boolean isNewFrame = false; 369 int frameNumber = 0; 370 371 synchronized(this) { 372 if (updateSurface) { 373 isNewFrame = true; 374 frameNumber = mFrameNumber; 375 mSurface.updateTexImage(); 376 mSurface.getTransformMatrix(mSTMatrix); 377 updateSurface = false; 378 } 379 } 380 381 // Initial clear. 382 GLES20.glClearColor(0.0f, 1.0f, 0.0f, 1.0f); 383 GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT); 384 385 // Load the program, which is the basics rules to draw the vertexes and textures. 386 GLES20.glUseProgram(mProgram); 387 checkGlError("glUseProgram"); 388 389 // Activate the texture. 390 GLES20.glActiveTexture(GLES20.GL_TEXTURE0); 391 GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID); 392 393 // Load the vertexes coordinates. Simple here since it only draw a rectangle 394 // that fits the whole screen. 395 mTriangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET); 396 GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 397 TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices); 398 checkGlError("glVertexAttribPointer maPosition"); 399 GLES20.glEnableVertexAttribArray(maPositionHandle); 400 checkGlError("glEnableVertexAttribArray maPositionHandle"); 401 402 // Load the texture coordinates, which is essentially a rectangle that fits 403 // the whole video frame. 404 mTriangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET); 405 GLES20.glVertexAttribPointer(maTextureHandle, 3, GLES20.GL_FLOAT, false, 406 TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices); 407 checkGlError("glVertexAttribPointer maTextureHandle"); 408 GLES20.glEnableVertexAttribArray(maTextureHandle); 409 checkGlError("glEnableVertexAttribArray maTextureHandle"); 410 411 // Set up the GL matrices. 412 Matrix.setIdentityM(mMVPMatrix, 0); 413 GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0); 414 GLES20.glUniformMatrix4fv(muSTMatrixHandle, 1, false, mSTMatrix, 0); 415 416 // Draw a rectangle and render the video frame as a texture on it. 417 GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); 418 checkGlError("glDrawArrays"); 419 GLES20.glFinish(); 420 421 if (isNewFrame) { // avoid duplicates. 422 Log.d(TAG, mDrawNumber + "/" + frameNumber + " before dumping " 423 + System.currentTimeMillis()); 424 DumpToFile(frameNumber); 425 Log.d(TAG, mDrawNumber + "/" + frameNumber + " after dumping " 426 + System.currentTimeMillis()); 427 428 mDrawNumber++; 429 } 430 } 431 432 // Call the GL function that dumps the screen into a buffer, then write to a file. 433 private void DumpToFile(int frameNumber) { 434 GLES20.glReadPixels(mStartX, mStartY, mWidth, mHeight, 435 VideoDumpConfig.PIXEL_FORMAT, 436 VideoDumpConfig.PIXEL_TYPE, 437 mBuffer); 438 checkGlError("glReadPixels"); 439 440 Log.d(TAG, mDrawNumber + "/" + frameNumber + " after glReadPixels " 441 + System.currentTimeMillis()); 442 443 String filename = VideoDumpConfig.ROOT_DIR + VideoDumpConfig.IMAGE_PREFIX 444 + frameNumber + VideoDumpConfig.IMAGE_SUFFIX; 445 try { 446 mImageListWriter.write(filename); 447 mImageListWriter.newLine(); 448 FileOutputStream fos = new FileOutputStream(filename); 449 fos.write(mBuffer.array()); 450 fos.close(); 451 } catch (java.io.IOException e) { 452 Log.e(TAG, e.getMessage(), e); 453 } 454 } 455 456 /** 457 * Called when the surface changed size. 458 * Called after the surface is created and whenever the OpenGL surface size changes. 459 */ 460 public void onSurfaceChanged(GL10 glUnused, int width, int height) { 461 Log.d(TAG, "Surface size: " + width + "x" + height); 462 463 int video_width = mMediaPlayer.getVideoWidth(); 464 int video_height = mMediaPlayer.getVideoHeight(); 465 Log.d(TAG, "Video size: " + video_width 466 + "x" + video_height); 467 468 // TODO: adjust video_width and video_height with the surface size. 469 GLES20.glViewport(0, 0, video_width, video_height); 470 471 mWidth = Math.min(VideoDumpConfig.MAX_DUMP_WIDTH, video_width); 472 mHeight = Math.min(VideoDumpConfig.MAX_DUMP_HEIGHT, video_height); 473 mStartX = video_width / mWidth / 2 * mWidth; 474 mStartY = video_height / mHeight / 2 * mHeight; 475 476 Log.d(TAG, "dumping block start at (" + mStartX + "," + mStartY + ") " 477 + "size " + mWidth + "x" + mHeight); 478 479 int image_size = mWidth * mHeight * VideoDumpConfig.BYTES_PER_PIXEL; 480 mBuffer = ByteBuffer.allocate(image_size); 481 482 int bpp[] = new int[3]; 483 GLES20.glGetIntegerv(GLES20.GL_RED_BITS, bpp, 0); 484 GLES20.glGetIntegerv(GLES20.GL_GREEN_BITS, bpp, 1); 485 GLES20.glGetIntegerv(GLES20.GL_BLUE_BITS, bpp, 2); 486 Log.d(TAG, "rgb bits: " + bpp[0] + "-" + bpp[1] + "-" + bpp[2]); 487 488 // Save the properties into a xml file 489 // so the RgbPlayer can understand the output format. 490 Properties prop = new Properties(); 491 prop.setProperty("width", Integer.toString(mWidth)); 492 prop.setProperty("height", Integer.toString(mHeight)); 493 prop.setProperty("startX", Integer.toString(mStartX)); 494 prop.setProperty("startY", Integer.toString(mStartY)); 495 prop.setProperty("bytesPerPixel", 496 Integer.toString(VideoDumpConfig.BYTES_PER_PIXEL)); 497 prop.setProperty("frameRate", Integer.toString(VideoDumpConfig.FRAME_RATE)); 498 try { 499 prop.storeToXML(new FileOutputStream(VideoDumpConfig.ROOT_DIR 500 + VideoDumpConfig.PROPERTY_FILE), ""); 501 } catch (java.io.IOException e) { 502 Log.e(TAG, e.getMessage(), e); 503 } 504 } 505 506 /** 507 * Called when the surface is created or recreated. 508 * Called when the rendering thread starts and whenever the EGL context is lost. 509 * A place to put code to create resources that need to be created when the rendering 510 * starts, and that need to be recreated when the EGL context is lost e.g. texture. 511 * Note that when the EGL context is lost, all OpenGL resources associated with 512 * that context will be automatically deleted. 513 */ 514 public void onSurfaceCreated(GL10 glUnused, EGLConfig config) { 515 Log.d(TAG, "onSurfaceCreated"); 516 517 /* Set up shaders and handles to their variables */ 518 mProgram = createProgram(mVertexShader, mFragmentShader); 519 if (mProgram == 0) { 520 return; 521 } 522 maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition"); 523 checkGlError("glGetAttribLocation aPosition"); 524 if (maPositionHandle == -1) { 525 throw new RuntimeException("Could not get attrib location for aPosition"); 526 } 527 maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord"); 528 checkGlError("glGetAttribLocation aTextureCoord"); 529 if (maTextureHandle == -1) { 530 throw new RuntimeException("Could not get attrib location for aTextureCoord"); 531 } 532 533 muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); 534 checkGlError("glGetUniformLocation uMVPMatrix"); 535 if (muMVPMatrixHandle == -1) { 536 throw new RuntimeException("Could not get attrib location for uMVPMatrix"); 537 } 538 539 muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix"); 540 checkGlError("glGetUniformLocation uSTMatrix"); 541 if (muSTMatrixHandle == -1) { 542 throw new RuntimeException("Could not get attrib location for uSTMatrix"); 543 } 544 545 546 // Create our texture. This has to be done each time the surface is created. 547 int[] textures = new int[1]; 548 GLES20.glGenTextures(1, textures, 0); 549 550 mTextureID = textures[0]; 551 GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID); 552 checkGlError("glBindTexture mTextureID"); 553 554 // Can't do mipmapping with mediaplayer source 555 GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, 556 GLES20.GL_NEAREST); 557 GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, 558 GLES20.GL_LINEAR); 559 // Clamp to edge is the only option 560 GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, 561 GLES20.GL_CLAMP_TO_EDGE); 562 GLES20.glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, 563 GLES20.GL_CLAMP_TO_EDGE); 564 checkGlError("glTexParameteri mTextureID"); 565 566 /* 567 * Create the SurfaceTexture that will feed this textureID, 568 * and pass it to the MediaPlayer 569 */ 570 mSurface = new SurfaceTexture(mTextureID); 571 mSurface.setOnFrameAvailableListener(this); 572 573 Surface surface = new Surface(mSurface); 574 mMediaPlayer.setSurface(surface); 575 surface.release(); 576 577 try { 578 mMediaPlayer.prepare(); 579 } catch (IOException t) { 580 Log.e(TAG, "media player prepare failed"); 581 } 582 583 synchronized(this) { 584 updateSurface = false; 585 } 586 } 587 588 synchronized public void onFrameAvailable(SurfaceTexture surface) { 589 /* For simplicity, SurfaceTexture calls here when it has new 590 * data available. Call may come in from some random thread, 591 * so let's be safe and use synchronize. No OpenGL calls can be done here. 592 */ 593 mFrameNumber++; 594 updateSurface = true; 595 } 596 597 private int loadShader(int shaderType, String source) { 598 int shader = GLES20.glCreateShader(shaderType); 599 if (shader != 0) { 600 GLES20.glShaderSource(shader, source); 601 GLES20.glCompileShader(shader); 602 int[] compiled = new int[1]; 603 GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, compiled, 0); 604 if (compiled[0] == 0) { 605 Log.e(TAG, "Could not compile shader " + shaderType + ":"); 606 Log.e(TAG, GLES20.glGetShaderInfoLog(shader)); 607 GLES20.glDeleteShader(shader); 608 shader = 0; 609 } 610 } 611 return shader; 612 } 613 614 private int createProgram(String vertexSource, String fragmentSource) { 615 int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource); 616 if (vertexShader == 0) { 617 return 0; 618 } 619 int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource); 620 if (pixelShader == 0) { 621 return 0; 622 } 623 624 int program = GLES20.glCreateProgram(); 625 if (program != 0) { 626 GLES20.glAttachShader(program, vertexShader); 627 checkGlError("glAttachShader"); 628 GLES20.glAttachShader(program, pixelShader); 629 checkGlError("glAttachShader"); 630 GLES20.glLinkProgram(program); 631 int[] linkStatus = new int[1]; 632 GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0); 633 if (linkStatus[0] != GLES20.GL_TRUE) { 634 Log.e(TAG, "Could not link program: "); 635 Log.e(TAG, GLES20.glGetProgramInfoLog(program)); 636 GLES20.glDeleteProgram(program); 637 program = 0; 638 } 639 } 640 return program; 641 } 642 643 private void checkGlError(String op) { 644 int error; 645 while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) { 646 Log.e(TAG, op + ": glError " + error); 647 throw new RuntimeException(op + ": glError " + error); 648 } 649 } 650 651 } // End of class VideoDumpRender. 652 653 } // End of class VideoDumpView. 654