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