blob: 7d8669bffe7f7562d20774b8a2334e51de91cbb3 [file] [log] [blame]
Andrei Popescu6fa29582009-06-19 14:54:09 +01001/*
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
17package android.webkit;
18
19import android.content.Context;
Andrei Popescu64b86a12009-09-15 20:34:18 +010020import android.graphics.Bitmap;
21import android.graphics.BitmapFactory;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -080022import android.graphics.SurfaceTexture;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +010023import android.media.MediaPlayer;
Andrei Popescu64b86a12009-09-15 20:34:18 +010024import android.net.http.EventHandler;
25import android.net.http.Headers;
26import android.net.http.RequestHandle;
27import android.net.http.RequestQueue;
28import android.net.http.SslCertificate;
29import android.net.http.SslError;
Andrei Popescu6fa29582009-06-19 14:54:09 +010030import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
33import android.util.Log;
Andrei Popescu6fa29582009-06-19 14:54:09 +010034
Andrei Popescu64b86a12009-09-15 20:34:18 +010035import java.io.ByteArrayOutputStream;
36import java.io.IOException;
Ben Murdoch42509792011-02-18 12:34:35 +000037import java.net.MalformedURLException;
38import java.net.URL;
Andrei Popescu6fa29582009-06-19 14:54:09 +010039import java.util.HashMap;
Andrei Popescu290c34a2009-09-17 15:55:24 +010040import java.util.Map;
Andrei Popescu6fa29582009-06-19 14:54:09 +010041
42/**
Andrei Popescu3c946a1a2009-07-03 08:20:53 +010043 * <p>Proxy for HTML5 video views.
Andrei Popescu6fa29582009-06-19 14:54:09 +010044 */
Andrei Popescu290c34a2009-09-17 15:55:24 +010045class HTML5VideoViewProxy extends Handler
46 implements MediaPlayer.OnPreparedListener,
Andrei Popescu50c86232009-09-30 16:51:25 +010047 MediaPlayer.OnCompletionListener,
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -080048 MediaPlayer.OnErrorListener,
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -070049 MediaPlayer.OnInfoListener,
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -080050 SurfaceTexture.OnFrameAvailableListener {
Andrei Popescu6fa29582009-06-19 14:54:09 +010051 // Logging tag.
52 private static final String LOGTAG = "HTML5VideoViewProxy";
53
Andrei Popescu3c946a1a2009-07-03 08:20:53 +010054 // Message Ids for WebCore thread -> UI thread communication.
Andrei Popescu50c86232009-09-30 16:51:25 +010055 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 Zhuc0fccd12011-03-29 10:35:11 -070060 private static final int BUFFERING_START = 105;
61 private static final int BUFFERING_END = 106;
Andrei Popescu6fa29582009-06-19 14:54:09 +010062
Andrei Popescu64b86a12009-09-15 20:34:18 +010063 // Message Ids to be handled on the WebCore thread
Andrei Popescu290c34a2009-09-17 15:55:24 +010064 private static final int PREPARED = 200;
65 private static final int ENDED = 201;
Andrei Popescu50c86232009-09-30 16:51:25 +010066 private static final int POSTER_FETCHED = 202;
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -070067 private static final int PAUSED = 203;
Teng-Hui Zhub109c882011-05-04 16:19:49 -070068 private static final int STOPFULLSCREEN = 204;
Andrei Popescu64b86a12009-09-15 20:34:18 +010069
Andrei Popescu048eb3b2010-01-11 21:12:54 +000070 // Timer thread -> UI thread
71 private static final int TIMEUPDATE = 300;
72
Andrei Popescu290c34a2009-09-17 15:55:24 +010073 // The C++ MediaPlayerPrivateAndroid object.
74 int mNativePointer;
Andrei Popescu64b86a12009-09-15 20:34:18 +010075 // The handler for WebCore thread messages;
76 private Handler mWebCoreHandler;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +010077 // The WebView instance that created this view.
78 private WebView mWebView;
Andrei Popescu64b86a12009-09-15 20:34:18 +010079 // The poster image to be shown when the video is not playing.
Andrei Popescu50c86232009-09-30 16:51:25 +010080 // This ref prevents the bitmap from being GC'ed.
81 private Bitmap mPoster;
Andrei Popescu64b86a12009-09-15 20:34:18 +010082 // The poster downloader.
83 private PosterDownloader mPosterDownloader;
Andrei Popescu290c34a2009-09-17 15:55:24 +010084 // The seek position.
85 private int mSeekPosition;
Andrei Popescu64b86a12009-09-15 20:34:18 +010086 // 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 Zhu661e8b12011-03-02 15:09:34 -080092 private static HTML5VideoView mHTML5VideoView;
Andrei Popescu048eb3b2010-01-11 21:12:54 +000093
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -080094 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 Popescu3c946a1a2009-07-03 08:20:53 +010098
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -070099 private static void setPlayerBuffering(boolean playerBuffering) {
100 mHTML5VideoView.setPlayerBuffering(playerBuffering);
101 }
102
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800103 // 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 Zhu10ab6542011-03-16 16:42:32 -0700107 // Don't do this for full screen mode.
108 if (mHTML5VideoView != null
109 && !mHTML5VideoView.isFullScreenMode()) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800110 mBaseLayer = layer;
111 SurfaceTexture surfTexture = mHTML5VideoView.getSurfaceTexture();
112 int textureName = mHTML5VideoView.getTextureName();
113
114 int currentVideoLayerId = mHTML5VideoView.getVideoLayerId();
115 if (layer != 0 && surfTexture != null && currentVideoLayerId != -1) {
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700116 int playerState = mHTML5VideoView.getCurrentState();
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700117 if (mHTML5VideoView.getPlayerBuffering())
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700118 playerState = HTML5VideoView.STATE_NOTPREPARED;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800119 boolean foundInTree = nativeSendSurfaceTexture(surfTexture,
120 layer, currentVideoLayerId, textureName,
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700121 playerState);
Teng-Hui Zhu1e26d822011-03-23 16:54:20 -0700122 if (playerState >= HTML5VideoView.STATE_PREPARED
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700123 && !foundInTree) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800124 mHTML5VideoView.pauseAndDispatch(mCurrentProxy);
125 mHTML5VideoView.deleteSurfaceTexture();
Andrei Popescu31d2aa12010-04-08 15:19:22 +0100126 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100127 }
Andrei Popescua41f97b2010-01-11 18:36:25 +0000128 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800129 }
Andrei Popescua41f97b2010-01-11 18:36:25 +0000130
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800131 // When a WebView is paused, we also want to pause the video in it.
132 public static void pauseAndDispatch() {
133 if (mHTML5VideoView != null) {
134 mHTML5VideoView.pauseAndDispatch(mCurrentProxy);
135 // When switching out, clean the video content on the old page
136 // by telling the layer not readyToUseSurfTex.
137 setBaseLayer(mBaseLayer);
138 }
139 }
140
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700141 public static void enterFullScreenVideo(int layerId, String url,
142 HTML5VideoViewProxy proxy, WebView webView) {
143 // Save the inline video info and inherit it in the full screen
144 int savePosition = 0;
145 boolean savedIsPlaying = false;
146 if (mHTML5VideoView != null) {
147 // If we are playing the same video, then it is better to
148 // save the current position.
149 if (layerId == mHTML5VideoView.getVideoLayerId()) {
150 savePosition = mHTML5VideoView.getCurrentPosition();
151 savedIsPlaying = mHTML5VideoView.isPlaying();
152 }
153 mHTML5VideoView.pauseAndDispatch(mCurrentProxy);
154 mHTML5VideoView.release();
155 }
156 mHTML5VideoView = new HTML5VideoFullScreen(proxy.getContext(),
157 layerId, savePosition, savedIsPlaying);
158 mCurrentProxy = proxy;
159
160 mHTML5VideoView.setVideoURI(url, mCurrentProxy);
161
162 mHTML5VideoView.enterFullScreenVideoState(layerId, proxy, webView);
163 }
164
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800165 // This is on the UI thread.
166 // When native tell Java to play, we need to check whether or not it is
167 // still the same video by using videoLayerId and treat it differently.
168 public static void play(String url, int time, HTML5VideoViewProxy proxy,
169 WebChromeClient client, int videoLayerId) {
170 int currentVideoLayerId = -1;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700171 boolean backFromFullScreenMode = false;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700172 if (mHTML5VideoView != null) {
173 currentVideoLayerId = mHTML5VideoView.getVideoLayerId();
174 if (mHTML5VideoView instanceof HTML5VideoFullScreen) {
175 backFromFullScreenMode = true;
176 }
177 }
178
179 if (backFromFullScreenMode
180 || currentVideoLayerId != videoLayerId
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800181 || mHTML5VideoView.getSurfaceTexture() == null) {
182 // Here, we handle the case when switching to a new video,
183 // either inside a WebView or across WebViews
184 // For switching videos within a WebView or across the WebView,
185 // we need to pause the old one and re-create a new media player
186 // inside the HTML5VideoView.
187 if (mHTML5VideoView != null) {
Teng-Hui Zhu22954d42011-04-07 17:13:18 -0700188 if (!backFromFullScreenMode) {
189 mHTML5VideoView.pauseAndDispatch(mCurrentProxy);
190 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800191 // release the media player to avoid finalize error
192 mHTML5VideoView.release();
193 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800194 mCurrentProxy = proxy;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700195 mHTML5VideoView = new HTML5VideoInline(videoLayerId, time, false);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800196
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700197 mHTML5VideoView.setVideoURI(url, mCurrentProxy);
198 mHTML5VideoView.prepareDataAndDisplayMode(proxy);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800199 } else if (mCurrentProxy == proxy) {
200 // Here, we handle the case when we keep playing with one video
201 if (!mHTML5VideoView.isPlaying()) {
202 mHTML5VideoView.seekTo(time);
203 mHTML5VideoView.start();
204 }
205 } else if (mCurrentProxy != null) {
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700206 // Some other video is already playing. Notify the caller that
207 // its playback ended.
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100208 proxy.dispatchOnEnded();
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100209 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100210 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100211
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000212 public static boolean isPlaying(HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800213 return (mCurrentProxy == proxy && mHTML5VideoView != null
214 && mHTML5VideoView.isPlaying());
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000215 }
216
217 public static int getCurrentPosition() {
218 int currentPosMs = 0;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800219 if (mHTML5VideoView != null) {
220 currentPosMs = mHTML5VideoView.getCurrentPosition();
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000221 }
222 return currentPosMs;
223 }
224
Andrei Popescu290c34a2009-09-17 15:55:24 +0100225 public static void seek(int time, HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800226 if (mCurrentProxy == proxy && time >= 0 && mHTML5VideoView != null) {
227 mHTML5VideoView.seekTo(time);
Andrei Popescu290c34a2009-09-17 15:55:24 +0100228 }
229 }
230
231 public static void pause(HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800232 if (mCurrentProxy == proxy && mHTML5VideoView != null) {
233 mHTML5VideoView.pause();
Andrei Popescu290c34a2009-09-17 15:55:24 +0100234 }
235 }
Andrei Popescubf385d72009-09-18 18:59:52 +0100236
237 public static void onPrepared() {
Teng-Hui Zhu05049672011-04-06 18:09:27 -0700238 if (!mHTML5VideoView.isFullScreenMode() || mHTML5VideoView.getAutostart()) {
239 mHTML5VideoView.start();
240 }
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700241 if (mBaseLayer != 0) {
242 setBaseLayer(mBaseLayer);
243 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800244 }
245
246 public static void end() {
247 if (mCurrentProxy != null) {
248 if (isVideoSelfEnded)
249 mCurrentProxy.dispatchOnEnded();
250 else
251 mCurrentProxy.dispatchOnPaused();
252 }
253 isVideoSelfEnded = false;
Andrei Popescubf385d72009-09-18 18:59:52 +0100254 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100255 }
256
257 // A bunch event listeners for our VideoView
258 // MediaPlayer.OnPreparedListener
259 public void onPrepared(MediaPlayer mp) {
Andrei Popescubf385d72009-09-18 18:59:52 +0100260 VideoPlayer.onPrepared();
Andrei Popescu290c34a2009-09-17 15:55:24 +0100261 Message msg = Message.obtain(mWebCoreHandler, PREPARED);
262 Map<String, Object> map = new HashMap<String, Object>();
263 map.put("dur", new Integer(mp.getDuration()));
264 map.put("width", new Integer(mp.getVideoWidth()));
265 map.put("height", new Integer(mp.getVideoHeight()));
266 msg.obj = map;
267 mWebCoreHandler.sendMessage(msg);
268 }
269
270 // MediaPlayer.OnCompletionListener;
271 public void onCompletion(MediaPlayer mp) {
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100272 // The video ended by itself, so we need to
273 // send a message to the UI thread to dismiss
274 // the video view and to return to the WebView.
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700275 // arg1 == 1 means the video ends by itself.
276 sendMessage(obtainMessage(ENDED, 1, 0));
Andrei Popescu290c34a2009-09-17 15:55:24 +0100277 }
278
Andrei Popescu50c86232009-09-30 16:51:25 +0100279 // MediaPlayer.OnErrorListener
280 public boolean onError(MediaPlayer mp, int what, int extra) {
281 sendMessage(obtainMessage(ERROR));
282 return false;
283 }
284
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100285 public void dispatchOnEnded() {
Andrei Popescu290c34a2009-09-17 15:55:24 +0100286 Message msg = Message.obtain(mWebCoreHandler, ENDED);
287 mWebCoreHandler.sendMessage(msg);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100288 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100289
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700290 public void dispatchOnPaused() {
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700291 Message msg = Message.obtain(mWebCoreHandler, PAUSED);
292 mWebCoreHandler.sendMessage(msg);
293 }
294
295 public void dispatchOnStopFullScreen() {
296 Message msg = Message.obtain(mWebCoreHandler, STOPFULLSCREEN);
297 mWebCoreHandler.sendMessage(msg);
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700298 }
299
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000300 public void onTimeupdate() {
301 sendMessage(obtainMessage(TIMEUPDATE));
302 }
303
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800304 // When there is a frame ready from surface texture, we should tell WebView
305 // to refresh.
306 @Override
307 public void onFrameAvailable(SurfaceTexture surfaceTexture) {
308 // TODO: This should support partial invalidation too.
309 mWebView.invalidate();
310 }
311
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000312 // Handler for the messages from WebCore or Timer thread to the UI thread.
Andrei Popescu64b86a12009-09-15 20:34:18 +0100313 @Override
314 public void handleMessage(Message msg) {
315 // This executes on the UI thread.
316 switch (msg.what) {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100317 case PLAY: {
318 String url = (String) msg.obj;
319 WebChromeClient client = mWebView.getWebChromeClient();
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800320 int videoLayerID = msg.arg1;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100321 if (client != null) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800322 VideoPlayer.play(url, mSeekPosition, this, client, videoLayerID);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100323 }
324 break;
325 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100326 case SEEK: {
327 Integer time = (Integer) msg.obj;
328 mSeekPosition = time;
329 VideoPlayer.seek(mSeekPosition, this);
330 break;
331 }
332 case PAUSE: {
333 VideoPlayer.pause(this);
334 break;
335 }
Andrei Popescu46a83b42009-10-23 13:49:46 +0100336 case ENDED:
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700337 if (msg.arg1 == 1)
338 VideoPlayer.isVideoSelfEnded = true;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800339 VideoPlayer.end();
340 break;
Andrei Popescu50c86232009-09-30 16:51:25 +0100341 case ERROR: {
342 WebChromeClient client = mWebView.getWebChromeClient();
343 if (client != null) {
344 client.onHideCustomView();
345 }
346 break;
347 }
348 case LOAD_DEFAULT_POSTER: {
349 WebChromeClient client = mWebView.getWebChromeClient();
350 if (client != null) {
351 doSetPoster(client.getDefaultVideoPoster());
352 }
353 break;
354 }
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000355 case TIMEUPDATE: {
356 if (VideoPlayer.isPlaying(this)) {
357 sendTimeupdate();
358 }
359 break;
360 }
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700361 case BUFFERING_START: {
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700362 VideoPlayer.setPlayerBuffering(true);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700363 break;
364 }
365 case BUFFERING_END: {
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700366 VideoPlayer.setPlayerBuffering(false);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700367 break;
368 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100369 }
370 }
371
Andrei Popescu64b86a12009-09-15 20:34:18 +0100372 // Everything below this comment executes on the WebCore thread, except for
373 // the EventHandler methods, which are called on the network thread.
374
375 // A helper class that knows how to download posters
376 private static final class PosterDownloader implements EventHandler {
377 // The request queue. This is static as we have one queue for all posters.
378 private static RequestQueue mRequestQueue;
379 private static int mQueueRefCount = 0;
380 // The poster URL
Ben Murdoch42509792011-02-18 12:34:35 +0000381 private URL mUrl;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100382 // The proxy we're doing this for.
383 private final HTML5VideoViewProxy mProxy;
384 // The poster bytes. We only touch this on the network thread.
385 private ByteArrayOutputStream mPosterBytes;
386 // The request handle. We only touch this on the WebCore thread.
387 private RequestHandle mRequestHandle;
388 // The response status code.
389 private int mStatusCode;
390 // The response headers.
391 private Headers mHeaders;
392 // The handler to handle messages on the WebCore thread.
393 private Handler mHandler;
394
395 public PosterDownloader(String url, HTML5VideoViewProxy proxy) {
Ben Murdoch42509792011-02-18 12:34:35 +0000396 try {
397 mUrl = new URL(url);
398 } catch (MalformedURLException e) {
399 mUrl = null;
400 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100401 mProxy = proxy;
402 mHandler = new Handler();
403 }
404 // Start the download. Called on WebCore thread.
405 public void start() {
406 retainQueue();
Ben Murdoch42509792011-02-18 12:34:35 +0000407
408 if (mUrl == null) {
409 return;
410 }
411
412 // Only support downloading posters over http/https.
413 // FIXME: Add support for other schemes. WebKit seems able to load
414 // posters over other schemes e.g. file://, but gets the dimensions wrong.
415 String protocol = mUrl.getProtocol();
416 if ("http".equals(protocol) || "https".equals(protocol)) {
417 mRequestHandle = mRequestQueue.queueRequest(mUrl.toString(), "GET", null,
418 this, null, 0);
419 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100420 }
421 // Cancel the download if active and release the queue. Called on WebCore thread.
422 public void cancelAndReleaseQueue() {
423 if (mRequestHandle != null) {
424 mRequestHandle.cancel();
425 mRequestHandle = null;
426 }
427 releaseQueue();
428 }
429 // EventHandler methods. Executed on the network thread.
430 public void status(int major_version,
431 int minor_version,
432 int code,
433 String reason_phrase) {
434 mStatusCode = code;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100435 }
436
Andrei Popescu64b86a12009-09-15 20:34:18 +0100437 public void headers(Headers headers) {
438 mHeaders = headers;
439 }
440
441 public void data(byte[] data, int len) {
442 if (mPosterBytes == null) {
443 mPosterBytes = new ByteArrayOutputStream();
444 }
445 mPosterBytes.write(data, 0, len);
446 }
447
448 public void endData() {
449 if (mStatusCode == 200) {
450 if (mPosterBytes.size() > 0) {
451 Bitmap poster = BitmapFactory.decodeByteArray(
452 mPosterBytes.toByteArray(), 0, mPosterBytes.size());
Andrei Popescu50c86232009-09-30 16:51:25 +0100453 mProxy.doSetPoster(poster);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100454 }
455 cleanup();
456 } else if (mStatusCode >= 300 && mStatusCode < 400) {
457 // We have a redirect.
Ben Murdoch42509792011-02-18 12:34:35 +0000458 try {
459 mUrl = new URL(mHeaders.getLocation());
460 } catch (MalformedURLException e) {
461 mUrl = null;
462 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100463 if (mUrl != null) {
464 mHandler.post(new Runnable() {
465 public void run() {
466 if (mRequestHandle != null) {
Ben Murdoch42509792011-02-18 12:34:35 +0000467 mRequestHandle.setupRedirect(mUrl.toString(), mStatusCode,
Andrei Popescu64b86a12009-09-15 20:34:18 +0100468 new HashMap<String, String>());
469 }
470 }
471 });
472 }
473 }
474 }
475
476 public void certificate(SslCertificate certificate) {
477 // Don't care.
478 }
479
480 public void error(int id, String description) {
481 cleanup();
482 }
483
484 public boolean handleSslErrorRequest(SslError error) {
485 // Don't care. If this happens, data() will never be called so
486 // mPosterBytes will never be created, so no need to call cleanup.
487 return false;
488 }
489 // Tears down the poster bytes stream. Called on network thread.
490 private void cleanup() {
491 if (mPosterBytes != null) {
492 try {
493 mPosterBytes.close();
494 } catch (IOException ignored) {
495 // Ignored.
496 } finally {
497 mPosterBytes = null;
498 }
499 }
500 }
501
502 // Queue management methods. Called on WebCore thread.
503 private void retainQueue() {
504 if (mRequestQueue == null) {
505 mRequestQueue = new RequestQueue(mProxy.getContext());
506 }
507 mQueueRefCount++;
508 }
509
510 private void releaseQueue() {
511 if (mQueueRefCount == 0) {
512 return;
513 }
514 if (--mQueueRefCount == 0) {
515 mRequestQueue.shutdown();
516 mRequestQueue = null;
517 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100518 }
519 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100520
521 /**
522 * Private constructor.
Andrei Popescu290c34a2009-09-17 15:55:24 +0100523 * @param webView is the WebView that hosts the video.
524 * @param nativePtr is the C++ pointer to the MediaPlayerPrivate object.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100525 */
Andrei Popescu290c34a2009-09-17 15:55:24 +0100526 private HTML5VideoViewProxy(WebView webView, int nativePtr) {
Andrei Popescu6fa29582009-06-19 14:54:09 +0100527 // This handler is for the main (UI) thread.
528 super(Looper.getMainLooper());
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100529 // Save the WebView object.
530 mWebView = webView;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800531 // Pass Proxy into webview, such that every time we have a setBaseLayer
532 // call, we tell this Proxy to call the native to update the layer tree
533 // for the Video Layer's surface texture info
534 mWebView.setHTML5VideoViewProxy(this);
Andrei Popescu290c34a2009-09-17 15:55:24 +0100535 // Save the native ptr
536 mNativePointer = nativePtr;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100537 // create the message handler for this thread
538 createWebCoreHandler();
Andrei Popescu6fa29582009-06-19 14:54:09 +0100539 }
540
Andrei Popescu64b86a12009-09-15 20:34:18 +0100541 private void createWebCoreHandler() {
542 mWebCoreHandler = new Handler() {
543 @Override
544 public void handleMessage(Message msg) {
545 switch (msg.what) {
Andrei Popescu290c34a2009-09-17 15:55:24 +0100546 case PREPARED: {
547 Map<String, Object> map = (Map<String, Object>) msg.obj;
548 Integer duration = (Integer) map.get("dur");
549 Integer width = (Integer) map.get("width");
550 Integer height = (Integer) map.get("height");
551 nativeOnPrepared(duration.intValue(), width.intValue(),
552 height.intValue(), mNativePointer);
553 break;
554 }
555 case ENDED:
Ben Murdochff19d192011-01-17 18:08:56 +0000556 mSeekPosition = 0;
Andrei Popescu290c34a2009-09-17 15:55:24 +0100557 nativeOnEnded(mNativePointer);
558 break;
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700559 case PAUSED:
560 nativeOnPaused(mNativePointer);
561 break;
Andrei Popescu50c86232009-09-30 16:51:25 +0100562 case POSTER_FETCHED:
563 Bitmap poster = (Bitmap) msg.obj;
564 nativeOnPosterFetched(poster, mNativePointer);
565 break;
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000566 case TIMEUPDATE:
567 nativeOnTimeupdate(msg.arg1, mNativePointer);
568 break;
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700569 case STOPFULLSCREEN:
570 nativeOnStopFullscreen(mNativePointer);
571 break;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100572 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100573 }
574 };
Andrei Popescu6fa29582009-06-19 14:54:09 +0100575 }
576
Andrei Popescu64b86a12009-09-15 20:34:18 +0100577 private void doSetPoster(Bitmap poster) {
578 if (poster == null) {
579 return;
580 }
Andrei Popescu50c86232009-09-30 16:51:25 +0100581 // Save a ref to the bitmap and send it over to the WebCore thread.
582 mPoster = poster;
583 Message msg = Message.obtain(mWebCoreHandler, POSTER_FETCHED);
584 msg.obj = poster;
585 mWebCoreHandler.sendMessage(msg);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100586 }
587
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000588 private void sendTimeupdate() {
589 Message msg = Message.obtain(mWebCoreHandler, TIMEUPDATE);
590 msg.arg1 = VideoPlayer.getCurrentPosition();
591 mWebCoreHandler.sendMessage(msg);
592 }
593
Andrei Popescu64b86a12009-09-15 20:34:18 +0100594 public Context getContext() {
595 return mWebView.getContext();
596 }
597
598 // The public methods below are all called from WebKit only.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100599 /**
600 * Play a video stream.
601 * @param url is the URL of the video stream.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100602 */
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800603 public void play(String url, int position, int videoLayerID) {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100604 if (url == null) {
605 return;
606 }
Ben Murdochff19d192011-01-17 18:08:56 +0000607
608 if (position > 0) {
609 seek(position);
610 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100611 Message message = obtainMessage(PLAY);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800612 message.arg1 = videoLayerID;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100613 message.obj = url;
Andrei Popescu6fa29582009-06-19 14:54:09 +0100614 sendMessage(message);
615 }
616
Andrei Popescu64b86a12009-09-15 20:34:18 +0100617 /**
Andrei Popescu290c34a2009-09-17 15:55:24 +0100618 * Seek into the video stream.
619 * @param time is the position in the video stream.
620 */
621 public void seek(int time) {
622 Message message = obtainMessage(SEEK);
623 message.obj = new Integer(time);
624 sendMessage(message);
625 }
626
627 /**
628 * Pause the playback.
629 */
630 public void pause() {
631 Message message = obtainMessage(PAUSE);
632 sendMessage(message);
633 }
634
635 /**
Andrei Popescu50c86232009-09-30 16:51:25 +0100636 * Tear down this proxy object.
Andrei Popescu64b86a12009-09-15 20:34:18 +0100637 */
Andrei Popescu50c86232009-09-30 16:51:25 +0100638 public void teardown() {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100639 // This is called by the C++ MediaPlayerPrivate dtor.
640 // Cancel any active poster download.
641 if (mPosterDownloader != null) {
642 mPosterDownloader.cancelAndReleaseQueue();
643 }
Andrei Popescu50c86232009-09-30 16:51:25 +0100644 mNativePointer = 0;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100645 }
646
647 /**
648 * Load the poster image.
649 * @param url is the URL of the poster image.
650 */
651 public void loadPoster(String url) {
652 if (url == null) {
Andrei Popescu50c86232009-09-30 16:51:25 +0100653 Message message = obtainMessage(LOAD_DEFAULT_POSTER);
654 sendMessage(message);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100655 return;
656 }
657 // Cancel any active poster download.
658 if (mPosterDownloader != null) {
659 mPosterDownloader.cancelAndReleaseQueue();
660 }
661 // Load the poster asynchronously
662 mPosterDownloader = new PosterDownloader(url, this);
663 mPosterDownloader.start();
Patrick Scott0a5ce012009-07-02 08:56:10 -0400664 }
665
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700666 // These three function are called from UI thread only by WebView.
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800667 public void setBaseLayer(int layer) {
668 VideoPlayer.setBaseLayer(layer);
669 }
670
671 public void pauseAndDispatch() {
672 VideoPlayer.pauseAndDispatch();
673 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700674
675 public void enterFullScreenVideo(int layerId, String url) {
676 VideoPlayer.enterFullScreenVideo(layerId, url, this, mWebView);
677 }
678
Andrei Popescu6fa29582009-06-19 14:54:09 +0100679 /**
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100680 * The factory for HTML5VideoViewProxy instances.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100681 * @param webViewCore is the WebViewCore that is requesting the proxy.
682 *
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100683 * @return a new HTML5VideoViewProxy object.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100684 */
Andrei Popescu290c34a2009-09-17 15:55:24 +0100685 public static HTML5VideoViewProxy getInstance(WebViewCore webViewCore, int nativePtr) {
686 return new HTML5VideoViewProxy(webViewCore.getWebView(), nativePtr);
Andrei Popescu6fa29582009-06-19 14:54:09 +0100687 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100688
Ben Murdoch1708ad52010-11-11 15:56:16 +0000689 /* package */ WebView getWebView() {
690 return mWebView;
691 }
692
Andrei Popescu290c34a2009-09-17 15:55:24 +0100693 private native void nativeOnPrepared(int duration, int width, int height, int nativePointer);
694 private native void nativeOnEnded(int nativePointer);
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700695 private native void nativeOnPaused(int nativePointer);
Andrei Popescu50c86232009-09-30 16:51:25 +0100696 private native void nativeOnPosterFetched(Bitmap poster, int nativePointer);
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000697 private native void nativeOnTimeupdate(int position, int nativePointer);
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700698 private native void nativeOnStopFullscreen(int nativePointer);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800699 private native static boolean nativeSendSurfaceTexture(SurfaceTexture texture,
700 int baseLayer, int videoLayerId, int textureName,
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700701 int playerState);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700702
703 @Override
704 public boolean onInfo(MediaPlayer mp, int what, int extra) {
705 if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
706 sendMessage(obtainMessage(BUFFERING_START, what, extra));
707 } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
708 sendMessage(obtainMessage(BUFFERING_END, what, extra));
709 }
710 return false;
711 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100712}