blob: 2ab2ab94ca4c25ef0dc671b99ec95f97556d1200 [file] [log] [blame]
Teng-Hui Zhu778029e2012-08-01 13:58:10 -07001/*
2 * Copyright (C) 2012 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 */
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070016
17package android.webkit;
18
Teng-Hui Zhufb2fd5f2011-09-26 17:10:41 -070019import android.Manifest.permission;
20import android.content.pm.PackageManager;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070021import android.graphics.SurfaceTexture;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070022import android.webkit.HTML5VideoView;
23import android.webkit.HTML5VideoViewProxy;
Jamie Gennisfd8feee2011-08-28 17:33:58 -070024import android.view.Surface;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070025import android.opengl.GLES20;
Teng-Hui Zhu690ad542011-09-08 13:31:28 -070026import android.os.PowerManager;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070027
28/**
29 * @hide This is only used by the browser
30 */
31public class HTML5VideoInline extends HTML5VideoView{
32
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -070033 // Due to the fact that the decoder consume a lot of memory, we make the
34 // surface texture as singleton. But the GL texture (m_textureNames)
35 // associated with the surface texture can be used for showing the screen
36 // shot when paused, so they are not singleton.
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070037 private static SurfaceTexture mSurfaceTexture = null;
Teng-Hui Zhuc2b06d52012-05-09 14:13:52 -070038 private static int[] mTextureNames = null;
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -070039 // Every time when the VideoLayer Id change, we need to recreate the
40 // SurfaceTexture in order to delete the old video's decoder memory.
41 private static int mVideoLayerUsingSurfaceTexture = -1;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070042
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070043 // Video control FUNCTIONS:
44 @Override
45 public void start() {
Teng-Hui Zhu158fbdb2011-03-22 11:39:23 -070046 if (!getPauseDuringPreparing()) {
47 super.start();
48 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070049 }
50
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -070051 HTML5VideoInline(int videoLayerId, int position, boolean skipPrepare) {
52 init(videoLayerId, position, skipPrepare);
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070053 }
54
55 @Override
56 public void decideDisplayMode() {
Jamie Gennisfd8feee2011-08-28 17:33:58 -070057 SurfaceTexture surfaceTexture = getSurfaceTexture(getVideoLayerId());
58 Surface surface = new Surface(surfaceTexture);
59 mPlayer.setSurface(surface);
60 surface.release();
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070061 }
62
63 // Normally called immediately after setVideoURI. But for full screen,
64 // this should be after surface holder created
65 @Override
66 public void prepareDataAndDisplayMode(HTML5VideoViewProxy proxy) {
67 super.prepareDataAndDisplayMode(proxy);
68 setFrameAvailableListener(proxy);
Teng-Hui Zhufb2fd5f2011-09-26 17:10:41 -070069 // TODO: This is a workaround, after b/5375681 fixed, we should switch
70 // to the better way.
71 if (mProxy.getContext().checkCallingOrSelfPermission(permission.WAKE_LOCK)
72 == PackageManager.PERMISSION_GRANTED) {
73 mPlayer.setWakeMode(proxy.getContext(), PowerManager.FULL_WAKE_LOCK);
74 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070075 }
76
77 // Pause the play and update the play/pause button
78 @Override
79 public void pauseAndDispatch(HTML5VideoViewProxy proxy) {
80 super.pauseAndDispatch(proxy);
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070081 }
82
83 // Inline Video specific FUNCTIONS:
84
Teng-Hui Zhuc2b06d52012-05-09 14:13:52 -070085 public static SurfaceTexture getSurfaceTexture(int videoLayerId) {
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -070086 // Create the surface texture.
87 if (videoLayerId != mVideoLayerUsingSurfaceTexture
Teng-Hui Zhu1f546732011-11-02 15:50:00 -070088 || mSurfaceTexture == null
89 || mTextureNames == null) {
Teng-Hui Zhuc2b06d52012-05-09 14:13:52 -070090 // The GL texture will store in the VideoLayerManager at native side.
91 // They will be clean up when requested.
92 // The reason we recreated GL texture name is for screen shot support.
Teng-Hui Zhu1f546732011-11-02 15:50:00 -070093 mTextureNames = new int[1];
94 GLES20.glGenTextures(1, mTextureNames, 0);
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -070095 mSurfaceTexture = new SurfaceTexture(mTextureNames[0]);
96 }
97 mVideoLayerUsingSurfaceTexture = videoLayerId;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -070098 return mSurfaceTexture;
99 }
100
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700101 public static boolean surfaceTextureDeleted() {
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -0700102 return (mSurfaceTexture == null);
103 }
104
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700105 @Override
106 public void deleteSurfaceTexture() {
Teng-Hui Zhufdd646b2012-02-29 17:21:35 -0800107 cleanupSurfaceTexture();
108 return;
109 }
110
111 public static void cleanupSurfaceTexture() {
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700112 mSurfaceTexture = null;
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -0700113 mVideoLayerUsingSurfaceTexture = -1;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700114 return;
115 }
116
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700117 @Override
118 public int getTextureName() {
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -0700119 if (mTextureNames != null) {
120 return mTextureNames[0];
121 } else {
122 return 0;
123 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700124 }
125
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700126 private void setFrameAvailableListener(SurfaceTexture.OnFrameAvailableListener l) {
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700127 if (mSurfaceTexture != null) {
128 mSurfaceTexture.setOnFrameAvailableListener(l);
129 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700130 }
131
132}