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; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 22 | import android.media.MediaPlayer; |
| 23 | import android.media.MediaPlayer.OnPreparedListener; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 24 | import android.media.MediaPlayer.OnCompletionListener; |
| 25 | import android.media.MediaPlayer.OnErrorListener; |
| 26 | import android.net.http.EventHandler; |
| 27 | import android.net.http.Headers; |
| 28 | import android.net.http.RequestHandle; |
| 29 | import android.net.http.RequestQueue; |
| 30 | import android.net.http.SslCertificate; |
| 31 | import android.net.http.SslError; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 32 | import android.net.Uri; |
| 33 | import android.os.Bundle; |
| 34 | import android.os.Handler; |
| 35 | import android.os.Looper; |
| 36 | import android.os.Message; |
| 37 | import android.util.Log; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 38 | import android.view.MotionEvent; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 39 | import android.view.View; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 40 | import android.view.ViewGroup; |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame] | 41 | import android.webkit.ViewManager.ChildView; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 42 | import android.widget.AbsoluteLayout; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 43 | import android.widget.ImageView; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 44 | import android.widget.MediaController; |
| 45 | import android.widget.VideoView; |
| 46 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 47 | import java.io.ByteArrayOutputStream; |
| 48 | import java.io.IOException; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 49 | import java.util.HashMap; |
| 50 | |
| 51 | /** |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 52 | * <p>Proxy for HTML5 video views. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 53 | */ |
| 54 | class HTML5VideoViewProxy extends Handler { |
| 55 | // Logging tag. |
| 56 | private static final String LOGTAG = "HTML5VideoViewProxy"; |
| 57 | |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 58 | // Message Ids for WebCore thread -> UI thread communication. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 59 | private static final int INIT = 100; |
| 60 | private static final int PLAY = 101; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 61 | private static final int SET_POSTER = 102; |
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 |
| 64 | |
| 65 | // The handler for WebCore thread messages; |
| 66 | private Handler mWebCoreHandler; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 67 | // The WebView instance that created this view. |
| 68 | private WebView mWebView; |
| 69 | // The ChildView instance used by the ViewManager. |
| 70 | private ChildView mChildView; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 71 | // The poster image to be shown when the video is not playing. |
| 72 | private ImageView mPosterView; |
| 73 | // The poster downloader. |
| 74 | private PosterDownloader mPosterDownloader; |
| 75 | // A helper class to control the playback. This executes on the UI thread! |
| 76 | private static final class VideoPlayer { |
| 77 | // The proxy that is currently playing (if any). |
| 78 | private static HTML5VideoViewProxy mCurrentProxy; |
| 79 | // The VideoView instance. This is a singleton for now, at least until |
| 80 | // http://b/issue?id=1973663 is fixed. |
| 81 | private static VideoView mVideoView; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 82 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 83 | private static final WebChromeClient.CustomViewCallback mCallback = |
| 84 | new WebChromeClient.CustomViewCallback() { |
| 85 | public void onCustomViewHidden() { |
| 86 | // At this point the videoview is pretty much destroyed. |
| 87 | // It listens to SurfaceHolder.Callback.SurfaceDestroyed event |
| 88 | // which happens when the video view is detached from its parent |
| 89 | // view. This happens in the WebChromeClient before this method |
| 90 | // is invoked. |
| 91 | mCurrentProxy.playbackEnded(); |
| 92 | mCurrentProxy = null; |
| 93 | mVideoView = null; |
| 94 | } |
| 95 | }; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 96 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 97 | public static void play(String url, HTML5VideoViewProxy proxy, WebChromeClient client) { |
| 98 | if (mCurrentProxy != null) { |
| 99 | // Some other video is already playing. Notify the caller that its playback ended. |
| 100 | proxy.playbackEnded(); |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 101 | return; |
| 102 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 103 | mCurrentProxy = proxy; |
| 104 | mVideoView = new VideoView(proxy.getContext()); |
| 105 | mVideoView.setWillNotDraw(false); |
| 106 | mVideoView.setMediaController(new MediaController(proxy.getContext())); |
| 107 | mVideoView.setVideoURI(Uri.parse(url)); |
| 108 | mVideoView.start(); |
| 109 | client.onShowCustomView(mVideoView, mCallback); |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 110 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 111 | } |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 112 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 113 | // Handler for the messages from WebCore thread to the UI thread. |
| 114 | @Override |
| 115 | public void handleMessage(Message msg) { |
| 116 | // This executes on the UI thread. |
| 117 | switch (msg.what) { |
| 118 | case INIT: { |
| 119 | mPosterView = new ImageView(mWebView.getContext()); |
| 120 | mChildView.mView = mPosterView; |
| 121 | break; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 122 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 123 | case PLAY: { |
| 124 | String url = (String) msg.obj; |
| 125 | WebChromeClient client = mWebView.getWebChromeClient(); |
| 126 | if (client != null) { |
| 127 | VideoPlayer.play(url, this, client); |
| 128 | } |
| 129 | break; |
| 130 | } |
| 131 | case SET_POSTER: { |
| 132 | Bitmap poster = (Bitmap) msg.obj; |
| 133 | mPosterView.setImageBitmap(poster); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public void playbackEnded() { |
| 140 | // TODO: notify WebKit |
| 141 | } |
| 142 | |
| 143 | // Everything below this comment executes on the WebCore thread, except for |
| 144 | // the EventHandler methods, which are called on the network thread. |
| 145 | |
| 146 | // A helper class that knows how to download posters |
| 147 | private static final class PosterDownloader implements EventHandler { |
| 148 | // The request queue. This is static as we have one queue for all posters. |
| 149 | private static RequestQueue mRequestQueue; |
| 150 | private static int mQueueRefCount = 0; |
| 151 | // The poster URL |
| 152 | private String mUrl; |
| 153 | // The proxy we're doing this for. |
| 154 | private final HTML5VideoViewProxy mProxy; |
| 155 | // The poster bytes. We only touch this on the network thread. |
| 156 | private ByteArrayOutputStream mPosterBytes; |
| 157 | // The request handle. We only touch this on the WebCore thread. |
| 158 | private RequestHandle mRequestHandle; |
| 159 | // The response status code. |
| 160 | private int mStatusCode; |
| 161 | // The response headers. |
| 162 | private Headers mHeaders; |
| 163 | // The handler to handle messages on the WebCore thread. |
| 164 | private Handler mHandler; |
| 165 | |
| 166 | public PosterDownloader(String url, HTML5VideoViewProxy proxy) { |
| 167 | mUrl = url; |
| 168 | mProxy = proxy; |
| 169 | mHandler = new Handler(); |
| 170 | } |
| 171 | // Start the download. Called on WebCore thread. |
| 172 | public void start() { |
| 173 | retainQueue(); |
| 174 | mRequestHandle = mRequestQueue.queueRequest(mUrl, "GET", null, this, null, 0); |
| 175 | } |
| 176 | // Cancel the download if active and release the queue. Called on WebCore thread. |
| 177 | public void cancelAndReleaseQueue() { |
| 178 | if (mRequestHandle != null) { |
| 179 | mRequestHandle.cancel(); |
| 180 | mRequestHandle = null; |
| 181 | } |
| 182 | releaseQueue(); |
| 183 | } |
| 184 | // EventHandler methods. Executed on the network thread. |
| 185 | public void status(int major_version, |
| 186 | int minor_version, |
| 187 | int code, |
| 188 | String reason_phrase) { |
| 189 | mStatusCode = code; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 192 | public void headers(Headers headers) { |
| 193 | mHeaders = headers; |
| 194 | } |
| 195 | |
| 196 | public void data(byte[] data, int len) { |
| 197 | if (mPosterBytes == null) { |
| 198 | mPosterBytes = new ByteArrayOutputStream(); |
| 199 | } |
| 200 | mPosterBytes.write(data, 0, len); |
| 201 | } |
| 202 | |
| 203 | public void endData() { |
| 204 | if (mStatusCode == 200) { |
| 205 | if (mPosterBytes.size() > 0) { |
| 206 | Bitmap poster = BitmapFactory.decodeByteArray( |
| 207 | mPosterBytes.toByteArray(), 0, mPosterBytes.size()); |
| 208 | if (poster != null) { |
| 209 | mProxy.doSetPoster(poster); |
| 210 | } |
| 211 | } |
| 212 | cleanup(); |
| 213 | } else if (mStatusCode >= 300 && mStatusCode < 400) { |
| 214 | // We have a redirect. |
| 215 | mUrl = mHeaders.getLocation(); |
| 216 | if (mUrl != null) { |
| 217 | mHandler.post(new Runnable() { |
| 218 | public void run() { |
| 219 | if (mRequestHandle != null) { |
| 220 | mRequestHandle.setupRedirect(mUrl, mStatusCode, |
| 221 | new HashMap<String, String>()); |
| 222 | } |
| 223 | } |
| 224 | }); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | public void certificate(SslCertificate certificate) { |
| 230 | // Don't care. |
| 231 | } |
| 232 | |
| 233 | public void error(int id, String description) { |
| 234 | cleanup(); |
| 235 | } |
| 236 | |
| 237 | public boolean handleSslErrorRequest(SslError error) { |
| 238 | // Don't care. If this happens, data() will never be called so |
| 239 | // mPosterBytes will never be created, so no need to call cleanup. |
| 240 | return false; |
| 241 | } |
| 242 | // Tears down the poster bytes stream. Called on network thread. |
| 243 | private void cleanup() { |
| 244 | if (mPosterBytes != null) { |
| 245 | try { |
| 246 | mPosterBytes.close(); |
| 247 | } catch (IOException ignored) { |
| 248 | // Ignored. |
| 249 | } finally { |
| 250 | mPosterBytes = null; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // Queue management methods. Called on WebCore thread. |
| 256 | private void retainQueue() { |
| 257 | if (mRequestQueue == null) { |
| 258 | mRequestQueue = new RequestQueue(mProxy.getContext()); |
| 259 | } |
| 260 | mQueueRefCount++; |
| 261 | } |
| 262 | |
| 263 | private void releaseQueue() { |
| 264 | if (mQueueRefCount == 0) { |
| 265 | return; |
| 266 | } |
| 267 | if (--mQueueRefCount == 0) { |
| 268 | mRequestQueue.shutdown(); |
| 269 | mRequestQueue = null; |
| 270 | } |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 271 | } |
| 272 | } |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * Private constructor. |
| 276 | * @param context is the application context. |
| 277 | */ |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 278 | private HTML5VideoViewProxy(WebView webView) { |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 279 | // This handler is for the main (UI) thread. |
| 280 | super(Looper.getMainLooper()); |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 281 | // Save the WebView object. |
| 282 | mWebView = webView; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 283 | // create the message handler for this thread |
| 284 | createWebCoreHandler(); |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 287 | private void createWebCoreHandler() { |
| 288 | mWebCoreHandler = new Handler() { |
| 289 | @Override |
| 290 | public void handleMessage(Message msg) { |
| 291 | switch (msg.what) { |
| 292 | // TODO here we will process the messages from the VideoPlayer |
| 293 | // and will call native WebKit methods. |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 294 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 295 | } |
| 296 | }; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 299 | private void doSetPoster(Bitmap poster) { |
| 300 | if (poster == null) { |
| 301 | return; |
| 302 | } |
| 303 | // Send the bitmap over to the UI thread. |
| 304 | Message message = obtainMessage(SET_POSTER); |
| 305 | message.obj = poster; |
| 306 | sendMessage(message); |
| 307 | } |
| 308 | |
| 309 | public Context getContext() { |
| 310 | return mWebView.getContext(); |
| 311 | } |
| 312 | |
| 313 | // The public methods below are all called from WebKit only. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 314 | /** |
| 315 | * Play a video stream. |
| 316 | * @param url is the URL of the video stream. |
| 317 | * @param webview is the WebViewCore that is requesting the playback. |
| 318 | */ |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 319 | public void play(String url) { |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 320 | if (url == null) { |
| 321 | return; |
| 322 | } |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 323 | // We need to know the webview that is requesting the playback. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 324 | Message message = obtainMessage(PLAY); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 325 | message.obj = url; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 326 | sendMessage(message); |
| 327 | } |
| 328 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 329 | /** |
| 330 | * Create the child view that will cary the poster. |
| 331 | */ |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 332 | public void createView() { |
| 333 | mChildView = mWebView.mViewManager.createView(); |
| 334 | sendMessage(obtainMessage(INIT)); |
| 335 | } |
| 336 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 337 | /** |
| 338 | * Attach the poster view. |
| 339 | * @param x, y are the screen coordinates where the poster should be hung. |
| 340 | * @param width, height denote the size of the poster. |
| 341 | */ |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 342 | public void attachView(int x, int y, int width, int height) { |
| 343 | if (mChildView == null) { |
| 344 | return; |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame] | 345 | } |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 346 | mChildView.attachView(x, y, width, height); |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame] | 347 | } |
| 348 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 349 | /** |
| 350 | * Remove the child view and, thus, the poster. |
| 351 | */ |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 352 | public void removeView() { |
| 353 | if (mChildView == null) { |
| 354 | return; |
| 355 | } |
| 356 | mChildView.removeView(); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame^] | 357 | // This is called by the C++ MediaPlayerPrivate dtor. |
| 358 | // Cancel any active poster download. |
| 359 | if (mPosterDownloader != null) { |
| 360 | mPosterDownloader.cancelAndReleaseQueue(); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Load the poster image. |
| 366 | * @param url is the URL of the poster image. |
| 367 | */ |
| 368 | public void loadPoster(String url) { |
| 369 | if (url == null) { |
| 370 | return; |
| 371 | } |
| 372 | // Cancel any active poster download. |
| 373 | if (mPosterDownloader != null) { |
| 374 | mPosterDownloader.cancelAndReleaseQueue(); |
| 375 | } |
| 376 | // Load the poster asynchronously |
| 377 | mPosterDownloader = new PosterDownloader(url, this); |
| 378 | mPosterDownloader.start(); |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame] | 379 | } |
| 380 | |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 381 | /** |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 382 | * The factory for HTML5VideoViewProxy instances. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 383 | * @param webViewCore is the WebViewCore that is requesting the proxy. |
| 384 | * |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 385 | * @return a new HTML5VideoViewProxy object. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 386 | */ |
| 387 | public static HTML5VideoViewProxy getInstance(WebViewCore webViewCore) { |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 388 | return new HTML5VideoViewProxy(webViewCore.getWebView()); |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 389 | } |
| 390 | } |