Teng-Hui Zhu | 778029e | 2012-08-01 13:58:10 -0700 | [diff] [blame] | 1 | /* |
| 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 Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 16 | |
| 17 | package android.webkit; |
| 18 | |
Teng-Hui Zhu | fb2fd5f | 2011-09-26 17:10:41 -0700 | [diff] [blame] | 19 | import android.Manifest.permission; |
| 20 | import android.content.pm.PackageManager; |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 21 | import android.graphics.SurfaceTexture; |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 22 | import android.webkit.HTML5VideoView; |
| 23 | import android.webkit.HTML5VideoViewProxy; |
Jamie Gennis | fd8feee | 2011-08-28 17:33:58 -0700 | [diff] [blame] | 24 | import android.view.Surface; |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 25 | import android.opengl.GLES20; |
Teng-Hui Zhu | 690ad54 | 2011-09-08 13:31:28 -0700 | [diff] [blame] | 26 | import android.os.PowerManager; |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * @hide This is only used by the browser |
| 30 | */ |
| 31 | public class HTML5VideoInline extends HTML5VideoView{ |
| 32 | |
Teng-Hui Zhu | 3fafd39 | 2011-05-31 15:15:31 -0700 | [diff] [blame] | 33 | // 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 Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 37 | private static SurfaceTexture mSurfaceTexture = null; |
Teng-Hui Zhu | c2b06d5 | 2012-05-09 14:13:52 -0700 | [diff] [blame] | 38 | private static int[] mTextureNames = null; |
Teng-Hui Zhu | 3fafd39 | 2011-05-31 15:15:31 -0700 | [diff] [blame] | 39 | // 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 Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 42 | |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 43 | // Video control FUNCTIONS: |
| 44 | @Override |
| 45 | public void start() { |
Teng-Hui Zhu | 158fbdb | 2011-03-22 11:39:23 -0700 | [diff] [blame] | 46 | if (!getPauseDuringPreparing()) { |
| 47 | super.start(); |
| 48 | } |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Teng-Hui Zhu | c3a2858 | 2012-05-31 17:36:17 -0700 | [diff] [blame] | 51 | HTML5VideoInline(int videoLayerId, int position, boolean skipPrepare) { |
| 52 | init(videoLayerId, position, skipPrepare); |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void decideDisplayMode() { |
Jamie Gennis | fd8feee | 2011-08-28 17:33:58 -0700 | [diff] [blame] | 57 | SurfaceTexture surfaceTexture = getSurfaceTexture(getVideoLayerId()); |
| 58 | Surface surface = new Surface(surfaceTexture); |
| 59 | mPlayer.setSurface(surface); |
| 60 | surface.release(); |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 61 | } |
| 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 Zhu | fb2fd5f | 2011-09-26 17:10:41 -0700 | [diff] [blame] | 69 | // 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 Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 75 | } |
| 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 Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Inline Video specific FUNCTIONS: |
| 84 | |
Teng-Hui Zhu | c2b06d5 | 2012-05-09 14:13:52 -0700 | [diff] [blame] | 85 | public static SurfaceTexture getSurfaceTexture(int videoLayerId) { |
Teng-Hui Zhu | 3fafd39 | 2011-05-31 15:15:31 -0700 | [diff] [blame] | 86 | // Create the surface texture. |
| 87 | if (videoLayerId != mVideoLayerUsingSurfaceTexture |
Teng-Hui Zhu | 1f54673 | 2011-11-02 15:50:00 -0700 | [diff] [blame] | 88 | || mSurfaceTexture == null |
| 89 | || mTextureNames == null) { |
Teng-Hui Zhu | c2b06d5 | 2012-05-09 14:13:52 -0700 | [diff] [blame] | 90 | // 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 Zhu | 1f54673 | 2011-11-02 15:50:00 -0700 | [diff] [blame] | 93 | mTextureNames = new int[1]; |
| 94 | GLES20.glGenTextures(1, mTextureNames, 0); |
Teng-Hui Zhu | 3fafd39 | 2011-05-31 15:15:31 -0700 | [diff] [blame] | 95 | mSurfaceTexture = new SurfaceTexture(mTextureNames[0]); |
| 96 | } |
| 97 | mVideoLayerUsingSurfaceTexture = videoLayerId; |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 98 | return mSurfaceTexture; |
| 99 | } |
| 100 | |
Teng-Hui Zhu | c3a2858 | 2012-05-31 17:36:17 -0700 | [diff] [blame] | 101 | public static boolean surfaceTextureDeleted() { |
Teng-Hui Zhu | 3fafd39 | 2011-05-31 15:15:31 -0700 | [diff] [blame] | 102 | return (mSurfaceTexture == null); |
| 103 | } |
| 104 | |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 105 | @Override |
| 106 | public void deleteSurfaceTexture() { |
Teng-Hui Zhu | fdd646b | 2012-02-29 17:21:35 -0800 | [diff] [blame] | 107 | cleanupSurfaceTexture(); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | public static void cleanupSurfaceTexture() { |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 112 | mSurfaceTexture = null; |
Teng-Hui Zhu | 3fafd39 | 2011-05-31 15:15:31 -0700 | [diff] [blame] | 113 | mVideoLayerUsingSurfaceTexture = -1; |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 117 | @Override |
| 118 | public int getTextureName() { |
Teng-Hui Zhu | 3fafd39 | 2011-05-31 15:15:31 -0700 | [diff] [blame] | 119 | if (mTextureNames != null) { |
| 120 | return mTextureNames[0]; |
| 121 | } else { |
| 122 | return 0; |
| 123 | } |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 126 | private void setFrameAvailableListener(SurfaceTexture.OnFrameAvailableListener l) { |
Teng-Hui Zhu | c3a2858 | 2012-05-31 17:36:17 -0700 | [diff] [blame] | 127 | if (mSurfaceTexture != null) { |
| 128 | mSurfaceTexture.setOnFrameAvailableListener(l); |
| 129 | } |
Teng-Hui Zhu | 10ab6549 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } |