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