Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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.webkit; |
| 18 | |
| 19 | import android.content.Context; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.BitmapFactory; |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 22 | import android.graphics.SurfaceTexture; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 23 | import android.media.MediaPlayer; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 24 | import android.net.http.EventHandler; |
| 25 | import android.net.http.Headers; |
| 26 | import android.net.http.RequestHandle; |
| 27 | import android.net.http.RequestQueue; |
| 28 | import android.net.http.SslCertificate; |
| 29 | import android.net.http.SslError; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.Looper; |
| 32 | import android.os.Message; |
| 33 | import android.util.Log; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 34 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 35 | import java.io.ByteArrayOutputStream; |
| 36 | import java.io.IOException; |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 37 | import java.net.MalformedURLException; |
| 38 | import java.net.URL; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 39 | import java.util.HashMap; |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 40 | import java.util.Map; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 41 | |
| 42 | /** |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 43 | * <p>Proxy for HTML5 video views. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 44 | */ |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 45 | class HTML5VideoViewProxy extends Handler |
| 46 | implements MediaPlayer.OnPreparedListener, |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 47 | MediaPlayer.OnCompletionListener, |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 48 | MediaPlayer.OnErrorListener, |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 49 | MediaPlayer.OnInfoListener, |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 50 | SurfaceTexture.OnFrameAvailableListener { |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 51 | // Logging tag. |
| 52 | private static final String LOGTAG = "HTML5VideoViewProxy"; |
| 53 | |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 54 | // Message Ids for WebCore thread -> UI thread communication. |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 55 | private static final int PLAY = 100; |
| 56 | private static final int SEEK = 101; |
| 57 | private static final int PAUSE = 102; |
| 58 | private static final int ERROR = 103; |
| 59 | private static final int LOAD_DEFAULT_POSTER = 104; |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 60 | private static final int BUFFERING_START = 105; |
| 61 | private static final int BUFFERING_END = 106; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 62 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 63 | // Message Ids to be handled on the WebCore thread |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 64 | private static final int PREPARED = 200; |
| 65 | private static final int ENDED = 201; |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 66 | private static final int POSTER_FETCHED = 202; |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 67 | private static final int PAUSED = 203; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 68 | |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 69 | // Timer thread -> UI thread |
| 70 | private static final int TIMEUPDATE = 300; |
| 71 | |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 72 | // The C++ MediaPlayerPrivateAndroid object. |
| 73 | int mNativePointer; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 74 | // The handler for WebCore thread messages; |
| 75 | private Handler mWebCoreHandler; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 76 | // The WebView instance that created this view. |
| 77 | private WebView mWebView; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 78 | // The poster image to be shown when the video is not playing. |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 79 | // This ref prevents the bitmap from being GC'ed. |
| 80 | private Bitmap mPoster; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 81 | // The poster downloader. |
| 82 | private PosterDownloader mPosterDownloader; |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 83 | // The seek position. |
| 84 | private int mSeekPosition; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 85 | // A helper class to control the playback. This executes on the UI thread! |
| 86 | private static final class VideoPlayer { |
| 87 | // The proxy that is currently playing (if any). |
| 88 | private static HTML5VideoViewProxy mCurrentProxy; |
| 89 | // The VideoView instance. This is a singleton for now, at least until |
| 90 | // http://b/issue?id=1973663 is fixed. |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 91 | private static HTML5VideoView mHTML5VideoView; |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 92 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 93 | private static boolean isVideoSelfEnded = false; |
| 94 | // By using the baseLayer and the current video Layer ID, we can |
| 95 | // identify the exact layer on the UI thread to use the SurfaceTexture. |
| 96 | private static int mBaseLayer = 0; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 97 | |
Teng-Hui Zhu | f4d4e9e | 2011-03-30 14:39:56 -0700 | [diff] [blame] | 98 | private static void setPlayerBuffering(boolean playerBuffering) { |
| 99 | mHTML5VideoView.setPlayerBuffering(playerBuffering); |
| 100 | } |
| 101 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 102 | // Every time webView setBaseLayer, this will be called. |
| 103 | // When we found the Video layer, then we set the Surface Texture to it. |
| 104 | // Otherwise, we may want to delete the Surface Texture to save memory. |
| 105 | public static void setBaseLayer(int layer) { |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 106 | // Don't do this for full screen mode. |
| 107 | if (mHTML5VideoView != null |
| 108 | && !mHTML5VideoView.isFullScreenMode()) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 109 | mBaseLayer = layer; |
| 110 | SurfaceTexture surfTexture = mHTML5VideoView.getSurfaceTexture(); |
| 111 | int textureName = mHTML5VideoView.getTextureName(); |
| 112 | |
| 113 | int currentVideoLayerId = mHTML5VideoView.getVideoLayerId(); |
| 114 | if (layer != 0 && surfTexture != null && currentVideoLayerId != -1) { |
Teng-Hui Zhu | 265db32 | 2011-03-18 14:56:10 -0700 | [diff] [blame] | 115 | int playerState = mHTML5VideoView.getCurrentState(); |
Teng-Hui Zhu | f4d4e9e | 2011-03-30 14:39:56 -0700 | [diff] [blame] | 116 | if (mHTML5VideoView.getPlayerBuffering()) |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 117 | playerState = HTML5VideoView.STATE_NOTPREPARED; |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 118 | boolean foundInTree = nativeSendSurfaceTexture(surfTexture, |
| 119 | layer, currentVideoLayerId, textureName, |
Teng-Hui Zhu | 265db32 | 2011-03-18 14:56:10 -0700 | [diff] [blame] | 120 | playerState); |
Teng-Hui Zhu | 1e26d82 | 2011-03-23 16:54:20 -0700 | [diff] [blame] | 121 | if (playerState >= HTML5VideoView.STATE_PREPARED |
Teng-Hui Zhu | 265db32 | 2011-03-18 14:56:10 -0700 | [diff] [blame] | 122 | && !foundInTree) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 123 | mHTML5VideoView.pauseAndDispatch(mCurrentProxy); |
| 124 | mHTML5VideoView.deleteSurfaceTexture(); |
Andrei Popescu | 31d2aa1 | 2010-04-08 15:19:22 +0100 | [diff] [blame] | 125 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 126 | } |
Andrei Popescu | a41f97b | 2010-01-11 18:36:25 +0000 | [diff] [blame] | 127 | } |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 128 | } |
Andrei Popescu | a41f97b | 2010-01-11 18:36:25 +0000 | [diff] [blame] | 129 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 130 | // When a WebView is paused, we also want to pause the video in it. |
| 131 | public static void pauseAndDispatch() { |
| 132 | if (mHTML5VideoView != null) { |
| 133 | mHTML5VideoView.pauseAndDispatch(mCurrentProxy); |
| 134 | // When switching out, clean the video content on the old page |
| 135 | // by telling the layer not readyToUseSurfTex. |
| 136 | setBaseLayer(mBaseLayer); |
| 137 | } |
| 138 | } |
| 139 | |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 140 | public static void enterFullScreenVideo(int layerId, String url, |
| 141 | HTML5VideoViewProxy proxy, WebView webView) { |
| 142 | // Save the inline video info and inherit it in the full screen |
| 143 | int savePosition = 0; |
| 144 | boolean savedIsPlaying = false; |
| 145 | if (mHTML5VideoView != null) { |
| 146 | // If we are playing the same video, then it is better to |
| 147 | // save the current position. |
| 148 | if (layerId == mHTML5VideoView.getVideoLayerId()) { |
| 149 | savePosition = mHTML5VideoView.getCurrentPosition(); |
| 150 | savedIsPlaying = mHTML5VideoView.isPlaying(); |
| 151 | } |
| 152 | mHTML5VideoView.pauseAndDispatch(mCurrentProxy); |
| 153 | mHTML5VideoView.release(); |
| 154 | } |
| 155 | mHTML5VideoView = new HTML5VideoFullScreen(proxy.getContext(), |
| 156 | layerId, savePosition, savedIsPlaying); |
| 157 | mCurrentProxy = proxy; |
| 158 | |
| 159 | mHTML5VideoView.setVideoURI(url, mCurrentProxy); |
| 160 | |
| 161 | mHTML5VideoView.enterFullScreenVideoState(layerId, proxy, webView); |
| 162 | } |
| 163 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 164 | // This is on the UI thread. |
| 165 | // When native tell Java to play, we need to check whether or not it is |
| 166 | // still the same video by using videoLayerId and treat it differently. |
| 167 | public static void play(String url, int time, HTML5VideoViewProxy proxy, |
| 168 | WebChromeClient client, int videoLayerId) { |
| 169 | int currentVideoLayerId = -1; |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 170 | boolean backFromFullScreenMode = false; |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 171 | if (mHTML5VideoView != null) { |
| 172 | currentVideoLayerId = mHTML5VideoView.getVideoLayerId(); |
| 173 | if (mHTML5VideoView instanceof HTML5VideoFullScreen) { |
| 174 | backFromFullScreenMode = true; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (backFromFullScreenMode |
| 179 | || currentVideoLayerId != videoLayerId |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 180 | || mHTML5VideoView.getSurfaceTexture() == null) { |
| 181 | // Here, we handle the case when switching to a new video, |
| 182 | // either inside a WebView or across WebViews |
| 183 | // For switching videos within a WebView or across the WebView, |
| 184 | // we need to pause the old one and re-create a new media player |
| 185 | // inside the HTML5VideoView. |
| 186 | if (mHTML5VideoView != null) { |
Teng-Hui Zhu | 22954d4 | 2011-04-07 17:13:18 -0700 | [diff] [blame^] | 187 | if (!backFromFullScreenMode) { |
| 188 | mHTML5VideoView.pauseAndDispatch(mCurrentProxy); |
| 189 | } |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 190 | // release the media player to avoid finalize error |
| 191 | mHTML5VideoView.release(); |
| 192 | } |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 193 | mCurrentProxy = proxy; |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 194 | mHTML5VideoView = new HTML5VideoInline(videoLayerId, time, false); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 195 | |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 196 | mHTML5VideoView.setVideoURI(url, mCurrentProxy); |
| 197 | mHTML5VideoView.prepareDataAndDisplayMode(proxy); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 198 | } else if (mCurrentProxy == proxy) { |
| 199 | // Here, we handle the case when we keep playing with one video |
| 200 | if (!mHTML5VideoView.isPlaying()) { |
| 201 | mHTML5VideoView.seekTo(time); |
| 202 | mHTML5VideoView.start(); |
| 203 | } |
| 204 | } else if (mCurrentProxy != null) { |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 205 | // Some other video is already playing. Notify the caller that |
| 206 | // its playback ended. |
Andrei Popescu | c4fbceb | 2010-04-14 13:30:23 +0100 | [diff] [blame] | 207 | proxy.dispatchOnEnded(); |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 208 | } |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 209 | } |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 210 | |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 211 | public static boolean isPlaying(HTML5VideoViewProxy proxy) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 212 | return (mCurrentProxy == proxy && mHTML5VideoView != null |
| 213 | && mHTML5VideoView.isPlaying()); |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | public static int getCurrentPosition() { |
| 217 | int currentPosMs = 0; |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 218 | if (mHTML5VideoView != null) { |
| 219 | currentPosMs = mHTML5VideoView.getCurrentPosition(); |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 220 | } |
| 221 | return currentPosMs; |
| 222 | } |
| 223 | |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 224 | public static void seek(int time, HTML5VideoViewProxy proxy) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 225 | if (mCurrentProxy == proxy && time >= 0 && mHTML5VideoView != null) { |
| 226 | mHTML5VideoView.seekTo(time); |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | public static void pause(HTML5VideoViewProxy proxy) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 231 | if (mCurrentProxy == proxy && mHTML5VideoView != null) { |
| 232 | mHTML5VideoView.pause(); |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 233 | } |
| 234 | } |
Andrei Popescu | bf385d7 | 2009-09-18 18:59:52 +0100 | [diff] [blame] | 235 | |
| 236 | public static void onPrepared() { |
Teng-Hui Zhu | 0504967 | 2011-04-06 18:09:27 -0700 | [diff] [blame] | 237 | if (!mHTML5VideoView.isFullScreenMode() || mHTML5VideoView.getAutostart()) { |
| 238 | mHTML5VideoView.start(); |
| 239 | } |
Teng-Hui Zhu | 265db32 | 2011-03-18 14:56:10 -0700 | [diff] [blame] | 240 | if (mBaseLayer != 0) { |
| 241 | setBaseLayer(mBaseLayer); |
| 242 | } |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | public static void end() { |
| 246 | if (mCurrentProxy != null) { |
| 247 | if (isVideoSelfEnded) |
| 248 | mCurrentProxy.dispatchOnEnded(); |
| 249 | else |
| 250 | mCurrentProxy.dispatchOnPaused(); |
| 251 | } |
| 252 | isVideoSelfEnded = false; |
Andrei Popescu | bf385d7 | 2009-09-18 18:59:52 +0100 | [diff] [blame] | 253 | } |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // A bunch event listeners for our VideoView |
| 257 | // MediaPlayer.OnPreparedListener |
| 258 | public void onPrepared(MediaPlayer mp) { |
Andrei Popescu | bf385d7 | 2009-09-18 18:59:52 +0100 | [diff] [blame] | 259 | VideoPlayer.onPrepared(); |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 260 | Message msg = Message.obtain(mWebCoreHandler, PREPARED); |
| 261 | Map<String, Object> map = new HashMap<String, Object>(); |
| 262 | map.put("dur", new Integer(mp.getDuration())); |
| 263 | map.put("width", new Integer(mp.getVideoWidth())); |
| 264 | map.put("height", new Integer(mp.getVideoHeight())); |
| 265 | msg.obj = map; |
| 266 | mWebCoreHandler.sendMessage(msg); |
| 267 | } |
| 268 | |
| 269 | // MediaPlayer.OnCompletionListener; |
| 270 | public void onCompletion(MediaPlayer mp) { |
Andrei Popescu | c4fbceb | 2010-04-14 13:30:23 +0100 | [diff] [blame] | 271 | // The video ended by itself, so we need to |
| 272 | // send a message to the UI thread to dismiss |
| 273 | // the video view and to return to the WebView. |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 274 | // arg1 == 1 means the video ends by itself. |
| 275 | sendMessage(obtainMessage(ENDED, 1, 0)); |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 278 | // MediaPlayer.OnErrorListener |
| 279 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 280 | sendMessage(obtainMessage(ERROR)); |
| 281 | return false; |
| 282 | } |
| 283 | |
Andrei Popescu | c4fbceb | 2010-04-14 13:30:23 +0100 | [diff] [blame] | 284 | public void dispatchOnEnded() { |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 285 | Message msg = Message.obtain(mWebCoreHandler, ENDED); |
| 286 | mWebCoreHandler.sendMessage(msg); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 287 | } |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 288 | |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 289 | public void dispatchOnPaused() { |
| 290 | Message msg = Message.obtain(mWebCoreHandler, PAUSED); |
| 291 | mWebCoreHandler.sendMessage(msg); |
| 292 | } |
| 293 | |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 294 | public void onTimeupdate() { |
| 295 | sendMessage(obtainMessage(TIMEUPDATE)); |
| 296 | } |
| 297 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 298 | // When there is a frame ready from surface texture, we should tell WebView |
| 299 | // to refresh. |
| 300 | @Override |
| 301 | public void onFrameAvailable(SurfaceTexture surfaceTexture) { |
| 302 | // TODO: This should support partial invalidation too. |
| 303 | mWebView.invalidate(); |
| 304 | } |
| 305 | |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 306 | // Handler for the messages from WebCore or Timer thread to the UI thread. |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 307 | @Override |
| 308 | public void handleMessage(Message msg) { |
| 309 | // This executes on the UI thread. |
| 310 | switch (msg.what) { |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 311 | case PLAY: { |
| 312 | String url = (String) msg.obj; |
| 313 | WebChromeClient client = mWebView.getWebChromeClient(); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 314 | int videoLayerID = msg.arg1; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 315 | if (client != null) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 316 | VideoPlayer.play(url, mSeekPosition, this, client, videoLayerID); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 317 | } |
| 318 | break; |
| 319 | } |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 320 | case SEEK: { |
| 321 | Integer time = (Integer) msg.obj; |
| 322 | mSeekPosition = time; |
| 323 | VideoPlayer.seek(mSeekPosition, this); |
| 324 | break; |
| 325 | } |
| 326 | case PAUSE: { |
| 327 | VideoPlayer.pause(this); |
| 328 | break; |
| 329 | } |
Andrei Popescu | 46a83b4 | 2009-10-23 13:49:46 +0100 | [diff] [blame] | 330 | case ENDED: |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 331 | if (msg.arg1 == 1) |
| 332 | VideoPlayer.isVideoSelfEnded = true; |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 333 | VideoPlayer.end(); |
| 334 | break; |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 335 | case ERROR: { |
| 336 | WebChromeClient client = mWebView.getWebChromeClient(); |
| 337 | if (client != null) { |
| 338 | client.onHideCustomView(); |
| 339 | } |
| 340 | break; |
| 341 | } |
| 342 | case LOAD_DEFAULT_POSTER: { |
| 343 | WebChromeClient client = mWebView.getWebChromeClient(); |
| 344 | if (client != null) { |
| 345 | doSetPoster(client.getDefaultVideoPoster()); |
| 346 | } |
| 347 | break; |
| 348 | } |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 349 | case TIMEUPDATE: { |
| 350 | if (VideoPlayer.isPlaying(this)) { |
| 351 | sendTimeupdate(); |
| 352 | } |
| 353 | break; |
| 354 | } |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 355 | case BUFFERING_START: { |
Teng-Hui Zhu | f4d4e9e | 2011-03-30 14:39:56 -0700 | [diff] [blame] | 356 | VideoPlayer.setPlayerBuffering(true); |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 357 | break; |
| 358 | } |
| 359 | case BUFFERING_END: { |
Teng-Hui Zhu | f4d4e9e | 2011-03-30 14:39:56 -0700 | [diff] [blame] | 360 | VideoPlayer.setPlayerBuffering(false); |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 361 | break; |
| 362 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 366 | // Everything below this comment executes on the WebCore thread, except for |
| 367 | // the EventHandler methods, which are called on the network thread. |
| 368 | |
| 369 | // A helper class that knows how to download posters |
| 370 | private static final class PosterDownloader implements EventHandler { |
| 371 | // The request queue. This is static as we have one queue for all posters. |
| 372 | private static RequestQueue mRequestQueue; |
| 373 | private static int mQueueRefCount = 0; |
| 374 | // The poster URL |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 375 | private URL mUrl; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 376 | // The proxy we're doing this for. |
| 377 | private final HTML5VideoViewProxy mProxy; |
| 378 | // The poster bytes. We only touch this on the network thread. |
| 379 | private ByteArrayOutputStream mPosterBytes; |
| 380 | // The request handle. We only touch this on the WebCore thread. |
| 381 | private RequestHandle mRequestHandle; |
| 382 | // The response status code. |
| 383 | private int mStatusCode; |
| 384 | // The response headers. |
| 385 | private Headers mHeaders; |
| 386 | // The handler to handle messages on the WebCore thread. |
| 387 | private Handler mHandler; |
| 388 | |
| 389 | public PosterDownloader(String url, HTML5VideoViewProxy proxy) { |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 390 | try { |
| 391 | mUrl = new URL(url); |
| 392 | } catch (MalformedURLException e) { |
| 393 | mUrl = null; |
| 394 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 395 | mProxy = proxy; |
| 396 | mHandler = new Handler(); |
| 397 | } |
| 398 | // Start the download. Called on WebCore thread. |
| 399 | public void start() { |
| 400 | retainQueue(); |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 401 | |
| 402 | if (mUrl == null) { |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | // Only support downloading posters over http/https. |
| 407 | // FIXME: Add support for other schemes. WebKit seems able to load |
| 408 | // posters over other schemes e.g. file://, but gets the dimensions wrong. |
| 409 | String protocol = mUrl.getProtocol(); |
| 410 | if ("http".equals(protocol) || "https".equals(protocol)) { |
| 411 | mRequestHandle = mRequestQueue.queueRequest(mUrl.toString(), "GET", null, |
| 412 | this, null, 0); |
| 413 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 414 | } |
| 415 | // Cancel the download if active and release the queue. Called on WebCore thread. |
| 416 | public void cancelAndReleaseQueue() { |
| 417 | if (mRequestHandle != null) { |
| 418 | mRequestHandle.cancel(); |
| 419 | mRequestHandle = null; |
| 420 | } |
| 421 | releaseQueue(); |
| 422 | } |
| 423 | // EventHandler methods. Executed on the network thread. |
| 424 | public void status(int major_version, |
| 425 | int minor_version, |
| 426 | int code, |
| 427 | String reason_phrase) { |
| 428 | mStatusCode = code; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 431 | public void headers(Headers headers) { |
| 432 | mHeaders = headers; |
| 433 | } |
| 434 | |
| 435 | public void data(byte[] data, int len) { |
| 436 | if (mPosterBytes == null) { |
| 437 | mPosterBytes = new ByteArrayOutputStream(); |
| 438 | } |
| 439 | mPosterBytes.write(data, 0, len); |
| 440 | } |
| 441 | |
| 442 | public void endData() { |
| 443 | if (mStatusCode == 200) { |
| 444 | if (mPosterBytes.size() > 0) { |
| 445 | Bitmap poster = BitmapFactory.decodeByteArray( |
| 446 | mPosterBytes.toByteArray(), 0, mPosterBytes.size()); |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 447 | mProxy.doSetPoster(poster); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 448 | } |
| 449 | cleanup(); |
| 450 | } else if (mStatusCode >= 300 && mStatusCode < 400) { |
| 451 | // We have a redirect. |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 452 | try { |
| 453 | mUrl = new URL(mHeaders.getLocation()); |
| 454 | } catch (MalformedURLException e) { |
| 455 | mUrl = null; |
| 456 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 457 | if (mUrl != null) { |
| 458 | mHandler.post(new Runnable() { |
| 459 | public void run() { |
| 460 | if (mRequestHandle != null) { |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 461 | mRequestHandle.setupRedirect(mUrl.toString(), mStatusCode, |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 462 | new HashMap<String, String>()); |
| 463 | } |
| 464 | } |
| 465 | }); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | public void certificate(SslCertificate certificate) { |
| 471 | // Don't care. |
| 472 | } |
| 473 | |
| 474 | public void error(int id, String description) { |
| 475 | cleanup(); |
| 476 | } |
| 477 | |
| 478 | public boolean handleSslErrorRequest(SslError error) { |
| 479 | // Don't care. If this happens, data() will never be called so |
| 480 | // mPosterBytes will never be created, so no need to call cleanup. |
| 481 | return false; |
| 482 | } |
| 483 | // Tears down the poster bytes stream. Called on network thread. |
| 484 | private void cleanup() { |
| 485 | if (mPosterBytes != null) { |
| 486 | try { |
| 487 | mPosterBytes.close(); |
| 488 | } catch (IOException ignored) { |
| 489 | // Ignored. |
| 490 | } finally { |
| 491 | mPosterBytes = null; |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // Queue management methods. Called on WebCore thread. |
| 497 | private void retainQueue() { |
| 498 | if (mRequestQueue == null) { |
| 499 | mRequestQueue = new RequestQueue(mProxy.getContext()); |
| 500 | } |
| 501 | mQueueRefCount++; |
| 502 | } |
| 503 | |
| 504 | private void releaseQueue() { |
| 505 | if (mQueueRefCount == 0) { |
| 506 | return; |
| 507 | } |
| 508 | if (--mQueueRefCount == 0) { |
| 509 | mRequestQueue.shutdown(); |
| 510 | mRequestQueue = null; |
| 511 | } |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 512 | } |
| 513 | } |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 514 | |
| 515 | /** |
| 516 | * Private constructor. |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 517 | * @param webView is the WebView that hosts the video. |
| 518 | * @param nativePtr is the C++ pointer to the MediaPlayerPrivate object. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 519 | */ |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 520 | private HTML5VideoViewProxy(WebView webView, int nativePtr) { |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 521 | // This handler is for the main (UI) thread. |
| 522 | super(Looper.getMainLooper()); |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 523 | // Save the WebView object. |
| 524 | mWebView = webView; |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 525 | // Pass Proxy into webview, such that every time we have a setBaseLayer |
| 526 | // call, we tell this Proxy to call the native to update the layer tree |
| 527 | // for the Video Layer's surface texture info |
| 528 | mWebView.setHTML5VideoViewProxy(this); |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 529 | // Save the native ptr |
| 530 | mNativePointer = nativePtr; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 531 | // create the message handler for this thread |
| 532 | createWebCoreHandler(); |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 533 | } |
| 534 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 535 | private void createWebCoreHandler() { |
| 536 | mWebCoreHandler = new Handler() { |
| 537 | @Override |
| 538 | public void handleMessage(Message msg) { |
| 539 | switch (msg.what) { |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 540 | case PREPARED: { |
| 541 | Map<String, Object> map = (Map<String, Object>) msg.obj; |
| 542 | Integer duration = (Integer) map.get("dur"); |
| 543 | Integer width = (Integer) map.get("width"); |
| 544 | Integer height = (Integer) map.get("height"); |
| 545 | nativeOnPrepared(duration.intValue(), width.intValue(), |
| 546 | height.intValue(), mNativePointer); |
| 547 | break; |
| 548 | } |
| 549 | case ENDED: |
Ben Murdoch | ff19d19 | 2011-01-17 18:08:56 +0000 | [diff] [blame] | 550 | mSeekPosition = 0; |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 551 | nativeOnEnded(mNativePointer); |
| 552 | break; |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 553 | case PAUSED: |
| 554 | nativeOnPaused(mNativePointer); |
| 555 | break; |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 556 | case POSTER_FETCHED: |
| 557 | Bitmap poster = (Bitmap) msg.obj; |
| 558 | nativeOnPosterFetched(poster, mNativePointer); |
| 559 | break; |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 560 | case TIMEUPDATE: |
| 561 | nativeOnTimeupdate(msg.arg1, mNativePointer); |
| 562 | break; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 563 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 564 | } |
| 565 | }; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 566 | } |
| 567 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 568 | private void doSetPoster(Bitmap poster) { |
| 569 | if (poster == null) { |
| 570 | return; |
| 571 | } |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 572 | // Save a ref to the bitmap and send it over to the WebCore thread. |
| 573 | mPoster = poster; |
| 574 | Message msg = Message.obtain(mWebCoreHandler, POSTER_FETCHED); |
| 575 | msg.obj = poster; |
| 576 | mWebCoreHandler.sendMessage(msg); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 577 | } |
| 578 | |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 579 | private void sendTimeupdate() { |
| 580 | Message msg = Message.obtain(mWebCoreHandler, TIMEUPDATE); |
| 581 | msg.arg1 = VideoPlayer.getCurrentPosition(); |
| 582 | mWebCoreHandler.sendMessage(msg); |
| 583 | } |
| 584 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 585 | public Context getContext() { |
| 586 | return mWebView.getContext(); |
| 587 | } |
| 588 | |
| 589 | // The public methods below are all called from WebKit only. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 590 | /** |
| 591 | * Play a video stream. |
| 592 | * @param url is the URL of the video stream. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 593 | */ |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 594 | public void play(String url, int position, int videoLayerID) { |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 595 | if (url == null) { |
| 596 | return; |
| 597 | } |
Ben Murdoch | ff19d19 | 2011-01-17 18:08:56 +0000 | [diff] [blame] | 598 | |
| 599 | if (position > 0) { |
| 600 | seek(position); |
| 601 | } |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 602 | Message message = obtainMessage(PLAY); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 603 | message.arg1 = videoLayerID; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 604 | message.obj = url; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 605 | sendMessage(message); |
| 606 | } |
| 607 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 608 | /** |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 609 | * Seek into the video stream. |
| 610 | * @param time is the position in the video stream. |
| 611 | */ |
| 612 | public void seek(int time) { |
| 613 | Message message = obtainMessage(SEEK); |
| 614 | message.obj = new Integer(time); |
| 615 | sendMessage(message); |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Pause the playback. |
| 620 | */ |
| 621 | public void pause() { |
| 622 | Message message = obtainMessage(PAUSE); |
| 623 | sendMessage(message); |
| 624 | } |
| 625 | |
| 626 | /** |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 627 | * Tear down this proxy object. |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 628 | */ |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 629 | public void teardown() { |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 630 | // This is called by the C++ MediaPlayerPrivate dtor. |
| 631 | // Cancel any active poster download. |
| 632 | if (mPosterDownloader != null) { |
| 633 | mPosterDownloader.cancelAndReleaseQueue(); |
| 634 | } |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 635 | mNativePointer = 0; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Load the poster image. |
| 640 | * @param url is the URL of the poster image. |
| 641 | */ |
| 642 | public void loadPoster(String url) { |
| 643 | if (url == null) { |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 644 | Message message = obtainMessage(LOAD_DEFAULT_POSTER); |
| 645 | sendMessage(message); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | // Cancel any active poster download. |
| 649 | if (mPosterDownloader != null) { |
| 650 | mPosterDownloader.cancelAndReleaseQueue(); |
| 651 | } |
| 652 | // Load the poster asynchronously |
| 653 | mPosterDownloader = new PosterDownloader(url, this); |
| 654 | mPosterDownloader.start(); |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame] | 655 | } |
| 656 | |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 657 | // These three function are called from UI thread only by WebView. |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 658 | public void setBaseLayer(int layer) { |
| 659 | VideoPlayer.setBaseLayer(layer); |
| 660 | } |
| 661 | |
| 662 | public void pauseAndDispatch() { |
| 663 | VideoPlayer.pauseAndDispatch(); |
| 664 | } |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 665 | |
| 666 | public void enterFullScreenVideo(int layerId, String url) { |
| 667 | VideoPlayer.enterFullScreenVideo(layerId, url, this, mWebView); |
| 668 | } |
| 669 | |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 670 | /** |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 671 | * The factory for HTML5VideoViewProxy instances. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 672 | * @param webViewCore is the WebViewCore that is requesting the proxy. |
| 673 | * |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 674 | * @return a new HTML5VideoViewProxy object. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 675 | */ |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 676 | public static HTML5VideoViewProxy getInstance(WebViewCore webViewCore, int nativePtr) { |
| 677 | return new HTML5VideoViewProxy(webViewCore.getWebView(), nativePtr); |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 678 | } |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 679 | |
Ben Murdoch | 1708ad5 | 2010-11-11 15:56:16 +0000 | [diff] [blame] | 680 | /* package */ WebView getWebView() { |
| 681 | return mWebView; |
| 682 | } |
| 683 | |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 684 | private native void nativeOnPrepared(int duration, int width, int height, int nativePointer); |
| 685 | private native void nativeOnEnded(int nativePointer); |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 686 | private native void nativeOnPaused(int nativePointer); |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 687 | private native void nativeOnPosterFetched(Bitmap poster, int nativePointer); |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 688 | private native void nativeOnTimeupdate(int position, int nativePointer); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 689 | private native static boolean nativeSendSurfaceTexture(SurfaceTexture texture, |
| 690 | int baseLayer, int videoLayerId, int textureName, |
Teng-Hui Zhu | 265db32 | 2011-03-18 14:56:10 -0700 | [diff] [blame] | 691 | int playerState); |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 692 | |
| 693 | @Override |
| 694 | public boolean onInfo(MediaPlayer mp, int what, int extra) { |
| 695 | if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) { |
| 696 | sendMessage(obtainMessage(BUFFERING_START, what, extra)); |
| 697 | } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) { |
| 698 | sendMessage(obtainMessage(BUFFERING_END, what, extra)); |
| 699 | } |
| 700 | return false; |
| 701 | } |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 702 | } |