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 static int[] mTextureNames = null;
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        init(videoLayerId, position, false);
39    }
40
41    @Override
42    public void decideDisplayMode() {
43        SurfaceTexture surfaceTexture = getSurfaceTexture(getVideoLayerId());
44        Surface surface = new Surface(surfaceTexture);
45        mPlayer.setSurface(surface);
46        surface.release();
47    }
48
49    // Normally called immediately after setVideoURI. But for full screen,
50    // this should be after surface holder created
51    @Override
52    public void prepareDataAndDisplayMode(HTML5VideoViewProxy proxy) {
53        super.prepareDataAndDisplayMode(proxy);
54        setFrameAvailableListener(proxy);
55        // TODO: This is a workaround, after b/5375681 fixed, we should switch
56        // to the better way.
57        if (mProxy.getContext().checkCallingOrSelfPermission(permission.WAKE_LOCK)
58                == PackageManager.PERMISSION_GRANTED) {
59            mPlayer.setWakeMode(proxy.getContext(), PowerManager.FULL_WAKE_LOCK);
60        }
61    }
62
63    // Pause the play and update the play/pause button
64    @Override
65    public void pauseAndDispatch(HTML5VideoViewProxy proxy) {
66        super.pauseAndDispatch(proxy);
67    }
68
69    // Inline Video specific FUNCTIONS:
70
71    public static SurfaceTexture getSurfaceTexture(int videoLayerId) {
72        // Create the surface texture.
73        if (videoLayerId != mVideoLayerUsingSurfaceTexture
74            || mSurfaceTexture == null
75            || mTextureNames == null) {
76            // The GL texture will store in the VideoLayerManager at native side.
77            // They will be clean up when requested.
78            // The reason we recreated GL texture name is for screen shot support.
79            mTextureNames = new int[1];
80            GLES20.glGenTextures(1, mTextureNames, 0);
81            mSurfaceTexture = new SurfaceTexture(mTextureNames[0]);
82        }
83        mVideoLayerUsingSurfaceTexture = videoLayerId;
84        return mSurfaceTexture;
85    }
86
87    public boolean surfaceTextureDeleted() {
88        return (mSurfaceTexture == null);
89    }
90
91    @Override
92    public void deleteSurfaceTexture() {
93        cleanupSurfaceTexture();
94        return;
95    }
96
97    public static void cleanupSurfaceTexture() {
98        mSurfaceTexture = null;
99        mVideoLayerUsingSurfaceTexture = -1;
100        return;
101    }
102
103    @Override
104    public int getTextureName() {
105        if (mTextureNames != null) {
106            return mTextureNames[0];
107        } else {
108            return 0;
109        }
110    }
111
112    private void setFrameAvailableListener(SurfaceTexture.OnFrameAvailableListener l) {
113        mSurfaceTexture.setOnFrameAvailableListener(l);
114    }
115
116}
117