Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.webkit; |
| 18 | |
| 19 | import android.content.Context; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.BitmapFactory; |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 22 | import android.graphics.SurfaceTexture; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 23 | import android.media.MediaPlayer; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 24 | import android.net.http.EventHandler; |
| 25 | import android.net.http.Headers; |
| 26 | import android.net.http.RequestHandle; |
| 27 | import android.net.http.RequestQueue; |
| 28 | import android.net.http.SslCertificate; |
| 29 | import android.net.http.SslError; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.Looper; |
| 32 | import android.os.Message; |
| 33 | import android.util.Log; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 34 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 35 | import java.io.ByteArrayOutputStream; |
| 36 | import java.io.IOException; |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 37 | import java.net.MalformedURLException; |
| 38 | import java.net.URL; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 39 | import java.util.HashMap; |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 40 | import java.util.Map; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 41 | |
| 42 | /** |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 43 | * <p>Proxy for HTML5 video views. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 44 | */ |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 45 | class HTML5VideoViewProxy extends Handler |
| 46 | implements MediaPlayer.OnPreparedListener, |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 47 | MediaPlayer.OnCompletionListener, |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 48 | MediaPlayer.OnErrorListener, |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 49 | MediaPlayer.OnInfoListener, |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 50 | SurfaceTexture.OnFrameAvailableListener { |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 51 | // Logging tag. |
| 52 | private static final String LOGTAG = "HTML5VideoViewProxy"; |
| 53 | |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 54 | // Message Ids for WebCore thread -> UI thread communication. |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 55 | 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 Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 60 | private static final int BUFFERING_START = 105; |
| 61 | private static final int BUFFERING_END = 106; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 62 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 63 | // Message Ids to be handled on the WebCore thread |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 64 | private static final int PREPARED = 200; |
| 65 | private static final int ENDED = 201; |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 66 | private static final int POSTER_FETCHED = 202; |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 67 | private static final int PAUSED = 203; |
Teng-Hui Zhu | b109c88 | 2011-05-04 16:19:49 -0700 | [diff] [blame] | 68 | private static final int STOPFULLSCREEN = 204; |
Teng-Hui Zhu | e4c89e3 | 2012-01-10 16:00:21 -0800 | [diff] [blame] | 69 | private static final int RESTORESTATE = 205; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 70 | |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 71 | // Timer thread -> UI thread |
| 72 | private static final int TIMEUPDATE = 300; |
| 73 | |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 74 | // The C++ MediaPlayerPrivateAndroid object. |
| 75 | int mNativePointer; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 76 | // The handler for WebCore thread messages; |
| 77 | private Handler mWebCoreHandler; |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 78 | // The WebViewClassic instance that created this view. |
| 79 | private WebViewClassic mWebView; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 80 | // The poster image to be shown when the video is not playing. |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 81 | // This ref prevents the bitmap from being GC'ed. |
| 82 | private Bitmap mPoster; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 83 | // The poster downloader. |
| 84 | private PosterDownloader mPosterDownloader; |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 85 | // The seek position. |
| 86 | private int mSeekPosition; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 87 | // 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 Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 93 | private static HTML5VideoView mHTML5VideoView; |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 94 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 95 | 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 Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 99 | |
Teng-Hui Zhu | f4d4e9e | 2011-03-30 14:39:56 -0700 | [diff] [blame] | 100 | private static void setPlayerBuffering(boolean playerBuffering) { |
| 101 | mHTML5VideoView.setPlayerBuffering(playerBuffering); |
| 102 | } |
| 103 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 104 | // 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 Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 108 | // Don't do this for full screen mode. |
| 109 | if (mHTML5VideoView != null |
Teng-Hui Zhu | 3fafd39 | 2011-05-31 15:15:31 -0700 | [diff] [blame] | 110 | && !mHTML5VideoView.isFullScreenMode() |
| 111 | && !mHTML5VideoView.surfaceTextureDeleted()) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 112 | mBaseLayer = layer; |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 113 | |
| 114 | int currentVideoLayerId = mHTML5VideoView.getVideoLayerId(); |
Teng-Hui Zhu | c2b06d5 | 2012-05-09 14:13:52 -0700 | [diff] [blame] | 115 | SurfaceTexture surfTexture = |
| 116 | HTML5VideoInline.getSurfaceTexture(currentVideoLayerId); |
Teng-Hui Zhu | 3fafd39 | 2011-05-31 15:15:31 -0700 | [diff] [blame] | 117 | int textureName = mHTML5VideoView.getTextureName(); |
| 118 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 119 | if (layer != 0 && surfTexture != null && currentVideoLayerId != -1) { |
Teng-Hui Zhu | 265db32 | 2011-03-18 14:56:10 -0700 | [diff] [blame] | 120 | int playerState = mHTML5VideoView.getCurrentState(); |
Teng-Hui Zhu | f4d4e9e | 2011-03-30 14:39:56 -0700 | [diff] [blame] | 121 | if (mHTML5VideoView.getPlayerBuffering()) |
Teng-Hui Zhu | c2b06d5 | 2012-05-09 14:13:52 -0700 | [diff] [blame] | 122 | playerState = HTML5VideoView.STATE_PREPARING; |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 123 | boolean foundInTree = nativeSendSurfaceTexture(surfTexture, |
| 124 | layer, currentVideoLayerId, textureName, |
Teng-Hui Zhu | 265db32 | 2011-03-18 14:56:10 -0700 | [diff] [blame] | 125 | playerState); |
Teng-Hui Zhu | 1e26d82 | 2011-03-23 16:54:20 -0700 | [diff] [blame] | 126 | if (playerState >= HTML5VideoView.STATE_PREPARED |
Teng-Hui Zhu | 265db32 | 2011-03-18 14:56:10 -0700 | [diff] [blame] | 127 | && !foundInTree) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 128 | mHTML5VideoView.pauseAndDispatch(mCurrentProxy); |
| 129 | mHTML5VideoView.deleteSurfaceTexture(); |
Andrei Popescu | 31d2aa1 | 2010-04-08 15:19:22 +0100 | [diff] [blame] | 130 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 131 | } |
Andrei Popescu | a41f97b | 2010-01-11 18:36:25 +0000 | [diff] [blame] | 132 | } |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 133 | } |
Andrei Popescu | a41f97b | 2010-01-11 18:36:25 +0000 | [diff] [blame] | 134 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 135 | // When a WebView is paused, we also want to pause the video in it. |
| 136 | public static void pauseAndDispatch() { |
| 137 | if (mHTML5VideoView != null) { |
| 138 | mHTML5VideoView.pauseAndDispatch(mCurrentProxy); |
| 139 | // When switching out, clean the video content on the old page |
| 140 | // by telling the layer not readyToUseSurfTex. |
| 141 | setBaseLayer(mBaseLayer); |
| 142 | } |
| 143 | } |
| 144 | |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 145 | public static void enterFullScreenVideo(int layerId, String url, |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 146 | HTML5VideoViewProxy proxy, WebViewClassic webView) { |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 147 | // Save the inline video info and inherit it in the full screen |
| 148 | int savePosition = 0; |
Teng-Hui Zhu | c2b06d5 | 2012-05-09 14:13:52 -0700 | [diff] [blame] | 149 | boolean canSkipPrepare = false; |
Teng-Hui Zhu | 6a9586b | 2012-06-15 11:22:23 -0700 | [diff] [blame^] | 150 | boolean forceStart = false; |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 151 | if (mHTML5VideoView != null) { |
Teng-Hui Zhu | 2b64c5a | 2012-03-19 13:56:49 -0700 | [diff] [blame] | 152 | // We don't allow enter full screen mode while the previous |
| 153 | // full screen video hasn't finished yet. |
| 154 | if (!mHTML5VideoView.fullScreenExited() && mHTML5VideoView.isFullScreenMode()) { |
| 155 | Log.w(LOGTAG, "Try to reenter the full screen mode"); |
| 156 | return; |
| 157 | } |
Teng-Hui Zhu | 6a9586b | 2012-06-15 11:22:23 -0700 | [diff] [blame^] | 158 | int playerState = mHTML5VideoView.getCurrentState(); |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 159 | // If we are playing the same video, then it is better to |
| 160 | // save the current position. |
| 161 | if (layerId == mHTML5VideoView.getVideoLayerId()) { |
| 162 | savePosition = mHTML5VideoView.getCurrentPosition(); |
Teng-Hui Zhu | c2b06d5 | 2012-05-09 14:13:52 -0700 | [diff] [blame] | 163 | canSkipPrepare = (playerState == HTML5VideoView.STATE_PREPARING |
| 164 | || playerState == HTML5VideoView.STATE_PREPARED |
| 165 | || playerState == HTML5VideoView.STATE_PLAYING) |
| 166 | && !mHTML5VideoView.isFullScreenMode(); |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 167 | } |
Teng-Hui Zhu | c2b06d5 | 2012-05-09 14:13:52 -0700 | [diff] [blame] | 168 | if (!canSkipPrepare) { |
| 169 | mHTML5VideoView.reset(); |
Teng-Hui Zhu | 6a9586b | 2012-06-15 11:22:23 -0700 | [diff] [blame^] | 170 | } else { |
| 171 | forceStart = playerState == HTML5VideoView.STATE_PREPARING |
| 172 | || playerState == HTML5VideoView.STATE_PLAYING; |
Teng-Hui Zhu | c2b06d5 | 2012-05-09 14:13:52 -0700 | [diff] [blame] | 173 | } |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 174 | } |
| 175 | mHTML5VideoView = new HTML5VideoFullScreen(proxy.getContext(), |
Teng-Hui Zhu | c2b06d5 | 2012-05-09 14:13:52 -0700 | [diff] [blame] | 176 | layerId, savePosition, canSkipPrepare); |
Teng-Hui Zhu | 6a9586b | 2012-06-15 11:22:23 -0700 | [diff] [blame^] | 177 | mHTML5VideoView.setStartWhenPrepared(forceStart); |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 178 | mCurrentProxy = proxy; |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 179 | mHTML5VideoView.setVideoURI(url, mCurrentProxy); |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 180 | mHTML5VideoView.enterFullScreenVideoState(layerId, proxy, webView); |
| 181 | } |
| 182 | |
Teng-Hui Zhu | d7678a1 | 2012-01-16 15:33:09 -0800 | [diff] [blame] | 183 | public static void exitFullScreenVideo(HTML5VideoViewProxy proxy, |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 184 | WebViewClassic webView) { |
Teng-Hui Zhu | d7678a1 | 2012-01-16 15:33:09 -0800 | [diff] [blame] | 185 | if (!mHTML5VideoView.fullScreenExited() && mHTML5VideoView.isFullScreenMode()) { |
| 186 | WebChromeClient client = webView.getWebChromeClient(); |
| 187 | if (client != null) { |
| 188 | client.onHideCustomView(); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 193 | // This is on the UI thread. |
| 194 | // When native tell Java to play, we need to check whether or not it is |
| 195 | // still the same video by using videoLayerId and treat it differently. |
| 196 | public static void play(String url, int time, HTML5VideoViewProxy proxy, |
| 197 | WebChromeClient client, int videoLayerId) { |
| 198 | int currentVideoLayerId = -1; |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 199 | boolean backFromFullScreenMode = false; |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 200 | if (mHTML5VideoView != null) { |
| 201 | currentVideoLayerId = mHTML5VideoView.getVideoLayerId(); |
Teng-Hui Zhu | 1a88acb | 2011-06-01 13:28:41 -0700 | [diff] [blame] | 202 | backFromFullScreenMode = mHTML5VideoView.fullScreenExited(); |
Teng-Hui Zhu | 96fae5e | 2012-01-18 15:25:51 -0800 | [diff] [blame] | 203 | |
| 204 | // When playing video back to back in full screen mode, |
| 205 | // javascript will switch the src and call play. |
| 206 | // In this case, we can just reuse the same full screen view, |
| 207 | // and play the video after prepared. |
| 208 | if (mHTML5VideoView.isFullScreenMode() |
| 209 | && !backFromFullScreenMode |
| 210 | && currentVideoLayerId != videoLayerId |
| 211 | && mCurrentProxy != proxy) { |
| 212 | mCurrentProxy = proxy; |
| 213 | mHTML5VideoView.setStartWhenPrepared(true); |
| 214 | mHTML5VideoView.setVideoURI(url, proxy); |
| 215 | mHTML5VideoView.reprepareData(proxy); |
| 216 | return; |
| 217 | } |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | if (backFromFullScreenMode |
Teng-Hui Zhu | 3fafd39 | 2011-05-31 15:15:31 -0700 | [diff] [blame] | 221 | || currentVideoLayerId != videoLayerId |
| 222 | || mHTML5VideoView.surfaceTextureDeleted()) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 223 | // 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 Zhu | 22954d4 | 2011-04-07 17:13:18 -0700 | [diff] [blame] | 229 | if (!backFromFullScreenMode) { |
| 230 | mHTML5VideoView.pauseAndDispatch(mCurrentProxy); |
| 231 | } |
Teng-Hui Zhu | c2b06d5 | 2012-05-09 14:13:52 -0700 | [diff] [blame] | 232 | mHTML5VideoView.reset(); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 233 | } |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 234 | mCurrentProxy = proxy; |
Teng-Hui Zhu | e4c89e3 | 2012-01-10 16:00:21 -0800 | [diff] [blame] | 235 | mHTML5VideoView = new HTML5VideoInline(videoLayerId, time); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 236 | |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 237 | mHTML5VideoView.setVideoURI(url, mCurrentProxy); |
| 238 | mHTML5VideoView.prepareDataAndDisplayMode(proxy); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 239 | } else if (mCurrentProxy == proxy) { |
| 240 | // Here, we handle the case when we keep playing with one video |
| 241 | if (!mHTML5VideoView.isPlaying()) { |
| 242 | mHTML5VideoView.seekTo(time); |
| 243 | mHTML5VideoView.start(); |
| 244 | } |
| 245 | } else if (mCurrentProxy != null) { |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 246 | // Some other video is already playing. Notify the caller that |
| 247 | // its playback ended. |
Andrei Popescu | c4fbceb | 2010-04-14 13:30:23 +0100 | [diff] [blame] | 248 | proxy.dispatchOnEnded(); |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 249 | } |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 250 | } |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 251 | |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 252 | public static boolean isPlaying(HTML5VideoViewProxy proxy) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 253 | return (mCurrentProxy == proxy && mHTML5VideoView != null |
| 254 | && mHTML5VideoView.isPlaying()); |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | public static int getCurrentPosition() { |
| 258 | int currentPosMs = 0; |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 259 | if (mHTML5VideoView != null) { |
| 260 | currentPosMs = mHTML5VideoView.getCurrentPosition(); |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 261 | } |
| 262 | return currentPosMs; |
| 263 | } |
| 264 | |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 265 | public static void seek(int time, HTML5VideoViewProxy proxy) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 266 | if (mCurrentProxy == proxy && time >= 0 && mHTML5VideoView != null) { |
| 267 | mHTML5VideoView.seekTo(time); |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | public static void pause(HTML5VideoViewProxy proxy) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 272 | if (mCurrentProxy == proxy && mHTML5VideoView != null) { |
| 273 | mHTML5VideoView.pause(); |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 274 | } |
| 275 | } |
Andrei Popescu | bf385d7 | 2009-09-18 18:59:52 +0100 | [diff] [blame] | 276 | |
| 277 | public static void onPrepared() { |
Teng-Hui Zhu | e4c89e3 | 2012-01-10 16:00:21 -0800 | [diff] [blame] | 278 | if (!mHTML5VideoView.isFullScreenMode()) { |
Teng-Hui Zhu | 0504967 | 2011-04-06 18:09:27 -0700 | [diff] [blame] | 279 | mHTML5VideoView.start(); |
| 280 | } |
Teng-Hui Zhu | 265db32 | 2011-03-18 14:56:10 -0700 | [diff] [blame] | 281 | if (mBaseLayer != 0) { |
| 282 | setBaseLayer(mBaseLayer); |
| 283 | } |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | public static void end() { |
Teng-Hui Zhu | 4dd9dc8 | 2012-05-10 17:20:19 -0700 | [diff] [blame] | 287 | mHTML5VideoView.showControllerInFullScreen(); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 288 | if (mCurrentProxy != null) { |
| 289 | if (isVideoSelfEnded) |
| 290 | mCurrentProxy.dispatchOnEnded(); |
| 291 | else |
| 292 | mCurrentProxy.dispatchOnPaused(); |
| 293 | } |
| 294 | isVideoSelfEnded = false; |
Andrei Popescu | bf385d7 | 2009-09-18 18:59:52 +0100 | [diff] [blame] | 295 | } |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | // A bunch event listeners for our VideoView |
| 299 | // MediaPlayer.OnPreparedListener |
| 300 | public void onPrepared(MediaPlayer mp) { |
Andrei Popescu | bf385d7 | 2009-09-18 18:59:52 +0100 | [diff] [blame] | 301 | VideoPlayer.onPrepared(); |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 302 | Message msg = Message.obtain(mWebCoreHandler, PREPARED); |
| 303 | Map<String, Object> map = new HashMap<String, Object>(); |
| 304 | map.put("dur", new Integer(mp.getDuration())); |
| 305 | map.put("width", new Integer(mp.getVideoWidth())); |
| 306 | map.put("height", new Integer(mp.getVideoHeight())); |
| 307 | msg.obj = map; |
| 308 | mWebCoreHandler.sendMessage(msg); |
| 309 | } |
| 310 | |
| 311 | // MediaPlayer.OnCompletionListener; |
| 312 | public void onCompletion(MediaPlayer mp) { |
Andrei Popescu | c4fbceb | 2010-04-14 13:30:23 +0100 | [diff] [blame] | 313 | // The video ended by itself, so we need to |
| 314 | // send a message to the UI thread to dismiss |
| 315 | // the video view and to return to the WebView. |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 316 | // arg1 == 1 means the video ends by itself. |
| 317 | sendMessage(obtainMessage(ENDED, 1, 0)); |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 320 | // MediaPlayer.OnErrorListener |
| 321 | public boolean onError(MediaPlayer mp, int what, int extra) { |
| 322 | sendMessage(obtainMessage(ERROR)); |
| 323 | return false; |
| 324 | } |
| 325 | |
Andrei Popescu | c4fbceb | 2010-04-14 13:30:23 +0100 | [diff] [blame] | 326 | public void dispatchOnEnded() { |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 327 | Message msg = Message.obtain(mWebCoreHandler, ENDED); |
| 328 | mWebCoreHandler.sendMessage(msg); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 329 | } |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 330 | |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 331 | public void dispatchOnPaused() { |
Teng-Hui Zhu | b109c88 | 2011-05-04 16:19:49 -0700 | [diff] [blame] | 332 | Message msg = Message.obtain(mWebCoreHandler, PAUSED); |
| 333 | mWebCoreHandler.sendMessage(msg); |
| 334 | } |
| 335 | |
| 336 | public void dispatchOnStopFullScreen() { |
| 337 | Message msg = Message.obtain(mWebCoreHandler, STOPFULLSCREEN); |
| 338 | mWebCoreHandler.sendMessage(msg); |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Teng-Hui Zhu | e4c89e3 | 2012-01-10 16:00:21 -0800 | [diff] [blame] | 341 | public void dispatchOnRestoreState() { |
| 342 | Message msg = Message.obtain(mWebCoreHandler, RESTORESTATE); |
| 343 | mWebCoreHandler.sendMessage(msg); |
| 344 | } |
| 345 | |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 346 | public void onTimeupdate() { |
| 347 | sendMessage(obtainMessage(TIMEUPDATE)); |
| 348 | } |
| 349 | |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 350 | // When there is a frame ready from surface texture, we should tell WebView |
| 351 | // to refresh. |
| 352 | @Override |
| 353 | public void onFrameAvailable(SurfaceTexture surfaceTexture) { |
| 354 | // TODO: This should support partial invalidation too. |
| 355 | mWebView.invalidate(); |
| 356 | } |
| 357 | |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 358 | // Handler for the messages from WebCore or Timer thread to the UI thread. |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 359 | @Override |
| 360 | public void handleMessage(Message msg) { |
| 361 | // This executes on the UI thread. |
| 362 | switch (msg.what) { |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 363 | case PLAY: { |
| 364 | String url = (String) msg.obj; |
| 365 | WebChromeClient client = mWebView.getWebChromeClient(); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 366 | int videoLayerID = msg.arg1; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 367 | if (client != null) { |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 368 | VideoPlayer.play(url, mSeekPosition, this, client, videoLayerID); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 369 | } |
| 370 | break; |
| 371 | } |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 372 | case SEEK: { |
| 373 | Integer time = (Integer) msg.obj; |
| 374 | mSeekPosition = time; |
| 375 | VideoPlayer.seek(mSeekPosition, this); |
| 376 | break; |
| 377 | } |
| 378 | case PAUSE: { |
| 379 | VideoPlayer.pause(this); |
| 380 | break; |
| 381 | } |
Andrei Popescu | 46a83b4 | 2009-10-23 13:49:46 +0100 | [diff] [blame] | 382 | case ENDED: |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 383 | if (msg.arg1 == 1) |
| 384 | VideoPlayer.isVideoSelfEnded = true; |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 385 | VideoPlayer.end(); |
| 386 | break; |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 387 | case ERROR: { |
| 388 | WebChromeClient client = mWebView.getWebChromeClient(); |
| 389 | if (client != null) { |
| 390 | client.onHideCustomView(); |
| 391 | } |
| 392 | break; |
| 393 | } |
| 394 | case LOAD_DEFAULT_POSTER: { |
| 395 | WebChromeClient client = mWebView.getWebChromeClient(); |
| 396 | if (client != null) { |
| 397 | doSetPoster(client.getDefaultVideoPoster()); |
| 398 | } |
| 399 | break; |
| 400 | } |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 401 | case TIMEUPDATE: { |
| 402 | if (VideoPlayer.isPlaying(this)) { |
| 403 | sendTimeupdate(); |
| 404 | } |
| 405 | break; |
| 406 | } |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 407 | case BUFFERING_START: { |
Teng-Hui Zhu | f4d4e9e | 2011-03-30 14:39:56 -0700 | [diff] [blame] | 408 | VideoPlayer.setPlayerBuffering(true); |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 409 | break; |
| 410 | } |
| 411 | case BUFFERING_END: { |
Teng-Hui Zhu | f4d4e9e | 2011-03-30 14:39:56 -0700 | [diff] [blame] | 412 | VideoPlayer.setPlayerBuffering(false); |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 413 | break; |
| 414 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 418 | // Everything below this comment executes on the WebCore thread, except for |
| 419 | // the EventHandler methods, which are called on the network thread. |
| 420 | |
| 421 | // A helper class that knows how to download posters |
| 422 | private static final class PosterDownloader implements EventHandler { |
| 423 | // The request queue. This is static as we have one queue for all posters. |
| 424 | private static RequestQueue mRequestQueue; |
| 425 | private static int mQueueRefCount = 0; |
| 426 | // The poster URL |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 427 | private URL mUrl; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 428 | // The proxy we're doing this for. |
| 429 | private final HTML5VideoViewProxy mProxy; |
| 430 | // The poster bytes. We only touch this on the network thread. |
| 431 | private ByteArrayOutputStream mPosterBytes; |
| 432 | // The request handle. We only touch this on the WebCore thread. |
| 433 | private RequestHandle mRequestHandle; |
| 434 | // The response status code. |
| 435 | private int mStatusCode; |
| 436 | // The response headers. |
| 437 | private Headers mHeaders; |
| 438 | // The handler to handle messages on the WebCore thread. |
| 439 | private Handler mHandler; |
| 440 | |
| 441 | public PosterDownloader(String url, HTML5VideoViewProxy proxy) { |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 442 | try { |
| 443 | mUrl = new URL(url); |
| 444 | } catch (MalformedURLException e) { |
| 445 | mUrl = null; |
| 446 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 447 | mProxy = proxy; |
| 448 | mHandler = new Handler(); |
| 449 | } |
| 450 | // Start the download. Called on WebCore thread. |
| 451 | public void start() { |
| 452 | retainQueue(); |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 453 | |
| 454 | if (mUrl == null) { |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | // Only support downloading posters over http/https. |
| 459 | // FIXME: Add support for other schemes. WebKit seems able to load |
| 460 | // posters over other schemes e.g. file://, but gets the dimensions wrong. |
| 461 | String protocol = mUrl.getProtocol(); |
| 462 | if ("http".equals(protocol) || "https".equals(protocol)) { |
| 463 | mRequestHandle = mRequestQueue.queueRequest(mUrl.toString(), "GET", null, |
| 464 | this, null, 0); |
| 465 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 466 | } |
| 467 | // Cancel the download if active and release the queue. Called on WebCore thread. |
| 468 | public void cancelAndReleaseQueue() { |
| 469 | if (mRequestHandle != null) { |
| 470 | mRequestHandle.cancel(); |
| 471 | mRequestHandle = null; |
| 472 | } |
| 473 | releaseQueue(); |
| 474 | } |
| 475 | // EventHandler methods. Executed on the network thread. |
| 476 | public void status(int major_version, |
| 477 | int minor_version, |
| 478 | int code, |
| 479 | String reason_phrase) { |
| 480 | mStatusCode = code; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 481 | } |
| 482 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 483 | public void headers(Headers headers) { |
| 484 | mHeaders = headers; |
| 485 | } |
| 486 | |
| 487 | public void data(byte[] data, int len) { |
| 488 | if (mPosterBytes == null) { |
| 489 | mPosterBytes = new ByteArrayOutputStream(); |
| 490 | } |
| 491 | mPosterBytes.write(data, 0, len); |
| 492 | } |
| 493 | |
| 494 | public void endData() { |
| 495 | if (mStatusCode == 200) { |
| 496 | if (mPosterBytes.size() > 0) { |
| 497 | Bitmap poster = BitmapFactory.decodeByteArray( |
| 498 | mPosterBytes.toByteArray(), 0, mPosterBytes.size()); |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 499 | mProxy.doSetPoster(poster); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 500 | } |
| 501 | cleanup(); |
| 502 | } else if (mStatusCode >= 300 && mStatusCode < 400) { |
| 503 | // We have a redirect. |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 504 | try { |
| 505 | mUrl = new URL(mHeaders.getLocation()); |
| 506 | } catch (MalformedURLException e) { |
| 507 | mUrl = null; |
| 508 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 509 | if (mUrl != null) { |
| 510 | mHandler.post(new Runnable() { |
| 511 | public void run() { |
| 512 | if (mRequestHandle != null) { |
Ben Murdoch | 4250979 | 2011-02-18 12:34:35 +0000 | [diff] [blame] | 513 | mRequestHandle.setupRedirect(mUrl.toString(), mStatusCode, |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 514 | new HashMap<String, String>()); |
| 515 | } |
| 516 | } |
| 517 | }); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | public void certificate(SslCertificate certificate) { |
| 523 | // Don't care. |
| 524 | } |
| 525 | |
| 526 | public void error(int id, String description) { |
| 527 | cleanup(); |
| 528 | } |
| 529 | |
| 530 | public boolean handleSslErrorRequest(SslError error) { |
| 531 | // Don't care. If this happens, data() will never be called so |
| 532 | // mPosterBytes will never be created, so no need to call cleanup. |
| 533 | return false; |
| 534 | } |
| 535 | // Tears down the poster bytes stream. Called on network thread. |
| 536 | private void cleanup() { |
| 537 | if (mPosterBytes != null) { |
| 538 | try { |
| 539 | mPosterBytes.close(); |
| 540 | } catch (IOException ignored) { |
| 541 | // Ignored. |
| 542 | } finally { |
| 543 | mPosterBytes = null; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // Queue management methods. Called on WebCore thread. |
| 549 | private void retainQueue() { |
| 550 | if (mRequestQueue == null) { |
| 551 | mRequestQueue = new RequestQueue(mProxy.getContext()); |
| 552 | } |
| 553 | mQueueRefCount++; |
| 554 | } |
| 555 | |
| 556 | private void releaseQueue() { |
| 557 | if (mQueueRefCount == 0) { |
| 558 | return; |
| 559 | } |
| 560 | if (--mQueueRefCount == 0) { |
| 561 | mRequestQueue.shutdown(); |
| 562 | mRequestQueue = null; |
| 563 | } |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 564 | } |
| 565 | } |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 566 | |
| 567 | /** |
| 568 | * Private constructor. |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 569 | * @param webView is the WebView that hosts the video. |
| 570 | * @param nativePtr is the C++ pointer to the MediaPlayerPrivate object. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 571 | */ |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 572 | private HTML5VideoViewProxy(WebViewClassic webView, int nativePtr) { |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 573 | // This handler is for the main (UI) thread. |
| 574 | super(Looper.getMainLooper()); |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 575 | // Save the WebView object. |
| 576 | mWebView = webView; |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 577 | // Pass Proxy into webview, such that every time we have a setBaseLayer |
| 578 | // call, we tell this Proxy to call the native to update the layer tree |
| 579 | // for the Video Layer's surface texture info |
| 580 | mWebView.setHTML5VideoViewProxy(this); |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 581 | // Save the native ptr |
| 582 | mNativePointer = nativePtr; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 583 | // create the message handler for this thread |
| 584 | createWebCoreHandler(); |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 587 | private void createWebCoreHandler() { |
| 588 | mWebCoreHandler = new Handler() { |
| 589 | @Override |
| 590 | public void handleMessage(Message msg) { |
| 591 | switch (msg.what) { |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 592 | case PREPARED: { |
| 593 | Map<String, Object> map = (Map<String, Object>) msg.obj; |
| 594 | Integer duration = (Integer) map.get("dur"); |
| 595 | Integer width = (Integer) map.get("width"); |
| 596 | Integer height = (Integer) map.get("height"); |
| 597 | nativeOnPrepared(duration.intValue(), width.intValue(), |
| 598 | height.intValue(), mNativePointer); |
| 599 | break; |
| 600 | } |
| 601 | case ENDED: |
Ben Murdoch | ff19d19 | 2011-01-17 18:08:56 +0000 | [diff] [blame] | 602 | mSeekPosition = 0; |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 603 | nativeOnEnded(mNativePointer); |
| 604 | break; |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 605 | case PAUSED: |
| 606 | nativeOnPaused(mNativePointer); |
| 607 | break; |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 608 | case POSTER_FETCHED: |
| 609 | Bitmap poster = (Bitmap) msg.obj; |
| 610 | nativeOnPosterFetched(poster, mNativePointer); |
| 611 | break; |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 612 | case TIMEUPDATE: |
| 613 | nativeOnTimeupdate(msg.arg1, mNativePointer); |
| 614 | break; |
Teng-Hui Zhu | b109c88 | 2011-05-04 16:19:49 -0700 | [diff] [blame] | 615 | case STOPFULLSCREEN: |
| 616 | nativeOnStopFullscreen(mNativePointer); |
| 617 | break; |
Teng-Hui Zhu | e4c89e3 | 2012-01-10 16:00:21 -0800 | [diff] [blame] | 618 | case RESTORESTATE: |
| 619 | nativeOnRestoreState(mNativePointer); |
| 620 | break; |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 621 | } |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 622 | } |
| 623 | }; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 624 | } |
| 625 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 626 | private void doSetPoster(Bitmap poster) { |
| 627 | if (poster == null) { |
| 628 | return; |
| 629 | } |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 630 | // Save a ref to the bitmap and send it over to the WebCore thread. |
| 631 | mPoster = poster; |
| 632 | Message msg = Message.obtain(mWebCoreHandler, POSTER_FETCHED); |
| 633 | msg.obj = poster; |
| 634 | mWebCoreHandler.sendMessage(msg); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 637 | private void sendTimeupdate() { |
| 638 | Message msg = Message.obtain(mWebCoreHandler, TIMEUPDATE); |
| 639 | msg.arg1 = VideoPlayer.getCurrentPosition(); |
| 640 | mWebCoreHandler.sendMessage(msg); |
| 641 | } |
| 642 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 643 | public Context getContext() { |
| 644 | return mWebView.getContext(); |
| 645 | } |
| 646 | |
| 647 | // The public methods below are all called from WebKit only. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 648 | /** |
| 649 | * Play a video stream. |
| 650 | * @param url is the URL of the video stream. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 651 | */ |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 652 | public void play(String url, int position, int videoLayerID) { |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 653 | if (url == null) { |
| 654 | return; |
| 655 | } |
Ben Murdoch | ff19d19 | 2011-01-17 18:08:56 +0000 | [diff] [blame] | 656 | |
| 657 | if (position > 0) { |
| 658 | seek(position); |
| 659 | } |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 660 | Message message = obtainMessage(PLAY); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 661 | message.arg1 = videoLayerID; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 662 | message.obj = url; |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 663 | sendMessage(message); |
| 664 | } |
| 665 | |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 666 | /** |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 667 | * Seek into the video stream. |
| 668 | * @param time is the position in the video stream. |
| 669 | */ |
| 670 | public void seek(int time) { |
| 671 | Message message = obtainMessage(SEEK); |
| 672 | message.obj = new Integer(time); |
| 673 | sendMessage(message); |
| 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Pause the playback. |
| 678 | */ |
| 679 | public void pause() { |
| 680 | Message message = obtainMessage(PAUSE); |
| 681 | sendMessage(message); |
| 682 | } |
| 683 | |
| 684 | /** |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 685 | * Tear down this proxy object. |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 686 | */ |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 687 | public void teardown() { |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 688 | // This is called by the C++ MediaPlayerPrivate dtor. |
| 689 | // Cancel any active poster download. |
| 690 | if (mPosterDownloader != null) { |
| 691 | mPosterDownloader.cancelAndReleaseQueue(); |
| 692 | } |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 693 | mNativePointer = 0; |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /** |
| 697 | * Load the poster image. |
| 698 | * @param url is the URL of the poster image. |
| 699 | */ |
| 700 | public void loadPoster(String url) { |
| 701 | if (url == null) { |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 702 | Message message = obtainMessage(LOAD_DEFAULT_POSTER); |
| 703 | sendMessage(message); |
Andrei Popescu | 64b86a1 | 2009-09-15 20:34:18 +0100 | [diff] [blame] | 704 | return; |
| 705 | } |
| 706 | // Cancel any active poster download. |
| 707 | if (mPosterDownloader != null) { |
| 708 | mPosterDownloader.cancelAndReleaseQueue(); |
| 709 | } |
| 710 | // Load the poster asynchronously |
| 711 | mPosterDownloader = new PosterDownloader(url, this); |
| 712 | mPosterDownloader.start(); |
Patrick Scott | 0a5ce01 | 2009-07-02 08:56:10 -0400 | [diff] [blame] | 713 | } |
| 714 | |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 715 | // These three function are called from UI thread only by WebView. |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 716 | public void setBaseLayer(int layer) { |
| 717 | VideoPlayer.setBaseLayer(layer); |
| 718 | } |
| 719 | |
| 720 | public void pauseAndDispatch() { |
| 721 | VideoPlayer.pauseAndDispatch(); |
| 722 | } |
Teng-Hui Zhu | 10ab654 | 2011-03-16 16:42:32 -0700 | [diff] [blame] | 723 | |
| 724 | public void enterFullScreenVideo(int layerId, String url) { |
| 725 | VideoPlayer.enterFullScreenVideo(layerId, url, this, mWebView); |
| 726 | } |
| 727 | |
Teng-Hui Zhu | d7678a1 | 2012-01-16 15:33:09 -0800 | [diff] [blame] | 728 | public void exitFullScreenVideo() { |
| 729 | VideoPlayer.exitFullScreenVideo(this, mWebView); |
| 730 | } |
| 731 | |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 732 | /** |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 733 | * The factory for HTML5VideoViewProxy instances. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 734 | * @param webViewCore is the WebViewCore that is requesting the proxy. |
| 735 | * |
Andrei Popescu | 3c946a1a | 2009-07-03 08:20:53 +0100 | [diff] [blame] | 736 | * @return a new HTML5VideoViewProxy object. |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 737 | */ |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 738 | public static HTML5VideoViewProxy getInstance(WebViewCore webViewCore, int nativePtr) { |
Jonathan Dixon | a6c4d8e | 2012-03-08 17:55:31 +0000 | [diff] [blame] | 739 | return new HTML5VideoViewProxy(webViewCore.getWebViewClassic(), nativePtr); |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 740 | } |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 741 | |
Jonathan Dixon | 3c90952 | 2012-02-28 18:45:06 +0000 | [diff] [blame] | 742 | /* package */ WebViewClassic getWebView() { |
Ben Murdoch | 1708ad5 | 2010-11-11 15:56:16 +0000 | [diff] [blame] | 743 | return mWebView; |
| 744 | } |
| 745 | |
Andrei Popescu | 290c34a | 2009-09-17 15:55:24 +0100 | [diff] [blame] | 746 | private native void nativeOnPrepared(int duration, int width, int height, int nativePointer); |
| 747 | private native void nativeOnEnded(int nativePointer); |
Shimeng (Simon) Wang | 43aaa2d | 2010-10-18 16:25:59 -0700 | [diff] [blame] | 748 | private native void nativeOnPaused(int nativePointer); |
Andrei Popescu | 50c8623 | 2009-09-30 16:51:25 +0100 | [diff] [blame] | 749 | private native void nativeOnPosterFetched(Bitmap poster, int nativePointer); |
Andrei Popescu | 048eb3b | 2010-01-11 21:12:54 +0000 | [diff] [blame] | 750 | private native void nativeOnTimeupdate(int position, int nativePointer); |
Teng-Hui Zhu | b109c88 | 2011-05-04 16:19:49 -0700 | [diff] [blame] | 751 | private native void nativeOnStopFullscreen(int nativePointer); |
Teng-Hui Zhu | e4c89e3 | 2012-01-10 16:00:21 -0800 | [diff] [blame] | 752 | private native void nativeOnRestoreState(int nativePointer); |
Teng-Hui Zhu | 661e8b1 | 2011-03-02 15:09:34 -0800 | [diff] [blame] | 753 | private native static boolean nativeSendSurfaceTexture(SurfaceTexture texture, |
| 754 | int baseLayer, int videoLayerId, int textureName, |
Teng-Hui Zhu | 265db32 | 2011-03-18 14:56:10 -0700 | [diff] [blame] | 755 | int playerState); |
Teng-Hui Zhu | c0fccd1 | 2011-03-29 10:35:11 -0700 | [diff] [blame] | 756 | |
| 757 | @Override |
| 758 | public boolean onInfo(MediaPlayer mp, int what, int extra) { |
| 759 | if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) { |
| 760 | sendMessage(obtainMessage(BUFFERING_START, what, extra)); |
| 761 | } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) { |
| 762 | sendMessage(obtainMessage(BUFFERING_END, what, extra)); |
| 763 | } |
| 764 | return false; |
| 765 | } |
Andrei Popescu | 6fa2958 | 2009-06-19 14:54:09 +0100 | [diff] [blame] | 766 | } |