blob: 5fa4badda334e149dfbc0b53d658eef942587ffd [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;
Jonathan Dixon3c909522012-02-28 18:45:06 +000078 // The WebViewClassic instance that created this view.
79 private WebViewClassic 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,
Jonathan Dixon3c909522012-02-28 18:45:06 +0000145 HTML5VideoViewProxy proxy, WebViewClassic webView) {
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700146 // 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) {
Teng-Hui Zhu2b64c5a2012-03-19 13:56:49 -0700149 // We don't allow enter full screen mode while the previous
150 // full screen video hasn't finished yet.
151 if (!mHTML5VideoView.fullScreenExited() && mHTML5VideoView.isFullScreenMode()) {
152 Log.w(LOGTAG, "Try to reenter the full screen mode");
153 return;
154 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700155 // If we are playing the same video, then it is better to
156 // save the current position.
157 if (layerId == mHTML5VideoView.getVideoLayerId()) {
158 savePosition = mHTML5VideoView.getCurrentPosition();
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700159 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700160 mHTML5VideoView.release();
161 }
162 mHTML5VideoView = new HTML5VideoFullScreen(proxy.getContext(),
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800163 layerId, savePosition);
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700164 mCurrentProxy = proxy;
165
166 mHTML5VideoView.setVideoURI(url, mCurrentProxy);
167
168 mHTML5VideoView.enterFullScreenVideoState(layerId, proxy, webView);
169 }
170
Teng-Hui Zhud7678a12012-01-16 15:33:09 -0800171 public static void exitFullScreenVideo(HTML5VideoViewProxy proxy,
Jonathan Dixon3c909522012-02-28 18:45:06 +0000172 WebViewClassic webView) {
Teng-Hui Zhud7678a12012-01-16 15:33:09 -0800173 if (!mHTML5VideoView.fullScreenExited() && mHTML5VideoView.isFullScreenMode()) {
174 WebChromeClient client = webView.getWebChromeClient();
175 if (client != null) {
176 client.onHideCustomView();
177 }
178 }
179 }
180
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800181 // This is on the UI thread.
182 // When native tell Java to play, we need to check whether or not it is
183 // still the same video by using videoLayerId and treat it differently.
184 public static void play(String url, int time, HTML5VideoViewProxy proxy,
185 WebChromeClient client, int videoLayerId) {
186 int currentVideoLayerId = -1;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700187 boolean backFromFullScreenMode = false;
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700188 if (mHTML5VideoView != null) {
189 currentVideoLayerId = mHTML5VideoView.getVideoLayerId();
Teng-Hui Zhu1a88acb2011-06-01 13:28:41 -0700190 backFromFullScreenMode = mHTML5VideoView.fullScreenExited();
Teng-Hui Zhu96fae5e2012-01-18 15:25:51 -0800191
192 // When playing video back to back in full screen mode,
193 // javascript will switch the src and call play.
194 // In this case, we can just reuse the same full screen view,
195 // and play the video after prepared.
196 if (mHTML5VideoView.isFullScreenMode()
197 && !backFromFullScreenMode
198 && currentVideoLayerId != videoLayerId
199 && mCurrentProxy != proxy) {
200 mCurrentProxy = proxy;
201 mHTML5VideoView.setStartWhenPrepared(true);
202 mHTML5VideoView.setVideoURI(url, proxy);
203 mHTML5VideoView.reprepareData(proxy);
204 return;
205 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700206 }
207
208 if (backFromFullScreenMode
Teng-Hui Zhu3fafd392011-05-31 15:15:31 -0700209 || currentVideoLayerId != videoLayerId
210 || mHTML5VideoView.surfaceTextureDeleted()) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800211 // Here, we handle the case when switching to a new video,
212 // either inside a WebView or across WebViews
213 // For switching videos within a WebView or across the WebView,
214 // we need to pause the old one and re-create a new media player
215 // inside the HTML5VideoView.
216 if (mHTML5VideoView != null) {
Teng-Hui Zhu22954d42011-04-07 17:13:18 -0700217 if (!backFromFullScreenMode) {
218 mHTML5VideoView.pauseAndDispatch(mCurrentProxy);
219 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800220 // release the media player to avoid finalize error
221 mHTML5VideoView.release();
222 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800223 mCurrentProxy = proxy;
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800224 mHTML5VideoView = new HTML5VideoInline(videoLayerId, time);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800225
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700226 mHTML5VideoView.setVideoURI(url, mCurrentProxy);
227 mHTML5VideoView.prepareDataAndDisplayMode(proxy);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800228 } else if (mCurrentProxy == proxy) {
229 // Here, we handle the case when we keep playing with one video
230 if (!mHTML5VideoView.isPlaying()) {
231 mHTML5VideoView.seekTo(time);
232 mHTML5VideoView.start();
233 }
234 } else if (mCurrentProxy != null) {
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700235 // Some other video is already playing. Notify the caller that
236 // its playback ended.
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100237 proxy.dispatchOnEnded();
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100238 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100239 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100240
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000241 public static boolean isPlaying(HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800242 return (mCurrentProxy == proxy && mHTML5VideoView != null
243 && mHTML5VideoView.isPlaying());
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000244 }
245
246 public static int getCurrentPosition() {
247 int currentPosMs = 0;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800248 if (mHTML5VideoView != null) {
249 currentPosMs = mHTML5VideoView.getCurrentPosition();
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000250 }
251 return currentPosMs;
252 }
253
Andrei Popescu290c34a2009-09-17 15:55:24 +0100254 public static void seek(int time, HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800255 if (mCurrentProxy == proxy && time >= 0 && mHTML5VideoView != null) {
256 mHTML5VideoView.seekTo(time);
Andrei Popescu290c34a2009-09-17 15:55:24 +0100257 }
258 }
259
260 public static void pause(HTML5VideoViewProxy proxy) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800261 if (mCurrentProxy == proxy && mHTML5VideoView != null) {
262 mHTML5VideoView.pause();
Andrei Popescu290c34a2009-09-17 15:55:24 +0100263 }
264 }
Andrei Popescubf385d72009-09-18 18:59:52 +0100265
266 public static void onPrepared() {
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800267 if (!mHTML5VideoView.isFullScreenMode()) {
Teng-Hui Zhu05049672011-04-06 18:09:27 -0700268 mHTML5VideoView.start();
269 }
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700270 if (mBaseLayer != 0) {
271 setBaseLayer(mBaseLayer);
272 }
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800273 }
274
275 public static void end() {
276 if (mCurrentProxy != null) {
277 if (isVideoSelfEnded)
278 mCurrentProxy.dispatchOnEnded();
279 else
280 mCurrentProxy.dispatchOnPaused();
281 }
282 isVideoSelfEnded = false;
Andrei Popescubf385d72009-09-18 18:59:52 +0100283 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100284 }
285
286 // A bunch event listeners for our VideoView
287 // MediaPlayer.OnPreparedListener
288 public void onPrepared(MediaPlayer mp) {
Andrei Popescubf385d72009-09-18 18:59:52 +0100289 VideoPlayer.onPrepared();
Andrei Popescu290c34a2009-09-17 15:55:24 +0100290 Message msg = Message.obtain(mWebCoreHandler, PREPARED);
291 Map<String, Object> map = new HashMap<String, Object>();
292 map.put("dur", new Integer(mp.getDuration()));
293 map.put("width", new Integer(mp.getVideoWidth()));
294 map.put("height", new Integer(mp.getVideoHeight()));
295 msg.obj = map;
296 mWebCoreHandler.sendMessage(msg);
297 }
298
299 // MediaPlayer.OnCompletionListener;
300 public void onCompletion(MediaPlayer mp) {
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100301 // The video ended by itself, so we need to
302 // send a message to the UI thread to dismiss
303 // the video view and to return to the WebView.
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700304 // arg1 == 1 means the video ends by itself.
305 sendMessage(obtainMessage(ENDED, 1, 0));
Andrei Popescu290c34a2009-09-17 15:55:24 +0100306 }
307
Andrei Popescu50c86232009-09-30 16:51:25 +0100308 // MediaPlayer.OnErrorListener
309 public boolean onError(MediaPlayer mp, int what, int extra) {
310 sendMessage(obtainMessage(ERROR));
311 return false;
312 }
313
Andrei Popescuc4fbceb2010-04-14 13:30:23 +0100314 public void dispatchOnEnded() {
Andrei Popescu290c34a2009-09-17 15:55:24 +0100315 Message msg = Message.obtain(mWebCoreHandler, ENDED);
316 mWebCoreHandler.sendMessage(msg);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100317 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100318
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700319 public void dispatchOnPaused() {
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700320 Message msg = Message.obtain(mWebCoreHandler, PAUSED);
321 mWebCoreHandler.sendMessage(msg);
322 }
323
324 public void dispatchOnStopFullScreen() {
325 Message msg = Message.obtain(mWebCoreHandler, STOPFULLSCREEN);
326 mWebCoreHandler.sendMessage(msg);
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700327 }
328
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800329 public void dispatchOnRestoreState() {
330 Message msg = Message.obtain(mWebCoreHandler, RESTORESTATE);
331 mWebCoreHandler.sendMessage(msg);
332 }
333
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000334 public void onTimeupdate() {
335 sendMessage(obtainMessage(TIMEUPDATE));
336 }
337
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800338 // When there is a frame ready from surface texture, we should tell WebView
339 // to refresh.
340 @Override
341 public void onFrameAvailable(SurfaceTexture surfaceTexture) {
342 // TODO: This should support partial invalidation too.
343 mWebView.invalidate();
344 }
345
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000346 // Handler for the messages from WebCore or Timer thread to the UI thread.
Andrei Popescu64b86a12009-09-15 20:34:18 +0100347 @Override
348 public void handleMessage(Message msg) {
349 // This executes on the UI thread.
350 switch (msg.what) {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100351 case PLAY: {
352 String url = (String) msg.obj;
353 WebChromeClient client = mWebView.getWebChromeClient();
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800354 int videoLayerID = msg.arg1;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100355 if (client != null) {
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800356 VideoPlayer.play(url, mSeekPosition, this, client, videoLayerID);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100357 }
358 break;
359 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100360 case SEEK: {
361 Integer time = (Integer) msg.obj;
362 mSeekPosition = time;
363 VideoPlayer.seek(mSeekPosition, this);
364 break;
365 }
366 case PAUSE: {
367 VideoPlayer.pause(this);
368 break;
369 }
Andrei Popescu46a83b42009-10-23 13:49:46 +0100370 case ENDED:
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700371 if (msg.arg1 == 1)
372 VideoPlayer.isVideoSelfEnded = true;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800373 VideoPlayer.end();
374 break;
Andrei Popescu50c86232009-09-30 16:51:25 +0100375 case ERROR: {
376 WebChromeClient client = mWebView.getWebChromeClient();
377 if (client != null) {
378 client.onHideCustomView();
379 }
380 break;
381 }
382 case LOAD_DEFAULT_POSTER: {
383 WebChromeClient client = mWebView.getWebChromeClient();
384 if (client != null) {
385 doSetPoster(client.getDefaultVideoPoster());
386 }
387 break;
388 }
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000389 case TIMEUPDATE: {
390 if (VideoPlayer.isPlaying(this)) {
391 sendTimeupdate();
392 }
393 break;
394 }
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700395 case BUFFERING_START: {
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700396 VideoPlayer.setPlayerBuffering(true);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700397 break;
398 }
399 case BUFFERING_END: {
Teng-Hui Zhuf4d4e9e2011-03-30 14:39:56 -0700400 VideoPlayer.setPlayerBuffering(false);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700401 break;
402 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100403 }
404 }
405
Andrei Popescu64b86a12009-09-15 20:34:18 +0100406 // Everything below this comment executes on the WebCore thread, except for
407 // the EventHandler methods, which are called on the network thread.
408
409 // A helper class that knows how to download posters
410 private static final class PosterDownloader implements EventHandler {
411 // The request queue. This is static as we have one queue for all posters.
412 private static RequestQueue mRequestQueue;
413 private static int mQueueRefCount = 0;
414 // The poster URL
Ben Murdoch42509792011-02-18 12:34:35 +0000415 private URL mUrl;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100416 // The proxy we're doing this for.
417 private final HTML5VideoViewProxy mProxy;
418 // The poster bytes. We only touch this on the network thread.
419 private ByteArrayOutputStream mPosterBytes;
420 // The request handle. We only touch this on the WebCore thread.
421 private RequestHandle mRequestHandle;
422 // The response status code.
423 private int mStatusCode;
424 // The response headers.
425 private Headers mHeaders;
426 // The handler to handle messages on the WebCore thread.
427 private Handler mHandler;
428
429 public PosterDownloader(String url, HTML5VideoViewProxy proxy) {
Ben Murdoch42509792011-02-18 12:34:35 +0000430 try {
431 mUrl = new URL(url);
432 } catch (MalformedURLException e) {
433 mUrl = null;
434 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100435 mProxy = proxy;
436 mHandler = new Handler();
437 }
438 // Start the download. Called on WebCore thread.
439 public void start() {
440 retainQueue();
Ben Murdoch42509792011-02-18 12:34:35 +0000441
442 if (mUrl == null) {
443 return;
444 }
445
446 // Only support downloading posters over http/https.
447 // FIXME: Add support for other schemes. WebKit seems able to load
448 // posters over other schemes e.g. file://, but gets the dimensions wrong.
449 String protocol = mUrl.getProtocol();
450 if ("http".equals(protocol) || "https".equals(protocol)) {
451 mRequestHandle = mRequestQueue.queueRequest(mUrl.toString(), "GET", null,
452 this, null, 0);
453 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100454 }
455 // Cancel the download if active and release the queue. Called on WebCore thread.
456 public void cancelAndReleaseQueue() {
457 if (mRequestHandle != null) {
458 mRequestHandle.cancel();
459 mRequestHandle = null;
460 }
461 releaseQueue();
462 }
463 // EventHandler methods. Executed on the network thread.
464 public void status(int major_version,
465 int minor_version,
466 int code,
467 String reason_phrase) {
468 mStatusCode = code;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100469 }
470
Andrei Popescu64b86a12009-09-15 20:34:18 +0100471 public void headers(Headers headers) {
472 mHeaders = headers;
473 }
474
475 public void data(byte[] data, int len) {
476 if (mPosterBytes == null) {
477 mPosterBytes = new ByteArrayOutputStream();
478 }
479 mPosterBytes.write(data, 0, len);
480 }
481
482 public void endData() {
483 if (mStatusCode == 200) {
484 if (mPosterBytes.size() > 0) {
485 Bitmap poster = BitmapFactory.decodeByteArray(
486 mPosterBytes.toByteArray(), 0, mPosterBytes.size());
Andrei Popescu50c86232009-09-30 16:51:25 +0100487 mProxy.doSetPoster(poster);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100488 }
489 cleanup();
490 } else if (mStatusCode >= 300 && mStatusCode < 400) {
491 // We have a redirect.
Ben Murdoch42509792011-02-18 12:34:35 +0000492 try {
493 mUrl = new URL(mHeaders.getLocation());
494 } catch (MalformedURLException e) {
495 mUrl = null;
496 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100497 if (mUrl != null) {
498 mHandler.post(new Runnable() {
499 public void run() {
500 if (mRequestHandle != null) {
Ben Murdoch42509792011-02-18 12:34:35 +0000501 mRequestHandle.setupRedirect(mUrl.toString(), mStatusCode,
Andrei Popescu64b86a12009-09-15 20:34:18 +0100502 new HashMap<String, String>());
503 }
504 }
505 });
506 }
507 }
508 }
509
510 public void certificate(SslCertificate certificate) {
511 // Don't care.
512 }
513
514 public void error(int id, String description) {
515 cleanup();
516 }
517
518 public boolean handleSslErrorRequest(SslError error) {
519 // Don't care. If this happens, data() will never be called so
520 // mPosterBytes will never be created, so no need to call cleanup.
521 return false;
522 }
523 // Tears down the poster bytes stream. Called on network thread.
524 private void cleanup() {
525 if (mPosterBytes != null) {
526 try {
527 mPosterBytes.close();
528 } catch (IOException ignored) {
529 // Ignored.
530 } finally {
531 mPosterBytes = null;
532 }
533 }
534 }
535
536 // Queue management methods. Called on WebCore thread.
537 private void retainQueue() {
538 if (mRequestQueue == null) {
539 mRequestQueue = new RequestQueue(mProxy.getContext());
540 }
541 mQueueRefCount++;
542 }
543
544 private void releaseQueue() {
545 if (mQueueRefCount == 0) {
546 return;
547 }
548 if (--mQueueRefCount == 0) {
549 mRequestQueue.shutdown();
550 mRequestQueue = null;
551 }
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100552 }
553 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100554
555 /**
556 * Private constructor.
Andrei Popescu290c34a2009-09-17 15:55:24 +0100557 * @param webView is the WebView that hosts the video.
558 * @param nativePtr is the C++ pointer to the MediaPlayerPrivate object.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100559 */
Jonathan Dixon3c909522012-02-28 18:45:06 +0000560 private HTML5VideoViewProxy(WebViewClassic webView, int nativePtr) {
Andrei Popescu6fa29582009-06-19 14:54:09 +0100561 // This handler is for the main (UI) thread.
562 super(Looper.getMainLooper());
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100563 // Save the WebView object.
564 mWebView = webView;
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800565 // Pass Proxy into webview, such that every time we have a setBaseLayer
566 // call, we tell this Proxy to call the native to update the layer tree
567 // for the Video Layer's surface texture info
568 mWebView.setHTML5VideoViewProxy(this);
Andrei Popescu290c34a2009-09-17 15:55:24 +0100569 // Save the native ptr
570 mNativePointer = nativePtr;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100571 // create the message handler for this thread
572 createWebCoreHandler();
Andrei Popescu6fa29582009-06-19 14:54:09 +0100573 }
574
Andrei Popescu64b86a12009-09-15 20:34:18 +0100575 private void createWebCoreHandler() {
576 mWebCoreHandler = new Handler() {
577 @Override
578 public void handleMessage(Message msg) {
579 switch (msg.what) {
Andrei Popescu290c34a2009-09-17 15:55:24 +0100580 case PREPARED: {
581 Map<String, Object> map = (Map<String, Object>) msg.obj;
582 Integer duration = (Integer) map.get("dur");
583 Integer width = (Integer) map.get("width");
584 Integer height = (Integer) map.get("height");
585 nativeOnPrepared(duration.intValue(), width.intValue(),
586 height.intValue(), mNativePointer);
587 break;
588 }
589 case ENDED:
Ben Murdochff19d192011-01-17 18:08:56 +0000590 mSeekPosition = 0;
Andrei Popescu290c34a2009-09-17 15:55:24 +0100591 nativeOnEnded(mNativePointer);
592 break;
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700593 case PAUSED:
594 nativeOnPaused(mNativePointer);
595 break;
Andrei Popescu50c86232009-09-30 16:51:25 +0100596 case POSTER_FETCHED:
597 Bitmap poster = (Bitmap) msg.obj;
598 nativeOnPosterFetched(poster, mNativePointer);
599 break;
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000600 case TIMEUPDATE:
601 nativeOnTimeupdate(msg.arg1, mNativePointer);
602 break;
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700603 case STOPFULLSCREEN:
604 nativeOnStopFullscreen(mNativePointer);
605 break;
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800606 case RESTORESTATE:
607 nativeOnRestoreState(mNativePointer);
608 break;
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100609 }
Andrei Popescu64b86a12009-09-15 20:34:18 +0100610 }
611 };
Andrei Popescu6fa29582009-06-19 14:54:09 +0100612 }
613
Andrei Popescu64b86a12009-09-15 20:34:18 +0100614 private void doSetPoster(Bitmap poster) {
615 if (poster == null) {
616 return;
617 }
Andrei Popescu50c86232009-09-30 16:51:25 +0100618 // Save a ref to the bitmap and send it over to the WebCore thread.
619 mPoster = poster;
620 Message msg = Message.obtain(mWebCoreHandler, POSTER_FETCHED);
621 msg.obj = poster;
622 mWebCoreHandler.sendMessage(msg);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100623 }
624
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000625 private void sendTimeupdate() {
626 Message msg = Message.obtain(mWebCoreHandler, TIMEUPDATE);
627 msg.arg1 = VideoPlayer.getCurrentPosition();
628 mWebCoreHandler.sendMessage(msg);
629 }
630
Andrei Popescu64b86a12009-09-15 20:34:18 +0100631 public Context getContext() {
632 return mWebView.getContext();
633 }
634
635 // The public methods below are all called from WebKit only.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100636 /**
637 * Play a video stream.
638 * @param url is the URL of the video stream.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100639 */
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800640 public void play(String url, int position, int videoLayerID) {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100641 if (url == null) {
642 return;
643 }
Ben Murdochff19d192011-01-17 18:08:56 +0000644
645 if (position > 0) {
646 seek(position);
647 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100648 Message message = obtainMessage(PLAY);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800649 message.arg1 = videoLayerID;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100650 message.obj = url;
Andrei Popescu6fa29582009-06-19 14:54:09 +0100651 sendMessage(message);
652 }
653
Andrei Popescu64b86a12009-09-15 20:34:18 +0100654 /**
Andrei Popescu290c34a2009-09-17 15:55:24 +0100655 * Seek into the video stream.
656 * @param time is the position in the video stream.
657 */
658 public void seek(int time) {
659 Message message = obtainMessage(SEEK);
660 message.obj = new Integer(time);
661 sendMessage(message);
662 }
663
664 /**
665 * Pause the playback.
666 */
667 public void pause() {
668 Message message = obtainMessage(PAUSE);
669 sendMessage(message);
670 }
671
672 /**
Andrei Popescu50c86232009-09-30 16:51:25 +0100673 * Tear down this proxy object.
Andrei Popescu64b86a12009-09-15 20:34:18 +0100674 */
Andrei Popescu50c86232009-09-30 16:51:25 +0100675 public void teardown() {
Andrei Popescu64b86a12009-09-15 20:34:18 +0100676 // This is called by the C++ MediaPlayerPrivate dtor.
677 // Cancel any active poster download.
678 if (mPosterDownloader != null) {
679 mPosterDownloader.cancelAndReleaseQueue();
680 }
Andrei Popescu50c86232009-09-30 16:51:25 +0100681 mNativePointer = 0;
Andrei Popescu64b86a12009-09-15 20:34:18 +0100682 }
683
684 /**
685 * Load the poster image.
686 * @param url is the URL of the poster image.
687 */
688 public void loadPoster(String url) {
689 if (url == null) {
Andrei Popescu50c86232009-09-30 16:51:25 +0100690 Message message = obtainMessage(LOAD_DEFAULT_POSTER);
691 sendMessage(message);
Andrei Popescu64b86a12009-09-15 20:34:18 +0100692 return;
693 }
694 // Cancel any active poster download.
695 if (mPosterDownloader != null) {
696 mPosterDownloader.cancelAndReleaseQueue();
697 }
698 // Load the poster asynchronously
699 mPosterDownloader = new PosterDownloader(url, this);
700 mPosterDownloader.start();
Patrick Scott0a5ce012009-07-02 08:56:10 -0400701 }
702
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700703 // These three function are called from UI thread only by WebView.
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800704 public void setBaseLayer(int layer) {
705 VideoPlayer.setBaseLayer(layer);
706 }
707
708 public void pauseAndDispatch() {
709 VideoPlayer.pauseAndDispatch();
710 }
Teng-Hui Zhu10ab6542011-03-16 16:42:32 -0700711
712 public void enterFullScreenVideo(int layerId, String url) {
713 VideoPlayer.enterFullScreenVideo(layerId, url, this, mWebView);
714 }
715
Teng-Hui Zhud7678a12012-01-16 15:33:09 -0800716 public void exitFullScreenVideo() {
717 VideoPlayer.exitFullScreenVideo(this, mWebView);
718 }
719
Andrei Popescu6fa29582009-06-19 14:54:09 +0100720 /**
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100721 * The factory for HTML5VideoViewProxy instances.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100722 * @param webViewCore is the WebViewCore that is requesting the proxy.
723 *
Andrei Popescu3c946a1a2009-07-03 08:20:53 +0100724 * @return a new HTML5VideoViewProxy object.
Andrei Popescu6fa29582009-06-19 14:54:09 +0100725 */
Andrei Popescu290c34a2009-09-17 15:55:24 +0100726 public static HTML5VideoViewProxy getInstance(WebViewCore webViewCore, int nativePtr) {
Jonathan Dixona6c4d8e2012-03-08 17:55:31 +0000727 return new HTML5VideoViewProxy(webViewCore.getWebViewClassic(), nativePtr);
Andrei Popescu6fa29582009-06-19 14:54:09 +0100728 }
Andrei Popescu290c34a2009-09-17 15:55:24 +0100729
Jonathan Dixon3c909522012-02-28 18:45:06 +0000730 /* package */ WebViewClassic getWebView() {
Ben Murdoch1708ad52010-11-11 15:56:16 +0000731 return mWebView;
732 }
733
Andrei Popescu290c34a2009-09-17 15:55:24 +0100734 private native void nativeOnPrepared(int duration, int width, int height, int nativePointer);
735 private native void nativeOnEnded(int nativePointer);
Shimeng (Simon) Wang43aaa2d2010-10-18 16:25:59 -0700736 private native void nativeOnPaused(int nativePointer);
Andrei Popescu50c86232009-09-30 16:51:25 +0100737 private native void nativeOnPosterFetched(Bitmap poster, int nativePointer);
Andrei Popescu048eb3b2010-01-11 21:12:54 +0000738 private native void nativeOnTimeupdate(int position, int nativePointer);
Teng-Hui Zhub109c882011-05-04 16:19:49 -0700739 private native void nativeOnStopFullscreen(int nativePointer);
Teng-Hui Zhue4c89e32012-01-10 16:00:21 -0800740 private native void nativeOnRestoreState(int nativePointer);
Teng-Hui Zhu661e8b12011-03-02 15:09:34 -0800741 private native static boolean nativeSendSurfaceTexture(SurfaceTexture texture,
742 int baseLayer, int videoLayerId, int textureName,
Teng-Hui Zhu265db322011-03-18 14:56:10 -0700743 int playerState);
Teng-Hui Zhuc0fccd12011-03-29 10:35:11 -0700744
745 @Override
746 public boolean onInfo(MediaPlayer mp, int what, int extra) {
747 if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
748 sendMessage(obtainMessage(BUFFERING_START, what, extra));
749 } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
750 sendMessage(obtainMessage(BUFFERING_END, what, extra));
751 }
752 return false;
753 }
Andrei Popescu6fa29582009-06-19 14:54:09 +0100754}