blob: 7c2dc380f60eadc44e11b8ac4187804e1a1eca11 [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;
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -080069 private static final int RESTORESTATE = 205;
Andrei Popescu64b86a12009-09-15 20:34:18 +010070
Andrei Popescu048eb3b2010-01-11 21:12:54 +000071 // Timer thread -> UI thread
72 private static final int TIMEUPDATE = 300;
73
Andrei Popescu290c34a2009-09-17 15:55:24 +010074 // The C++ MediaPlayerPrivateAndroid object.
75 int mNativePointer;
Andrei Popescu64b86a12009-09-15 20:34:18 +010076 // The handler for WebCore thread messages;
77 private Handler mWebCoreHandler;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +010078 // The WebView instance that created this view.
79 private WebView mWebView;
Andrei Popescu64b86a12009-09-15 20:34:18 +010080 // The poster image to be shown when the video is not playing.
Andrei Popescu50c86232009-09-30 16:51:25 +010081 // This ref prevents the bitmap from being GC'ed.
82 private Bitmap mPoster;
Andrei Popescu64b86a12009-09-15 20:34:18 +010083 // The poster downloader.
84 private PosterDownloader mPosterDownloader;
Andrei Popescu290c34a2009-09-17 15:55:24 +010085 // The seek position.
86 private int mSeekPosition;
Andrei Popescu64b86a12009-09-15 20:34:18 +010087 // A helper class to control the playback. This executes on the UI thread!
88 private static final class VideoPlayer {
89 // The proxy that is currently playing (if any).
90 private static HTML5VideoViewProxy mCurrentProxy;
91 // The VideoView instance. This is a singleton for now, at least until
92 // http://b/issue?id=1973663 is fixed.
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -080093 private static HTML5VideoView mHTML5VideoView;
Andrei Popescu048eb3b2010-01-11 21:12:54 +000094
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -080095 private static boolean isVideoSelfEnded = false;
96 // By using the baseLayer and the current video Layer ID, we can
97 // identify the exact layer on the UI thread to use the SurfaceTexture.
98 private static int mBaseLayer = 0;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +010099
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700100 private static void setPlayerBuffering(boolean playerBuffering) {
101 mHTML5VideoView.setPlayerBuffering(playerBuffering);
102 }
103
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800104 // Every time webView setBaseLayer, this will be called.
105 // When we found the Video layer, then we set the Surface Texture to it.
106 // Otherwise, we may want to delete the Surface Texture to save memory.
107 public static void setBaseLayer(int layer) {
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700108 // Don't do this for full screen mode.
109 if (mHTML5VideoView != null
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -0700110 && !mHTML5VideoView.isFullScreenMode()
111 && !mHTML5VideoView.surfaceTextureDeleted()) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800112 mBaseLayer = layer;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800113
114 int currentVideoLayerId = mHTML5VideoView.getVideoLayerId();
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -0700115 SurfaceTexture surfTexture = mHTML5VideoView.getSurfaceTexture(currentVideoLayerId);
116 int textureName = mHTML5VideoView.getTextureName();
117
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800118 if (layer != 0 && surfTexture != null && currentVideoLayerId != -1) {
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700119 int playerState = mHTML5VideoView.getCurrentState();
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700120 if (mHTML5VideoView.getPlayerBuffering())
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700121 playerState = HTML5VideoView.STATE_NOTPREPARED;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800122 boolean foundInTree = nativeSendSurfaceTexture(surfTexture,
123 layer, currentVideoLayerId, textureName,
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700124 playerState);
Teng-Hui Zhu1e26d822011-03-23 16:54:20 -0700125 if (playerState >= HTML5VideoView.STATE_PREPARED
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700126 && !foundInTree) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800127 mHTML5VideoView.pauseAndDispatch(mCurrentProxy);
128 mHTML5VideoView.deleteSurfaceTexture();
Andrei Popescu31d2aa12010-04-08 15:19:22 +0100129 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100130 }
Andrei Popescua41f97b2010-01-11 18:36:25 +0000131 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800132 }
Andrei Popescua41f97b2010-01-11 18:36:25 +0000133
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800134 // When a WebView is paused, we also want to pause the video in it.
135 public static void pauseAndDispatch() {
136 if (mHTML5VideoView != null) {
137 mHTML5VideoView.pauseAndDispatch(mCurrentProxy);
138 // When switching out, clean the video content on the old page
139 // by telling the layer not readyToUseSurfTex.
140 setBaseLayer(mBaseLayer);
141 }
142 }
143
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700144 public static void enterFullScreenVideo(int layerId, String url,
145 HTML5VideoViewProxy proxy, WebView webView) {
146 // Save the inline video info and inherit it in the full screen
147 int savePosition = 0;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700148 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();
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700153 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700154 mHTML5VideoView.release();
155 }
156 mHTML5VideoView = new HTML5VideoFullScreen(proxy.getContext(),
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800157 layerId, savePosition);
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700158 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();
Teng-Hui Zhu1a88acb2011-06-01 13:28:41 -0700174 backFromFullScreenMode = mHTML5VideoView.fullScreenExited();
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700175 }
176
177 if (backFromFullScreenMode
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -0700178 || currentVideoLayerId != videoLayerId
179 || mHTML5VideoView.surfaceTextureDeleted()) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800180 // Here, we handle the case when switching to a new video,
181 // either inside a WebView or across WebViews
182 // For switching videos within a WebView or across the WebView,
183 // we need to pause the old one and re-create a new media player
184 // inside the HTML5VideoView.
185 if (mHTML5VideoView != null) {
Teng-Hui Zhu22954d42011-04-07 17:13:18 -0700186 if (!backFromFullScreenMode) {
187 mHTML5VideoView.pauseAndDispatch(mCurrentProxy);
188 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800189 // release the media player to avoid finalize error
190 mHTML5VideoView.release();
191 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800192 mCurrentProxy = proxy;
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800193 mHTML5VideoView = new HTML5VideoInline(videoLayerId, time);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800194
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700195 mHTML5VideoView.setVideoURI(url, mCurrentProxy);
196 mHTML5VideoView.prepareDataAndDisplayMode(proxy);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800197 } else if (mCurrentProxy == proxy) {
198 // Here, we handle the case when we keep playing with one video
199 if (!mHTML5VideoView.isPlaying()) {
200 mHTML5VideoView.seekTo(time);
201 mHTML5VideoView.start();
202 }
203 } else if (mCurrentProxy != null) {
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700204 // Some other video is already playing. Notify the caller that
205 // its playback ended.
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100206 proxy.dispatchOnEnded();
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100207 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100208 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100209
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000210 public static boolean isPlaying(HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800211 return (mCurrentProxy == proxy && mHTML5VideoView != null
212 && mHTML5VideoView.isPlaying());
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000213 }
214
215 public static int getCurrentPosition() {
216 int currentPosMs = 0;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800217 if (mHTML5VideoView != null) {
218 currentPosMs = mHTML5VideoView.getCurrentPosition();
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000219 }
220 return currentPosMs;
221 }
222
Andrei Popescu290c34a2009-09-17 15:55:24 +0100223 public static void seek(int time, HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800224 if (mCurrentProxy == proxy && time >= 0 && mHTML5VideoView != null) {
225 mHTML5VideoView.seekTo(time);
Andrei Popescu290c34a2009-09-17 15:55:24 +0100226 }
227 }
228
229 public static void pause(HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800230 if (mCurrentProxy == proxy && mHTML5VideoView != null) {
231 mHTML5VideoView.pause();
Andrei Popescu290c34a2009-09-17 15:55:24 +0100232 }
233 }
Andrei Popescubf385d72009-09-18 18:59:52 +0100234
235 public static void onPrepared() {
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800236 if (!mHTML5VideoView.isFullScreenMode()) {
Teng-Hui Zhu05049672011-04-06 18:09:27 -0700237 mHTML5VideoView.start();
238 }
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700239 if (mBaseLayer != 0) {
240 setBaseLayer(mBaseLayer);
241 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800242 }
243
244 public static void end() {
245 if (mCurrentProxy != null) {
246 if (isVideoSelfEnded)
247 mCurrentProxy.dispatchOnEnded();
248 else
249 mCurrentProxy.dispatchOnPaused();
250 }
251 isVideoSelfEnded = false;
Andrei Popescubf385d72009-09-18 18:59:52 +0100252 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100253 }
254
255 // A bunch event listeners for our VideoView
256 // MediaPlayer.OnPreparedListener
257 public void onPrepared(MediaPlayer mp) {
Andrei Popescubf385d72009-09-18 18:59:52 +0100258 VideoPlayer.onPrepared();
Andrei Popescu290c34a2009-09-17 15:55:24 +0100259 Message msg = Message.obtain(mWebCoreHandler, PREPARED);
260 Map<String, Object> map = new HashMap<String, Object>();
261 map.put("dur", new Integer(mp.getDuration()));
262 map.put("width", new Integer(mp.getVideoWidth()));
263 map.put("height", new Integer(mp.getVideoHeight()));
264 msg.obj = map;
265 mWebCoreHandler.sendMessage(msg);
266 }
267
268 // MediaPlayer.OnCompletionListener;
269 public void onCompletion(MediaPlayer mp) {
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100270 // The video ended by itself, so we need to
271 // send a message to the UI thread to dismiss
272 // the video view and to return to the WebView.
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700273 // arg1 == 1 means the video ends by itself.
274 sendMessage(obtainMessage(ENDED, 1, 0));
Andrei Popescu290c34a2009-09-17 15:55:24 +0100275 }
276
Andrei Popescu50c86232009-09-30 16:51:25 +0100277 // MediaPlayer.OnErrorListener
278 public boolean onError(MediaPlayer mp, int what, int extra) {
279 sendMessage(obtainMessage(ERROR));
280 return false;
281 }
282
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100283 public void dispatchOnEnded() {
Andrei Popescu290c34a2009-09-17 15:55:24 +0100284 Message msg = Message.obtain(mWebCoreHandler, ENDED);
285 mWebCoreHandler.sendMessage(msg);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100286 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100287
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700288 public void dispatchOnPaused() {
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700289 Message msg = Message.obtain(mWebCoreHandler, PAUSED);
290 mWebCoreHandler.sendMessage(msg);
291 }
292
293 public void dispatchOnStopFullScreen() {
294 Message msg = Message.obtain(mWebCoreHandler, STOPFULLSCREEN);
295 mWebCoreHandler.sendMessage(msg);
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700296 }
297
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800298 public void dispatchOnRestoreState() {
299 Message msg = Message.obtain(mWebCoreHandler, RESTORESTATE);
300 mWebCoreHandler.sendMessage(msg);
301 }
302
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000303 public void onTimeupdate() {
304 sendMessage(obtainMessage(TIMEUPDATE));
305 }
306
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800307 // When there is a frame ready from surface texture, we should tell WebView
308 // to refresh.
309 @Override
310 public void onFrameAvailable(SurfaceTexture surfaceTexture) {
311 // TODO: This should support partial invalidation too.
312 mWebView.invalidate();
313 }
314
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000315 // Handler for the messages from WebCore or Timer thread to the UI thread.
Andrei Popescu64b86a12009-09-15 20:34:18 +0100316 @Override
317 public void handleMessage(Message msg) {
318 // This executes on the UI thread.
319 switch (msg.what) {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100320 case PLAY: {
321 String url = (String) msg.obj;
322 WebChromeClient client = mWebView.getWebChromeClient();
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800323 int videoLayerID = msg.arg1;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100324 if (client != null) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800325 VideoPlayer.play(url, mSeekPosition, this, client, videoLayerID);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100326 }
327 break;
328 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100329 case SEEK: {
330 Integer time = (Integer) msg.obj;
331 mSeekPosition = time;
332 VideoPlayer.seek(mSeekPosition, this);
333 break;
334 }
335 case PAUSE: {
336 VideoPlayer.pause(this);
337 break;
338 }
Andrei Popescu46a83b42009-10-23 13:49:46 +0100339 case ENDED:
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700340 if (msg.arg1 == 1)
341 VideoPlayer.isVideoSelfEnded = true;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800342 VideoPlayer.end();
343 break;
Andrei Popescu50c86232009-09-30 16:51:25 +0100344 case ERROR: {
345 WebChromeClient client = mWebView.getWebChromeClient();
346 if (client != null) {
347 client.onHideCustomView();
348 }
349 break;
350 }
351 case LOAD_DEFAULT_POSTER: {
352 WebChromeClient client = mWebView.getWebChromeClient();
353 if (client != null) {
354 doSetPoster(client.getDefaultVideoPoster());
355 }
356 break;
357 }
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000358 case TIMEUPDATE: {
359 if (VideoPlayer.isPlaying(this)) {
360 sendTimeupdate();
361 }
362 break;
363 }
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700364 case BUFFERING_START: {
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700365 VideoPlayer.setPlayerBuffering(true);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700366 break;
367 }
368 case BUFFERING_END: {
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700369 VideoPlayer.setPlayerBuffering(false);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700370 break;
371 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100372 }
373 }
374
Andrei Popescu64b86a12009-09-15 20:34:18 +0100375 // Everything below this comment executes on the WebCore thread, except for
376 // the EventHandler methods, which are called on the network thread.
377
378 // A helper class that knows how to download posters
379 private static final class PosterDownloader implements EventHandler {
380 // The request queue. This is static as we have one queue for all posters.
381 private static RequestQueue mRequestQueue;
382 private static int mQueueRefCount = 0;
383 // The poster URL
Ben Murdoch42509792011-02-18 12:34:35 +0000384 private URL mUrl;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100385 // The proxy we're doing this for.
386 private final HTML5VideoViewProxy mProxy;
387 // The poster bytes. We only touch this on the network thread.
388 private ByteArrayOutputStream mPosterBytes;
389 // The request handle. We only touch this on the WebCore thread.
390 private RequestHandle mRequestHandle;
391 // The response status code.
392 private int mStatusCode;
393 // The response headers.
394 private Headers mHeaders;
395 // The handler to handle messages on the WebCore thread.
396 private Handler mHandler;
397
398 public PosterDownloader(String url, HTML5VideoViewProxy proxy) {
Ben Murdoch42509792011-02-18 12:34:35 +0000399 try {
400 mUrl = new URL(url);
401 } catch (MalformedURLException e) {
402 mUrl = null;
403 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100404 mProxy = proxy;
405 mHandler = new Handler();
406 }
407 // Start the download. Called on WebCore thread.
408 public void start() {
409 retainQueue();
Ben Murdoch42509792011-02-18 12:34:35 +0000410
411 if (mUrl == null) {
412 return;
413 }
414
415 // Only support downloading posters over http/https.
416 // FIXME: Add support for other schemes. WebKit seems able to load
417 // posters over other schemes e.g. file://, but gets the dimensions wrong.
418 String protocol = mUrl.getProtocol();
419 if ("http".equals(protocol) || "https".equals(protocol)) {
420 mRequestHandle = mRequestQueue.queueRequest(mUrl.toString(), "GET", null,
421 this, null, 0);
422 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100423 }
424 // Cancel the download if active and release the queue. Called on WebCore thread.
425 public void cancelAndReleaseQueue() {
426 if (mRequestHandle != null) {
427 mRequestHandle.cancel();
428 mRequestHandle = null;
429 }
430 releaseQueue();
431 }
432 // EventHandler methods. Executed on the network thread.
433 public void status(int major_version,
434 int minor_version,
435 int code,
436 String reason_phrase) {
437 mStatusCode = code;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100438 }
439
Andrei Popescu64b86a12009-09-15 20:34:18 +0100440 public void headers(Headers headers) {
441 mHeaders = headers;
442 }
443
444 public void data(byte[] data, int len) {
445 if (mPosterBytes == null) {
446 mPosterBytes = new ByteArrayOutputStream();
447 }
448 mPosterBytes.write(data, 0, len);
449 }
450
451 public void endData() {
452 if (mStatusCode == 200) {
453 if (mPosterBytes.size() > 0) {
454 Bitmap poster = BitmapFactory.decodeByteArray(
455 mPosterBytes.toByteArray(), 0, mPosterBytes.size());
Andrei Popescu50c86232009-09-30 16:51:25 +0100456 mProxy.doSetPoster(poster);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100457 }
458 cleanup();
459 } else if (mStatusCode >= 300 && mStatusCode < 400) {
460 // We have a redirect.
Ben Murdoch42509792011-02-18 12:34:35 +0000461 try {
462 mUrl = new URL(mHeaders.getLocation());
463 } catch (MalformedURLException e) {
464 mUrl = null;
465 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100466 if (mUrl != null) {
467 mHandler.post(new Runnable() {
468 public void run() {
469 if (mRequestHandle != null) {
Ben Murdoch42509792011-02-18 12:34:35 +0000470 mRequestHandle.setupRedirect(mUrl.toString(), mStatusCode,
Andrei Popescu64b86a12009-09-15 20:34:18 +0100471 new HashMap<String, String>());
472 }
473 }
474 });
475 }
476 }
477 }
478
479 public void certificate(SslCertificate certificate) {
480 // Don't care.
481 }
482
483 public void error(int id, String description) {
484 cleanup();
485 }
486
487 public boolean handleSslErrorRequest(SslError error) {
488 // Don't care. If this happens, data() will never be called so
489 // mPosterBytes will never be created, so no need to call cleanup.
490 return false;
491 }
492 // Tears down the poster bytes stream. Called on network thread.
493 private void cleanup() {
494 if (mPosterBytes != null) {
495 try {
496 mPosterBytes.close();
497 } catch (IOException ignored) {
498 // Ignored.
499 } finally {
500 mPosterBytes = null;
501 }
502 }
503 }
504
505 // Queue management methods. Called on WebCore thread.
506 private void retainQueue() {
507 if (mRequestQueue == null) {
508 mRequestQueue = new RequestQueue(mProxy.getContext());
509 }
510 mQueueRefCount++;
511 }
512
513 private void releaseQueue() {
514 if (mQueueRefCount == 0) {
515 return;
516 }
517 if (--mQueueRefCount == 0) {
518 mRequestQueue.shutdown();
519 mRequestQueue = null;
520 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100521 }
522 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100523
524 /**
525 * Private constructor.
Andrei Popescu290c34a2009-09-17 15:55:24 +0100526 * @param webView is the WebView that hosts the video.
527 * @param nativePtr is the C++ pointer to the MediaPlayerPrivate object.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100528 */
Andrei Popescu290c34a2009-09-17 15:55:24 +0100529 private HTML5VideoViewProxy(WebView webView, int nativePtr) {
Andrei Popescu6fa29582009-06-19 14:54:09 +0100530 // This handler is for the main (UI) thread.
531 super(Looper.getMainLooper());
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100532 // Save the WebView object.
533 mWebView = webView;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800534 // Pass Proxy into webview, such that every time we have a setBaseLayer
535 // call, we tell this Proxy to call the native to update the layer tree
536 // for the Video Layer's surface texture info
537 mWebView.setHTML5VideoViewProxy(this);
Andrei Popescu290c34a2009-09-17 15:55:24 +0100538 // Save the native ptr
539 mNativePointer = nativePtr;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100540 // create the message handler for this thread
541 createWebCoreHandler();
Andrei Popescu6fa29582009-06-19 14:54:09 +0100542 }
543
Andrei Popescu64b86a12009-09-15 20:34:18 +0100544 private void createWebCoreHandler() {
545 mWebCoreHandler = new Handler() {
546 @Override
547 public void handleMessage(Message msg) {
548 switch (msg.what) {
Andrei Popescu290c34a2009-09-17 15:55:24 +0100549 case PREPARED: {
550 Map<String, Object> map = (Map<String, Object>) msg.obj;
551 Integer duration = (Integer) map.get("dur");
552 Integer width = (Integer) map.get("width");
553 Integer height = (Integer) map.get("height");
554 nativeOnPrepared(duration.intValue(), width.intValue(),
555 height.intValue(), mNativePointer);
556 break;
557 }
558 case ENDED:
Ben Murdochff19d192011-01-17 18:08:56 +0000559 mSeekPosition = 0;
Andrei Popescu290c34a2009-09-17 15:55:24 +0100560 nativeOnEnded(mNativePointer);
561 break;
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700562 case PAUSED:
563 nativeOnPaused(mNativePointer);
564 break;
Andrei Popescu50c86232009-09-30 16:51:25 +0100565 case POSTER_FETCHED:
566 Bitmap poster = (Bitmap) msg.obj;
567 nativeOnPosterFetched(poster, mNativePointer);
568 break;
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000569 case TIMEUPDATE:
570 nativeOnTimeupdate(msg.arg1, mNativePointer);
571 break;
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700572 case STOPFULLSCREEN:
573 nativeOnStopFullscreen(mNativePointer);
574 break;
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800575 case RESTORESTATE:
576 nativeOnRestoreState(mNativePointer);
577 break;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100578 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100579 }
580 };
Andrei Popescu6fa29582009-06-19 14:54:09 +0100581 }
582
Andrei Popescu64b86a12009-09-15 20:34:18 +0100583 private void doSetPoster(Bitmap poster) {
584 if (poster == null) {
585 return;
586 }
Andrei Popescu50c86232009-09-30 16:51:25 +0100587 // Save a ref to the bitmap and send it over to the WebCore thread.
588 mPoster = poster;
589 Message msg = Message.obtain(mWebCoreHandler, POSTER_FETCHED);
590 msg.obj = poster;
591 mWebCoreHandler.sendMessage(msg);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100592 }
593
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000594 private void sendTimeupdate() {
595 Message msg = Message.obtain(mWebCoreHandler, TIMEUPDATE);
596 msg.arg1 = VideoPlayer.getCurrentPosition();
597 mWebCoreHandler.sendMessage(msg);
598 }
599
Andrei Popescu64b86a12009-09-15 20:34:18 +0100600 public Context getContext() {
601 return mWebView.getContext();
602 }
603
604 // The public methods below are all called from WebKit only.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100605 /**
606 * Play a video stream.
607 * @param url is the URL of the video stream.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100608 */
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800609 public void play(String url, int position, int videoLayerID) {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100610 if (url == null) {
611 return;
612 }
Ben Murdochff19d192011-01-17 18:08:56 +0000613
614 if (position > 0) {
615 seek(position);
616 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100617 Message message = obtainMessage(PLAY);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800618 message.arg1 = videoLayerID;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100619 message.obj = url;
Andrei Popescu6fa29582009-06-19 14:54:09 +0100620 sendMessage(message);
621 }
622
Andrei Popescu64b86a12009-09-15 20:34:18 +0100623 /**
Andrei Popescu290c34a2009-09-17 15:55:24 +0100624 * Seek into the video stream.
625 * @param time is the position in the video stream.
626 */
627 public void seek(int time) {
628 Message message = obtainMessage(SEEK);
629 message.obj = new Integer(time);
630 sendMessage(message);
631 }
632
633 /**
634 * Pause the playback.
635 */
636 public void pause() {
637 Message message = obtainMessage(PAUSE);
638 sendMessage(message);
639 }
640
641 /**
Andrei Popescu50c86232009-09-30 16:51:25 +0100642 * Tear down this proxy object.
Andrei Popescu64b86a12009-09-15 20:34:18 +0100643 */
Andrei Popescu50c86232009-09-30 16:51:25 +0100644 public void teardown() {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100645 // This is called by the C++ MediaPlayerPrivate dtor.
646 // Cancel any active poster download.
647 if (mPosterDownloader != null) {
648 mPosterDownloader.cancelAndReleaseQueue();
649 }
Andrei Popescu50c86232009-09-30 16:51:25 +0100650 mNativePointer = 0;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100651 }
652
653 /**
654 * Load the poster image.
655 * @param url is the URL of the poster image.
656 */
657 public void loadPoster(String url) {
658 if (url == null) {
Andrei Popescu50c86232009-09-30 16:51:25 +0100659 Message message = obtainMessage(LOAD_DEFAULT_POSTER);
660 sendMessage(message);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100661 return;
662 }
663 // Cancel any active poster download.
664 if (mPosterDownloader != null) {
665 mPosterDownloader.cancelAndReleaseQueue();
666 }
667 // Load the poster asynchronously
668 mPosterDownloader = new PosterDownloader(url, this);
669 mPosterDownloader.start();
Patrick Scott0a5ce012009-07-02 08:56:10 -0400670 }
671
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700672 // These three function are called from UI thread only by WebView.
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800673 public void setBaseLayer(int layer) {
674 VideoPlayer.setBaseLayer(layer);
675 }
676
677 public void pauseAndDispatch() {
678 VideoPlayer.pauseAndDispatch();
679 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700680
681 public void enterFullScreenVideo(int layerId, String url) {
682 VideoPlayer.enterFullScreenVideo(layerId, url, this, mWebView);
683 }
684
Andrei Popescu6fa29582009-06-19 14:54:09 +0100685 /**
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100686 * The factory for HTML5VideoViewProxy instances.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100687 * @param webViewCore is the WebViewCore that is requesting the proxy.
688 *
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100689 * @return a new HTML5VideoViewProxy object.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100690 */
Andrei Popescu290c34a2009-09-17 15:55:24 +0100691 public static HTML5VideoViewProxy getInstance(WebViewCore webViewCore, int nativePtr) {
692 return new HTML5VideoViewProxy(webViewCore.getWebView(), nativePtr);
Andrei Popescu6fa29582009-06-19 14:54:09 +0100693 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100694
Ben Murdoch1708ad52010-11-11 15:56:16 +0000695 /* package */ WebView getWebView() {
696 return mWebView;
697 }
698
Andrei Popescu290c34a2009-09-17 15:55:24 +0100699 private native void nativeOnPrepared(int duration, int width, int height, int nativePointer);
700 private native void nativeOnEnded(int nativePointer);
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700701 private native void nativeOnPaused(int nativePointer);
Andrei Popescu50c86232009-09-30 16:51:25 +0100702 private native void nativeOnPosterFetched(Bitmap poster, int nativePointer);
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000703 private native void nativeOnTimeupdate(int position, int nativePointer);
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700704 private native void nativeOnStopFullscreen(int nativePointer);
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800705 private native void nativeOnRestoreState(int nativePointer);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800706 private native static boolean nativeSendSurfaceTexture(SurfaceTexture texture,
707 int baseLayer, int videoLayerId, int textureName,
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700708 int playerState);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700709
710 @Override
711 public boolean onInfo(MediaPlayer mp, int what, int extra) {
712 if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
713 sendMessage(obtainMessage(BUFFERING_START, what, extra));
714 } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
715 sendMessage(obtainMessage(BUFFERING_END, what, extra));
716 }
717 return false;
718 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100719}