HTML5VideoInline.java revision 1f5467338115c7e5ad1cfa54c362be86028bd6ea
1
2package android.webkit;
3
4import android.Manifest.permission;
5import android.content.pm.PackageManager;
6import android.graphics.SurfaceTexture;
7import android.media.MediaPlayer;
8import android.webkit.HTML5VideoView;
9import android.webkit.HTML5VideoViewProxy;
10import android.view.Surface;
11import android.opengl.GLES20;
12import android.os.PowerManager;
13
14/**
15 * @hide This is only used by the browser
16 */
17public class HTML5VideoInline extends HTML5VideoView{
18
19    // Due to the fact that the decoder consume a lot of memory, we make the
20    // surface texture as singleton. But the GL texture (m_textureNames)
21    // associated with the surface texture can be used for showing the screen
22    // shot when paused, so they are not singleton.
23    private static SurfaceTexture mSurfaceTexture = null;
24    private int[] mTextureNames;
25    // Every time when the VideoLayer Id change, we need to recreate the
26    // SurfaceTexture in order to delete the old video's decoder memory.
27    private static int mVideoLayerUsingSurfaceTexture = -1;
28
29    // Video control FUNCTIONS:
30    @Override
31    public void start() {
32        if (!getPauseDuringPreparing()) {
33            super.start();
34        }
35    }
36
37    HTML5VideoInline(int videoLayerId, int position,
38            boolean autoStart) {
39        init(videoLayerId, position, autoStart);
40        mTextureNames = null;
41    }
42
43    @Override
44    public void decideDisplayMode() {
45        SurfaceTexture surfaceTexture = getSurfaceTexture(getVideoLayerId());
46        Surface surface = new Surface(surfaceTexture);
47        mPlayer.setSurface(surface);
48        surface.release();
49    }
50
51    // Normally called immediately after setVideoURI. But for full screen,
52    // this should be after surface holder created
53    @Override
54    public void prepareDataAndDisplayMode(HTML5VideoViewProxy proxy) {
55        super.prepareDataAndDisplayMode(proxy);
56        setFrameAvailableListener(proxy);
57        // TODO: This is a workaround, after b/5375681 fixed, we should switch
58        // to the better way.
59        if (mProxy.getContext().checkCallingOrSelfPermission(permission.WAKE_LOCK)
60                == PackageManager.PERMISSION_GRANTED) {
61            mPlayer.setWakeMode(proxy.getContext(), PowerManager.FULL_WAKE_LOCK);
62        }
63    }
64
65    // Pause the play and update the play/pause button
66    @Override
67    public void pauseAndDispatch(HTML5VideoViewProxy proxy) {
68        super.pauseAndDispatch(proxy);
69    }
70
71    // Inline Video specific FUNCTIONS:
72
73    @Override
74    public SurfaceTexture getSurfaceTexture(int videoLayerId) {
75        // Create the surface texture.
76        if (videoLayerId != mVideoLayerUsingSurfaceTexture
77            || mSurfaceTexture == null
78            || mTextureNames == null) {
79            if (mTextureNames != null) {
80                GLES20.glDeleteTextures(1, mTextureNames, 0);
81            }
82            mTextureNames = new int[1];
83            GLES20.glGenTextures(1, mTextureNames, 0);
84            mSurfaceTexture = new SurfaceTexture(mTextureNames[0]);
85        }
86        mVideoLayerUsingSurfaceTexture = videoLayerId;
87        return mSurfaceTexture;
88    }
89
90    public boolean surfaceTextureDeleted() {
91        return (mSurfaceTexture == null);
92    }
93
94    @Override
95    public void deleteSurfaceTexture() {
96        mSurfaceTexture = null;
97        mVideoLayerUsingSurfaceTexture = -1;
98        return;
99    }
100
101    @Override
102    public int getTextureName() {
103        if (mTextureNames != null) {
104            return mTextureNames[0];
105        } else {
106            return 0;
107        }
108    }
109
110    private void setFrameAvailableListener(SurfaceTexture.OnFrameAvailableListener l) {
111        mSurfaceTexture.setOnFrameAvailableListener(l);
112    }
113
114}
115