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