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 android.media.cts; 18 19 import android.media.cts.R; 20 21 import android.platform.test.annotations.AppModeFull; 22 import java.io.IOException; 23 24 import javax.microedition.khronos.egl.EGLConfig; 25 import javax.microedition.khronos.opengles.GL10; 26 27 import android.content.Context; 28 import android.graphics.SurfaceTexture; 29 import android.media.MediaPlayer; 30 import android.opengl.GLSurfaceView; 31 import android.opengl.Matrix; 32 import android.util.Log; 33 import android.view.Surface; 34 35 @AppModeFull(reason = "TODO: evaluate and port to instant") 36 class VideoSurfaceView extends GLSurfaceView { 37 private static final String TAG = "VideoSurfaceView"; 38 private static final int SLEEP_TIME_MS = 1000; 39 40 VideoRender mRenderer; 41 private MediaPlayer mMediaPlayer = null; 42 43 public VideoSurfaceView(Context context, MediaPlayer mp) { 44 super(context); 45 46 setEGLContextClientVersion(2); 47 mMediaPlayer = mp; 48 mRenderer = new VideoRender(context); 49 setRenderer(mRenderer); 50 } 51 52 @Override 53 public void onResume() { 54 queueEvent(new Runnable(){ 55 public void run() { 56 mRenderer.setMediaPlayer(mMediaPlayer); 57 }}); 58 59 super.onResume(); 60 } 61 62 public void startTest() throws Exception { 63 Thread.sleep(SLEEP_TIME_MS); 64 mMediaPlayer.start(); 65 66 Thread.sleep(SLEEP_TIME_MS * 5); 67 mMediaPlayer.setSurface(null); 68 69 while (mMediaPlayer.isPlaying()) { 70 Thread.sleep(SLEEP_TIME_MS); 71 } 72 } 73 74 /** 75 * A GLSurfaceView implementation that wraps TextureRender. Used to render frames from a 76 * video decoder to a View. 77 */ 78 private static class VideoRender 79 implements GLSurfaceView.Renderer, SurfaceTexture.OnFrameAvailableListener { 80 private static String TAG = "VideoRender"; 81 82 private TextureRender mTextureRender; 83 private SurfaceTexture mSurfaceTexture; 84 private boolean updateSurface = false; 85 86 private MediaPlayer mMediaPlayer; 87 88 public VideoRender(Context context) { 89 mTextureRender = new TextureRender(); 90 } 91 92 public void setMediaPlayer(MediaPlayer player) { 93 mMediaPlayer = player; 94 } 95 96 public void onDrawFrame(GL10 glUnused) { 97 synchronized(this) { 98 if (updateSurface) { 99 mSurfaceTexture.updateTexImage(); 100 updateSurface = false; 101 } 102 } 103 104 mTextureRender.drawFrame(mSurfaceTexture); 105 } 106 107 public void onSurfaceChanged(GL10 glUnused, int width, int height) { 108 } 109 110 public void onSurfaceCreated(GL10 glUnused, EGLConfig config) { 111 mTextureRender.surfaceCreated(); 112 113 /* 114 * Create the SurfaceTexture that will feed this textureID, 115 * and pass it to the MediaPlayer 116 */ 117 mSurfaceTexture = new SurfaceTexture(mTextureRender.getTextureId()); 118 mSurfaceTexture.setOnFrameAvailableListener(this); 119 120 Surface surface = new Surface(mSurfaceTexture); 121 mMediaPlayer.setSurface(surface); 122 surface.release(); 123 124 try { 125 mMediaPlayer.prepare(); 126 } catch (IOException t) { 127 Log.e(TAG, "media player prepare failed"); 128 } 129 130 synchronized(this) { 131 updateSurface = false; 132 } 133 } 134 135 synchronized public void onFrameAvailable(SurfaceTexture surface) { 136 updateSurface = true; 137 } 138 } // End of class VideoRender. 139 140 } // End of class VideoSurfaceView. 141