blob: 2dba89d2beb678b57db04ec3499a037abb4a5fac [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;
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -070062 private static final int ENTER_FULLSCREEN = 107;
Andrei Popescu6fa29582009-06-19 14:54:09 +010063
Andrei Popescu64b86a12009-09-15 20:34:18 +010064 // Message Ids to be handled on the WebCore thread
Andrei Popescu290c34a2009-09-17 15:55:24 +010065 private static final int PREPARED = 200;
66 private static final int ENDED = 201;
Andrei Popescu50c86232009-09-30 16:51:25 +010067 private static final int POSTER_FETCHED = 202;
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -070068 private static final int PAUSED = 203;
Teng-Hui Zhub109c882011-05-04 16:19:49 -070069 private static final int STOPFULLSCREEN = 204;
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -080070 private static final int RESTORESTATE = 205;
Andrei Popescu64b86a12009-09-15 20:34:18 +010071
Andrei Popescu048eb3b2010-01-11 21:12:54 +000072 // Timer thread -> UI thread
73 private static final int TIMEUPDATE = 300;
74
Andrei Popescu290c34a2009-09-17 15:55:24 +010075 // The C++ MediaPlayerPrivateAndroid object.
76 int mNativePointer;
Andrei Popescu64b86a12009-09-15 20:34:18 +010077 // The handler for WebCore thread messages;
78 private Handler mWebCoreHandler;
Jonathan Dixon3c909522012-02-28 18:45:06 +000079 // The WebViewClassic instance that created this view.
80 private WebViewClassic mWebView;
Andrei Popescu64b86a12009-09-15 20:34:18 +010081 // The poster image to be shown when the video is not playing.
Andrei Popescu50c86232009-09-30 16:51:25 +010082 // This ref prevents the bitmap from being GC'ed.
83 private Bitmap mPoster;
Andrei Popescu64b86a12009-09-15 20:34:18 +010084 // The poster downloader.
85 private PosterDownloader mPosterDownloader;
Andrei Popescu290c34a2009-09-17 15:55:24 +010086 // The seek position.
87 private int mSeekPosition;
Andrei Popescu64b86a12009-09-15 20:34:18 +010088 // A helper class to control the playback. This executes on the UI thread!
89 private static final class VideoPlayer {
90 // The proxy that is currently playing (if any).
91 private static HTML5VideoViewProxy mCurrentProxy;
92 // The VideoView instance. This is a singleton for now, at least until
93 // http://b/issue?id=1973663 is fixed.
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -080094 private static HTML5VideoView mHTML5VideoView;
Andrei Popescu048eb3b2010-01-11 21:12:54 +000095
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -080096 private static boolean isVideoSelfEnded = false;
97 // By using the baseLayer and the current video Layer ID, we can
98 // identify the exact layer on the UI thread to use the SurfaceTexture.
99 private static int mBaseLayer = 0;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100100
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700101 private static void setPlayerBuffering(boolean playerBuffering) {
102 mHTML5VideoView.setPlayerBuffering(playerBuffering);
103 }
104
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800105 // Every time webView setBaseLayer, this will be called.
106 // When we found the Video layer, then we set the Surface Texture to it.
107 // Otherwise, we may want to delete the Surface Texture to save memory.
108 public static void setBaseLayer(int layer) {
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700109 mBaseLayer = layer;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700110 // Don't do this for full screen mode.
111 if (mHTML5VideoView != null
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700112 && !mHTML5VideoView.isFullScreenMode()) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800113 int currentVideoLayerId = mHTML5VideoView.getVideoLayerId();
Teng-Hui Zhuc2b06d52012-05-09 14:13:52 -0700114 SurfaceTexture surfTexture =
115 HTML5VideoInline.getSurfaceTexture(currentVideoLayerId);
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -0700116 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 Zhuc2b06d52012-05-09 14:13:52 -0700121 playerState = HTML5VideoView.STATE_PREPARING;
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);
Andrei Popescu31d2aa12010-04-08 15:19:22 +0100128 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100129 }
Andrei Popescua41f97b2010-01-11 18:36:25 +0000130 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800131 }
Andrei Popescua41f97b2010-01-11 18:36:25 +0000132
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800133 // 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);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800137 }
138 }
139
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700140 public static void enterFullScreenVideo(int layerId, String url,
Jonathan Dixon3c909522012-02-28 18:45:06 +0000141 HTML5VideoViewProxy proxy, WebViewClassic webView) {
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700142 // Save the inline video info and inherit it in the full screen
143 int savePosition = 0;
Teng-Hui Zhuc2b06d52012-05-09 14:13:52 -0700144 boolean canSkipPrepare = false;
Teng-Hui Zhu6a9586b2012-06-15 11:22:23 -0700145 boolean forceStart = false;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700146 if (mHTML5VideoView != null) {
Teng-Hui Zhu2b64c5a2012-03-19 13:56:49 -0700147 // We don't allow enter full screen mode while the previous
148 // full screen video hasn't finished yet.
149 if (!mHTML5VideoView.fullScreenExited() && mHTML5VideoView.isFullScreenMode()) {
150 Log.w(LOGTAG, "Try to reenter the full screen mode");
151 return;
152 }
Teng-Hui Zhu6a9586b2012-06-15 11:22:23 -0700153 int playerState = mHTML5VideoView.getCurrentState();
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700154 // If we are playing the same video, then it is better to
155 // save the current position.
156 if (layerId == mHTML5VideoView.getVideoLayerId()) {
157 savePosition = mHTML5VideoView.getCurrentPosition();
Teng-Hui Zhuc2b06d52012-05-09 14:13:52 -0700158 canSkipPrepare = (playerState == HTML5VideoView.STATE_PREPARING
159 || playerState == HTML5VideoView.STATE_PREPARED
160 || playerState == HTML5VideoView.STATE_PLAYING)
161 && !mHTML5VideoView.isFullScreenMode();
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700162 }
Teng-Hui Zhuc2b06d52012-05-09 14:13:52 -0700163 if (!canSkipPrepare) {
164 mHTML5VideoView.reset();
Teng-Hui Zhu6a9586b2012-06-15 11:22:23 -0700165 } else {
166 forceStart = playerState == HTML5VideoView.STATE_PREPARING
167 || playerState == HTML5VideoView.STATE_PLAYING;
Teng-Hui Zhuc2b06d52012-05-09 14:13:52 -0700168 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700169 }
170 mHTML5VideoView = new HTML5VideoFullScreen(proxy.getContext(),
Teng-Hui Zhuc2b06d52012-05-09 14:13:52 -0700171 layerId, savePosition, canSkipPrepare);
Teng-Hui Zhu6a9586b2012-06-15 11:22:23 -0700172 mHTML5VideoView.setStartWhenPrepared(forceStart);
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700173 mCurrentProxy = proxy;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700174 mHTML5VideoView.setVideoURI(url, mCurrentProxy);
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700175 mHTML5VideoView.enterFullScreenVideoState(layerId, proxy, webView);
176 }
177
Teng-Hui Zhud7678a12012-01-16 15:33:09 -0800178 public static void exitFullScreenVideo(HTML5VideoViewProxy proxy,
Jonathan Dixon3c909522012-02-28 18:45:06 +0000179 WebViewClassic webView) {
Teng-Hui Zhud7678a12012-01-16 15:33:09 -0800180 if (!mHTML5VideoView.fullScreenExited() && mHTML5VideoView.isFullScreenMode()) {
181 WebChromeClient client = webView.getWebChromeClient();
182 if (client != null) {
183 client.onHideCustomView();
184 }
185 }
186 }
187
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800188 // This is on the UI thread.
189 // When native tell Java to play, we need to check whether or not it is
190 // still the same video by using videoLayerId and treat it differently.
191 public static void play(String url, int time, HTML5VideoViewProxy proxy,
192 WebChromeClient client, int videoLayerId) {
193 int currentVideoLayerId = -1;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700194 boolean backFromFullScreenMode = false;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700195 if (mHTML5VideoView != null) {
196 currentVideoLayerId = mHTML5VideoView.getVideoLayerId();
Teng-Hui Zhu1a88acb2011-06-01 13:28:41 -0700197 backFromFullScreenMode = mHTML5VideoView.fullScreenExited();
Teng-Hui Zhu96fae5e2012-01-18 15:25:51 -0800198
199 // When playing video back to back in full screen mode,
200 // javascript will switch the src and call play.
201 // In this case, we can just reuse the same full screen view,
202 // and play the video after prepared.
203 if (mHTML5VideoView.isFullScreenMode()
204 && !backFromFullScreenMode
205 && currentVideoLayerId != videoLayerId
206 && mCurrentProxy != proxy) {
207 mCurrentProxy = proxy;
208 mHTML5VideoView.setStartWhenPrepared(true);
209 mHTML5VideoView.setVideoURI(url, proxy);
210 mHTML5VideoView.reprepareData(proxy);
211 return;
212 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700213 }
214
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700215 boolean skipPrepare = false;
216 boolean createInlineView = false;
217 if (backFromFullScreenMode && currentVideoLayerId == videoLayerId) {
218 skipPrepare = true;
219 createInlineView = true;
220 } else if(backFromFullScreenMode
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -0700221 || currentVideoLayerId != videoLayerId
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700222 || HTML5VideoInline.surfaceTextureDeleted()) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800223 // Here, we handle the case when switching to a new video,
224 // either inside a WebView or across WebViews
225 // For switching videos within a WebView or across the WebView,
226 // we need to pause the old one and re-create a new media player
227 // inside the HTML5VideoView.
228 if (mHTML5VideoView != null) {
Teng-Hui Zhu22954d42011-04-07 17:13:18 -0700229 if (!backFromFullScreenMode) {
230 mHTML5VideoView.pauseAndDispatch(mCurrentProxy);
231 }
Teng-Hui Zhuc2b06d52012-05-09 14:13:52 -0700232 mHTML5VideoView.reset();
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800233 }
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700234 createInlineView = true;
235 }
236 if (createInlineView) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800237 mCurrentProxy = proxy;
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700238 mHTML5VideoView = new HTML5VideoInline(videoLayerId, time, skipPrepare);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800239
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700240 mHTML5VideoView.setVideoURI(url, mCurrentProxy);
241 mHTML5VideoView.prepareDataAndDisplayMode(proxy);
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700242 return;
243 }
244
245 if (mCurrentProxy == proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800246 // Here, we handle the case when we keep playing with one video
247 if (!mHTML5VideoView.isPlaying()) {
248 mHTML5VideoView.seekTo(time);
249 mHTML5VideoView.start();
250 }
251 } else if (mCurrentProxy != null) {
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700252 // Some other video is already playing. Notify the caller that
253 // its playback ended.
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100254 proxy.dispatchOnEnded();
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100255 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100256 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100257
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000258 public static boolean isPlaying(HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800259 return (mCurrentProxy == proxy && mHTML5VideoView != null
260 && mHTML5VideoView.isPlaying());
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000261 }
262
263 public static int getCurrentPosition() {
264 int currentPosMs = 0;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800265 if (mHTML5VideoView != null) {
266 currentPosMs = mHTML5VideoView.getCurrentPosition();
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000267 }
268 return currentPosMs;
269 }
270
Andrei Popescu290c34a2009-09-17 15:55:24 +0100271 public static void seek(int time, HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800272 if (mCurrentProxy == proxy && time >= 0 && mHTML5VideoView != null) {
273 mHTML5VideoView.seekTo(time);
Andrei Popescu290c34a2009-09-17 15:55:24 +0100274 }
275 }
276
277 public static void pause(HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800278 if (mCurrentProxy == proxy && mHTML5VideoView != null) {
279 mHTML5VideoView.pause();
Andrei Popescu290c34a2009-09-17 15:55:24 +0100280 }
281 }
Andrei Popescubf385d72009-09-18 18:59:52 +0100282
283 public static void onPrepared() {
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800284 if (!mHTML5VideoView.isFullScreenMode()) {
Teng-Hui Zhu05049672011-04-06 18:09:27 -0700285 mHTML5VideoView.start();
286 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800287 }
288
289 public static void end() {
Teng-Hui Zhu4dd9dc82012-05-10 17:20:19 -0700290 mHTML5VideoView.showControllerInFullScreen();
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800291 if (mCurrentProxy != null) {
292 if (isVideoSelfEnded)
293 mCurrentProxy.dispatchOnEnded();
294 else
295 mCurrentProxy.dispatchOnPaused();
296 }
297 isVideoSelfEnded = false;
Andrei Popescubf385d72009-09-18 18:59:52 +0100298 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100299 }
300
301 // A bunch event listeners for our VideoView
302 // MediaPlayer.OnPreparedListener
303 public void onPrepared(MediaPlayer mp) {
Andrei Popescubf385d72009-09-18 18:59:52 +0100304 VideoPlayer.onPrepared();
Andrei Popescu290c34a2009-09-17 15:55:24 +0100305 Message msg = Message.obtain(mWebCoreHandler, PREPARED);
306 Map<String, Object> map = new HashMap<String, Object>();
307 map.put("dur", new Integer(mp.getDuration()));
308 map.put("width", new Integer(mp.getVideoWidth()));
309 map.put("height", new Integer(mp.getVideoHeight()));
310 msg.obj = map;
311 mWebCoreHandler.sendMessage(msg);
312 }
313
314 // MediaPlayer.OnCompletionListener;
315 public void onCompletion(MediaPlayer mp) {
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100316 // The video ended by itself, so we need to
317 // send a message to the UI thread to dismiss
318 // the video view and to return to the WebView.
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700319 // arg1 == 1 means the video ends by itself.
320 sendMessage(obtainMessage(ENDED, 1, 0));
Andrei Popescu290c34a2009-09-17 15:55:24 +0100321 }
322
Andrei Popescu50c86232009-09-30 16:51:25 +0100323 // MediaPlayer.OnErrorListener
324 public boolean onError(MediaPlayer mp, int what, int extra) {
325 sendMessage(obtainMessage(ERROR));
326 return false;
327 }
328
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100329 public void dispatchOnEnded() {
Andrei Popescu290c34a2009-09-17 15:55:24 +0100330 Message msg = Message.obtain(mWebCoreHandler, ENDED);
331 mWebCoreHandler.sendMessage(msg);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100332 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100333
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700334 public void dispatchOnPaused() {
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700335 Message msg = Message.obtain(mWebCoreHandler, PAUSED);
336 mWebCoreHandler.sendMessage(msg);
337 }
338
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700339 public void dispatchOnStopFullScreen(boolean stillPlaying) {
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700340 Message msg = Message.obtain(mWebCoreHandler, STOPFULLSCREEN);
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700341 msg.arg1 = stillPlaying ? 1 : 0;
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700342 mWebCoreHandler.sendMessage(msg);
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700343 }
344
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800345 public void dispatchOnRestoreState() {
346 Message msg = Message.obtain(mWebCoreHandler, RESTORESTATE);
347 mWebCoreHandler.sendMessage(msg);
348 }
349
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000350 public void onTimeupdate() {
351 sendMessage(obtainMessage(TIMEUPDATE));
352 }
353
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800354 // When there is a frame ready from surface texture, we should tell WebView
355 // to refresh.
356 @Override
357 public void onFrameAvailable(SurfaceTexture surfaceTexture) {
358 // TODO: This should support partial invalidation too.
359 mWebView.invalidate();
360 }
361
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000362 // Handler for the messages from WebCore or Timer thread to the UI thread.
Andrei Popescu64b86a12009-09-15 20:34:18 +0100363 @Override
364 public void handleMessage(Message msg) {
365 // This executes on the UI thread.
366 switch (msg.what) {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100367 case PLAY: {
368 String url = (String) msg.obj;
369 WebChromeClient client = mWebView.getWebChromeClient();
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800370 int videoLayerID = msg.arg1;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100371 if (client != null) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800372 VideoPlayer.play(url, mSeekPosition, this, client, videoLayerID);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100373 }
374 break;
375 }
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700376 case ENTER_FULLSCREEN:{
377 String url = (String) msg.obj;
378 WebChromeClient client = mWebView.getWebChromeClient();
379 int videoLayerID = msg.arg1;
380 if (client != null) {
381 VideoPlayer.enterFullScreenVideo(videoLayerID, url, this, mWebView);
382 }
383 break;
384 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100385 case SEEK: {
386 Integer time = (Integer) msg.obj;
387 mSeekPosition = time;
388 VideoPlayer.seek(mSeekPosition, this);
389 break;
390 }
391 case PAUSE: {
392 VideoPlayer.pause(this);
393 break;
394 }
Andrei Popescu46a83b42009-10-23 13:49:46 +0100395 case ENDED:
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700396 if (msg.arg1 == 1)
397 VideoPlayer.isVideoSelfEnded = true;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800398 VideoPlayer.end();
399 break;
Andrei Popescu50c86232009-09-30 16:51:25 +0100400 case ERROR: {
401 WebChromeClient client = mWebView.getWebChromeClient();
402 if (client != null) {
403 client.onHideCustomView();
404 }
405 break;
406 }
407 case LOAD_DEFAULT_POSTER: {
408 WebChromeClient client = mWebView.getWebChromeClient();
409 if (client != null) {
410 doSetPoster(client.getDefaultVideoPoster());
411 }
412 break;
413 }
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000414 case TIMEUPDATE: {
415 if (VideoPlayer.isPlaying(this)) {
416 sendTimeupdate();
417 }
418 break;
419 }
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700420 case BUFFERING_START: {
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700421 VideoPlayer.setPlayerBuffering(true);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700422 break;
423 }
424 case BUFFERING_END: {
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700425 VideoPlayer.setPlayerBuffering(false);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700426 break;
427 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100428 }
429 }
430
Andrei Popescu64b86a12009-09-15 20:34:18 +0100431 // Everything below this comment executes on the WebCore thread, except for
432 // the EventHandler methods, which are called on the network thread.
433
434 // A helper class that knows how to download posters
435 private static final class PosterDownloader implements EventHandler {
436 // The request queue. This is static as we have one queue for all posters.
437 private static RequestQueue mRequestQueue;
438 private static int mQueueRefCount = 0;
439 // The poster URL
Ben Murdoch42509792011-02-18 12:34:35 +0000440 private URL mUrl;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100441 // The proxy we're doing this for.
442 private final HTML5VideoViewProxy mProxy;
443 // The poster bytes. We only touch this on the network thread.
444 private ByteArrayOutputStream mPosterBytes;
445 // The request handle. We only touch this on the WebCore thread.
446 private RequestHandle mRequestHandle;
447 // The response status code.
448 private int mStatusCode;
449 // The response headers.
450 private Headers mHeaders;
451 // The handler to handle messages on the WebCore thread.
452 private Handler mHandler;
453
454 public PosterDownloader(String url, HTML5VideoViewProxy proxy) {
Ben Murdoch42509792011-02-18 12:34:35 +0000455 try {
456 mUrl = new URL(url);
457 } catch (MalformedURLException e) {
458 mUrl = null;
459 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100460 mProxy = proxy;
461 mHandler = new Handler();
462 }
463 // Start the download. Called on WebCore thread.
464 public void start() {
465 retainQueue();
Ben Murdoch42509792011-02-18 12:34:35 +0000466
467 if (mUrl == null) {
468 return;
469 }
470
471 // Only support downloading posters over http/https.
472 // FIXME: Add support for other schemes. WebKit seems able to load
473 // posters over other schemes e.g. file://, but gets the dimensions wrong.
474 String protocol = mUrl.getProtocol();
475 if ("http".equals(protocol) || "https".equals(protocol)) {
476 mRequestHandle = mRequestQueue.queueRequest(mUrl.toString(), "GET", null,
477 this, null, 0);
478 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100479 }
480 // Cancel the download if active and release the queue. Called on WebCore thread.
481 public void cancelAndReleaseQueue() {
482 if (mRequestHandle != null) {
483 mRequestHandle.cancel();
484 mRequestHandle = null;
485 }
486 releaseQueue();
487 }
488 // EventHandler methods. Executed on the network thread.
489 public void status(int major_version,
490 int minor_version,
491 int code,
492 String reason_phrase) {
493 mStatusCode = code;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100494 }
495
Andrei Popescu64b86a12009-09-15 20:34:18 +0100496 public void headers(Headers headers) {
497 mHeaders = headers;
498 }
499
500 public void data(byte[] data, int len) {
501 if (mPosterBytes == null) {
502 mPosterBytes = new ByteArrayOutputStream();
503 }
504 mPosterBytes.write(data, 0, len);
505 }
506
507 public void endData() {
508 if (mStatusCode == 200) {
509 if (mPosterBytes.size() > 0) {
510 Bitmap poster = BitmapFactory.decodeByteArray(
511 mPosterBytes.toByteArray(), 0, mPosterBytes.size());
Andrei Popescu50c86232009-09-30 16:51:25 +0100512 mProxy.doSetPoster(poster);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100513 }
514 cleanup();
515 } else if (mStatusCode >= 300 && mStatusCode < 400) {
516 // We have a redirect.
Ben Murdoch42509792011-02-18 12:34:35 +0000517 try {
518 mUrl = new URL(mHeaders.getLocation());
519 } catch (MalformedURLException e) {
520 mUrl = null;
521 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100522 if (mUrl != null) {
523 mHandler.post(new Runnable() {
524 public void run() {
525 if (mRequestHandle != null) {
Ben Murdoch42509792011-02-18 12:34:35 +0000526 mRequestHandle.setupRedirect(mUrl.toString(), mStatusCode,
Andrei Popescu64b86a12009-09-15 20:34:18 +0100527 new HashMap<String, String>());
528 }
529 }
530 });
531 }
532 }
533 }
534
535 public void certificate(SslCertificate certificate) {
536 // Don't care.
537 }
538
539 public void error(int id, String description) {
540 cleanup();
541 }
542
543 public boolean handleSslErrorRequest(SslError error) {
544 // Don't care. If this happens, data() will never be called so
545 // mPosterBytes will never be created, so no need to call cleanup.
546 return false;
547 }
548 // Tears down the poster bytes stream. Called on network thread.
549 private void cleanup() {
550 if (mPosterBytes != null) {
551 try {
552 mPosterBytes.close();
553 } catch (IOException ignored) {
554 // Ignored.
555 } finally {
556 mPosterBytes = null;
557 }
558 }
559 }
560
561 // Queue management methods. Called on WebCore thread.
562 private void retainQueue() {
563 if (mRequestQueue == null) {
564 mRequestQueue = new RequestQueue(mProxy.getContext());
565 }
566 mQueueRefCount++;
567 }
568
569 private void releaseQueue() {
570 if (mQueueRefCount == 0) {
571 return;
572 }
573 if (--mQueueRefCount == 0) {
574 mRequestQueue.shutdown();
575 mRequestQueue = null;
576 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100577 }
578 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100579
580 /**
581 * Private constructor.
Andrei Popescu290c34a2009-09-17 15:55:24 +0100582 * @param webView is the WebView that hosts the video.
583 * @param nativePtr is the C++ pointer to the MediaPlayerPrivate object.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100584 */
Jonathan Dixon3c909522012-02-28 18:45:06 +0000585 private HTML5VideoViewProxy(WebViewClassic webView, int nativePtr) {
Andrei Popescu6fa29582009-06-19 14:54:09 +0100586 // This handler is for the main (UI) thread.
587 super(Looper.getMainLooper());
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100588 // Save the WebView object.
589 mWebView = webView;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800590 // Pass Proxy into webview, such that every time we have a setBaseLayer
591 // call, we tell this Proxy to call the native to update the layer tree
592 // for the Video Layer's surface texture info
593 mWebView.setHTML5VideoViewProxy(this);
Andrei Popescu290c34a2009-09-17 15:55:24 +0100594 // Save the native ptr
595 mNativePointer = nativePtr;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100596 // create the message handler for this thread
597 createWebCoreHandler();
Andrei Popescu6fa29582009-06-19 14:54:09 +0100598 }
599
Andrei Popescu64b86a12009-09-15 20:34:18 +0100600 private void createWebCoreHandler() {
601 mWebCoreHandler = new Handler() {
602 @Override
603 public void handleMessage(Message msg) {
604 switch (msg.what) {
Andrei Popescu290c34a2009-09-17 15:55:24 +0100605 case PREPARED: {
606 Map<String, Object> map = (Map<String, Object>) msg.obj;
607 Integer duration = (Integer) map.get("dur");
608 Integer width = (Integer) map.get("width");
609 Integer height = (Integer) map.get("height");
610 nativeOnPrepared(duration.intValue(), width.intValue(),
611 height.intValue(), mNativePointer);
612 break;
613 }
614 case ENDED:
Ben Murdochff19d192011-01-17 18:08:56 +0000615 mSeekPosition = 0;
Andrei Popescu290c34a2009-09-17 15:55:24 +0100616 nativeOnEnded(mNativePointer);
617 break;
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700618 case PAUSED:
619 nativeOnPaused(mNativePointer);
620 break;
Andrei Popescu50c86232009-09-30 16:51:25 +0100621 case POSTER_FETCHED:
622 Bitmap poster = (Bitmap) msg.obj;
623 nativeOnPosterFetched(poster, mNativePointer);
624 break;
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000625 case TIMEUPDATE:
626 nativeOnTimeupdate(msg.arg1, mNativePointer);
627 break;
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700628 case STOPFULLSCREEN:
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700629 nativeOnStopFullscreen(msg.arg1, mNativePointer);
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700630 break;
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800631 case RESTORESTATE:
632 nativeOnRestoreState(mNativePointer);
633 break;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100634 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100635 }
636 };
Andrei Popescu6fa29582009-06-19 14:54:09 +0100637 }
638
Andrei Popescu64b86a12009-09-15 20:34:18 +0100639 private void doSetPoster(Bitmap poster) {
640 if (poster == null) {
641 return;
642 }
Andrei Popescu50c86232009-09-30 16:51:25 +0100643 // Save a ref to the bitmap and send it over to the WebCore thread.
644 mPoster = poster;
645 Message msg = Message.obtain(mWebCoreHandler, POSTER_FETCHED);
646 msg.obj = poster;
647 mWebCoreHandler.sendMessage(msg);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100648 }
649
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000650 private void sendTimeupdate() {
651 Message msg = Message.obtain(mWebCoreHandler, TIMEUPDATE);
652 msg.arg1 = VideoPlayer.getCurrentPosition();
653 mWebCoreHandler.sendMessage(msg);
654 }
655
Andrei Popescu64b86a12009-09-15 20:34:18 +0100656 public Context getContext() {
657 return mWebView.getContext();
658 }
659
660 // The public methods below are all called from WebKit only.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100661 /**
662 * Play a video stream.
663 * @param url is the URL of the video stream.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100664 */
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800665 public void play(String url, int position, int videoLayerID) {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100666 if (url == null) {
667 return;
668 }
Ben Murdochff19d192011-01-17 18:08:56 +0000669
670 if (position > 0) {
671 seek(position);
672 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100673 Message message = obtainMessage(PLAY);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800674 message.arg1 = videoLayerID;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100675 message.obj = url;
Andrei Popescu6fa29582009-06-19 14:54:09 +0100676 sendMessage(message);
677 }
678
Andrei Popescu64b86a12009-09-15 20:34:18 +0100679 /**
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700680 * Play a video stream in full screen mode.
681 * @param url is the URL of the video stream.
682 */
683 public void enterFullscreenForVideoLayer(String url, int videoLayerID) {
684 if (url == null) {
685 return;
686 }
687
688 Message message = obtainMessage(ENTER_FULLSCREEN);
689 message.arg1 = videoLayerID;
690 message.obj = url;
691 sendMessage(message);
692 }
693
694 /**
Andrei Popescu290c34a2009-09-17 15:55:24 +0100695 * Seek into the video stream.
696 * @param time is the position in the video stream.
697 */
698 public void seek(int time) {
699 Message message = obtainMessage(SEEK);
700 message.obj = new Integer(time);
701 sendMessage(message);
702 }
703
704 /**
705 * Pause the playback.
706 */
707 public void pause() {
708 Message message = obtainMessage(PAUSE);
709 sendMessage(message);
710 }
711
712 /**
Andrei Popescu50c86232009-09-30 16:51:25 +0100713 * Tear down this proxy object.
Andrei Popescu64b86a12009-09-15 20:34:18 +0100714 */
Andrei Popescu50c86232009-09-30 16:51:25 +0100715 public void teardown() {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100716 // This is called by the C++ MediaPlayerPrivate dtor.
717 // Cancel any active poster download.
718 if (mPosterDownloader != null) {
719 mPosterDownloader.cancelAndReleaseQueue();
720 }
Andrei Popescu50c86232009-09-30 16:51:25 +0100721 mNativePointer = 0;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100722 }
723
724 /**
725 * Load the poster image.
726 * @param url is the URL of the poster image.
727 */
728 public void loadPoster(String url) {
729 if (url == null) {
Andrei Popescu50c86232009-09-30 16:51:25 +0100730 Message message = obtainMessage(LOAD_DEFAULT_POSTER);
731 sendMessage(message);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100732 return;
733 }
734 // Cancel any active poster download.
735 if (mPosterDownloader != null) {
736 mPosterDownloader.cancelAndReleaseQueue();
737 }
738 // Load the poster asynchronously
739 mPosterDownloader = new PosterDownloader(url, this);
740 mPosterDownloader.start();
Patrick Scott0a5ce012009-07-02 08:56:10 -0400741 }
742
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700743 // These three function are called from UI thread only by WebView.
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800744 public void setBaseLayer(int layer) {
745 VideoPlayer.setBaseLayer(layer);
746 }
747
748 public void pauseAndDispatch() {
749 VideoPlayer.pauseAndDispatch();
750 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700751
752 public void enterFullScreenVideo(int layerId, String url) {
753 VideoPlayer.enterFullScreenVideo(layerId, url, this, mWebView);
754 }
755
Teng-Hui Zhud7678a12012-01-16 15:33:09 -0800756 public void exitFullScreenVideo() {
757 VideoPlayer.exitFullScreenVideo(this, mWebView);
758 }
759
Andrei Popescu6fa29582009-06-19 14:54:09 +0100760 /**
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100761 * The factory for HTML5VideoViewProxy instances.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100762 * @param webViewCore is the WebViewCore that is requesting the proxy.
763 *
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100764 * @return a new HTML5VideoViewProxy object.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100765 */
Andrei Popescu290c34a2009-09-17 15:55:24 +0100766 public static HTML5VideoViewProxy getInstance(WebViewCore webViewCore, int nativePtr) {
Jonathan Dixona6c4d8e2012-03-08 17:55:31 +0000767 return new HTML5VideoViewProxy(webViewCore.getWebViewClassic(), nativePtr);
Andrei Popescu6fa29582009-06-19 14:54:09 +0100768 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100769
Jonathan Dixon3c909522012-02-28 18:45:06 +0000770 /* package */ WebViewClassic getWebView() {
Ben Murdoch1708ad52010-11-11 15:56:16 +0000771 return mWebView;
772 }
773
Andrei Popescu290c34a2009-09-17 15:55:24 +0100774 private native void nativeOnPrepared(int duration, int width, int height, int nativePointer);
775 private native void nativeOnEnded(int nativePointer);
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700776 private native void nativeOnPaused(int nativePointer);
Andrei Popescu50c86232009-09-30 16:51:25 +0100777 private native void nativeOnPosterFetched(Bitmap poster, int nativePointer);
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000778 private native void nativeOnTimeupdate(int position, int nativePointer);
Teng-Hui Zhuc3a28582012-05-31 17:36:17 -0700779 private native void nativeOnStopFullscreen(int stillPlaying, int nativePointer);
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800780 private native void nativeOnRestoreState(int nativePointer);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800781 private native static boolean nativeSendSurfaceTexture(SurfaceTexture texture,
782 int baseLayer, int videoLayerId, int textureName,
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700783 int playerState);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700784
785 @Override
786 public boolean onInfo(MediaPlayer mp, int what, int extra) {
787 if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
788 sendMessage(obtainMessage(BUFFERING_START, what, extra));
789 } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
790 sendMessage(obtainMessage(BUFFERING_END, what, extra));
791 }
792 return false;
793 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100794}