OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/blink/webmediaplayer_cast_android.h" |
| 6 |
| 7 #include "gpu/GLES2/gl2extchromium.h" |
| 8 #include "gpu/blink/webgraphicscontext3d_impl.h" |
| 9 #include "gpu/command_buffer/client/gles2_interface.h" |
| 10 #include "gpu/command_buffer/common/sync_token.h" |
| 11 #include "media/base/android/media_common_android.h" |
| 12 #include "media/base/bind_to_current_loop.h" |
| 13 #include "media/blink/webmediaplayer_impl.h" |
| 14 #include "media/blink/webmediaplayer_params.h" |
| 15 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" |
| 16 #include "third_party/WebKit/public/web/WebDocument.h" |
| 17 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 18 #include "third_party/skia/include/core/SkCanvas.h" |
| 19 #include "third_party/skia/include/core/SkPaint.h" |
| 20 #include "third_party/skia/include/core/SkTypeface.h" |
| 21 #include "third_party/skia/include/gpu/GrContext.h" |
| 22 #include "third_party/skia/include/gpu/SkGrPixelRef.h" |
| 23 |
| 24 #if defined(OS_ANDROID) |
| 25 |
| 26 using gpu::gles2::GLES2Interface; |
| 27 |
| 28 namespace media { |
| 29 |
| 30 namespace { |
| 31 // File-static function is to allow it to run even after WMPI is deleted. |
| 32 void OnReleaseTexture(const WebMediaPlayerParams::Context3DCB& context_3d_cb, |
| 33 GLuint texture_id, |
| 34 const gpu::SyncToken& sync_token) { |
| 35 Context3D context_3d; |
| 36 if (!context_3d_cb.is_null()) |
| 37 context_3d = context_3d_cb.Run(); |
| 38 // GPU Process crashed. |
| 39 if (!context_3d.gl) |
| 40 return; |
| 41 |
| 42 GLES2Interface* gl = context_3d.gl; |
| 43 gl->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); |
| 44 gl->DeleteTextures(1, &texture_id); |
| 45 // Flush to ensure that the texture gets deleted in a timely fashion. |
| 46 gl->ShallowFlushCHROMIUM(); |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 51 WebMediaPlayerCast::WebMediaPlayerCast( |
| 52 WebMediaPlayerImpl* impl, |
| 53 blink::WebMediaPlayerClient* client, |
| 54 const WebMediaPlayerParams::Context3DCB& context_3d_cb) |
| 55 : webmediaplayer_(impl), client_(client), context_3d_cb_(context_3d_cb) {} |
| 56 |
| 57 WebMediaPlayerCast::~WebMediaPlayerCast() { |
| 58 if (player_manager_) { |
| 59 if (is_player_initialized_) |
| 60 player_manager_->DestroyPlayer(player_id_); |
| 61 |
| 62 player_manager_->UnregisterMediaPlayer(player_id_); |
| 63 } |
| 64 } |
| 65 |
| 66 void WebMediaPlayerCast::Initialize(const GURL& url, |
| 67 blink::WebLocalFrame* frame) { |
| 68 player_manager_->Initialize(MEDIA_PLAYER_TYPE_URL, player_id_, url, |
| 69 frame->document().firstPartyForCookies(), 0, |
| 70 frame->document().url(), true); |
| 71 is_player_initialized_ = true; |
| 72 } |
| 73 |
| 74 void WebMediaPlayerCast::set_media_player_manager( |
| 75 RendererMediaPlayerManagerInterface* media_player_manager) { |
| 76 player_manager_ = media_player_manager; |
| 77 player_id_ = player_manager_->RegisterMediaPlayer(this); |
| 78 } |
| 79 |
| 80 void WebMediaPlayerCast::requestRemotePlayback() { |
| 81 player_manager_->Seek(player_id_, base::TimeDelta::FromSecondsD( |
| 82 webmediaplayer_->currentTime())); |
| 83 player_manager_->RequestRemotePlayback(player_id_); |
| 84 } |
| 85 |
| 86 void WebMediaPlayerCast::requestRemotePlaybackControl() { |
| 87 player_manager_->RequestRemotePlaybackControl(player_id_); |
| 88 } |
| 89 |
| 90 // RendererMediaPlayerInterface implementation |
| 91 void WebMediaPlayerCast::OnMediaMetadataChanged(base::TimeDelta duration, |
| 92 int width, |
| 93 int height, |
| 94 bool success) {} |
| 95 |
| 96 void WebMediaPlayerCast::OnPlaybackComplete() { |
| 97 DVLOG(1) << __FUNCTION__; |
| 98 webmediaplayer_->OnRemotePlaybackEnded(); |
| 99 } |
| 100 |
| 101 void WebMediaPlayerCast::OnBufferingUpdate(int percentage) { |
| 102 DVLOG(1) << __FUNCTION__; |
| 103 } |
| 104 |
| 105 void WebMediaPlayerCast::OnSeekRequest(const base::TimeDelta& time_to_seek) { |
| 106 DVLOG(1) << __FUNCTION__; |
| 107 // DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 108 client_->requestSeek(time_to_seek.InSecondsF()); |
| 109 } |
| 110 |
| 111 void WebMediaPlayerCast::OnSeekComplete(const base::TimeDelta& current_time) { |
| 112 DVLOG(1) << __FUNCTION__; |
| 113 remote_time_at_ = base::TimeTicks::Now(); |
| 114 remote_time_ = current_time; |
| 115 webmediaplayer_->OnPipelineSeeked(true, PIPELINE_OK); |
| 116 } |
| 117 |
| 118 void WebMediaPlayerCast::OnMediaError(int error_type) { |
| 119 DVLOG(1) << __FUNCTION__; |
| 120 } |
| 121 |
| 122 void WebMediaPlayerCast::OnVideoSizeChanged(int width, int height) { |
| 123 DVLOG(1) << __FUNCTION__; |
| 124 } |
| 125 |
| 126 void WebMediaPlayerCast::OnTimeUpdate(base::TimeDelta current_timestamp, |
| 127 base::TimeTicks current_time_ticks) { |
| 128 DVLOG(1) << __FUNCTION__ << " " << current_timestamp.InSecondsF(); |
| 129 remote_time_at_ = current_time_ticks; |
| 130 remote_time_ = current_timestamp; |
| 131 } |
| 132 |
| 133 void WebMediaPlayerCast::OnPlayerReleased() { |
| 134 DVLOG(1) << __FUNCTION__; |
| 135 } |
| 136 |
| 137 void WebMediaPlayerCast::OnConnectedToRemoteDevice( |
| 138 const std::string& remote_playback_message) { |
| 139 DVLOG(1) << __FUNCTION__; |
| 140 remote_time_ = base::TimeDelta::FromSecondsD(webmediaplayer_->currentTime()); |
| 141 // Set paused so that progress bar doesn't advance while remote playback |
| 142 // is starting. |
| 143 webmediaplayer_->pause(); |
| 144 is_remote_ = true; |
| 145 DrawRemotePlaybackText(remote_playback_message); |
| 146 client_->connectedToRemoteDevice(); |
| 147 } |
| 148 |
| 149 double WebMediaPlayerCast::currentTime() const { |
| 150 base::TimeDelta ret = remote_time_; |
| 151 if (!paused_) { |
| 152 ret += base::TimeTicks::Now() - remote_time_at_; |
| 153 } |
| 154 return ret.InSecondsF(); |
| 155 } |
| 156 |
| 157 void WebMediaPlayerCast::play() { |
| 158 if (!paused_) |
| 159 return; |
| 160 |
| 161 player_manager_->Start(player_id_); |
| 162 remote_time_at_ = base::TimeTicks::Now(); |
| 163 paused_ = false; |
| 164 } |
| 165 void WebMediaPlayerCast::pause() { |
| 166 player_manager_->Pause(player_id_, true); |
| 167 } |
| 168 |
| 169 void WebMediaPlayerCast::seek(base::TimeDelta t) { |
| 170 should_notify_time_changed_ = true; |
| 171 player_manager_->Seek(player_id_, t); |
| 172 } |
| 173 |
| 174 void WebMediaPlayerCast::OnDisconnectedFromRemoteDevice() { |
| 175 DVLOG(1) << __FUNCTION__; |
| 176 is_remote_ = false; |
| 177 double t = currentTime(); |
| 178 if (t + media::kTimeUpdateInterval * 2 / 1000 > webmediaplayer_->duration()) { |
| 179 t = webmediaplayer_->duration(); |
| 180 } |
| 181 webmediaplayer_->OnDisconnectedFromRemoteDevice(t); |
| 182 } |
| 183 |
| 184 void WebMediaPlayerCast::OnDidExitFullscreen() { |
| 185 DVLOG(1) << __FUNCTION__; |
| 186 } |
| 187 |
| 188 void WebMediaPlayerCast::OnMediaPlayerPlay() { |
| 189 DVLOG(1) << __FUNCTION__ << " is_remote_ = " << is_remote_; |
| 190 if (is_remote_ && paused_) { |
| 191 paused_ = false; |
| 192 remote_time_at_ = base::TimeTicks::Now(); |
| 193 client_->playbackStateChanged(); |
| 194 } |
| 195 // Blink expects a timeChanged() in response to a seek(). |
| 196 if (should_notify_time_changed_) |
| 197 client_->timeChanged(); |
| 198 } |
| 199 |
| 200 void WebMediaPlayerCast::OnMediaPlayerPause() { |
| 201 DVLOG(1) << __FUNCTION__ << " is_remote_ = " << is_remote_; |
| 202 if (is_remote_ && !paused_) { |
| 203 paused_ = true; |
| 204 client_->playbackStateChanged(); |
| 205 } |
| 206 } |
| 207 |
| 208 void WebMediaPlayerCast::OnRemoteRouteAvailabilityChanged( |
| 209 bool routes_available) { |
| 210 DVLOG(1) << __FUNCTION__; |
| 211 client_->remoteRouteAvailabilityChanged(routes_available); |
| 212 } |
| 213 |
| 214 void WebMediaPlayerCast::ReleaseMediaResources() {} |
| 215 void WebMediaPlayerCast::OnWaitingForDecryptionKey() {} |
| 216 |
| 217 bool WebMediaPlayerCast::hasVideo() const { |
| 218 return true; |
| 219 } |
| 220 |
| 221 bool WebMediaPlayerCast::paused() const { |
| 222 return paused_; |
| 223 } |
| 224 |
| 225 #if defined(VIDEO_HOLE) |
| 226 bool WebMediaPlayerCast::UpdateBoundaryRectangle() { |
| 227 return false; |
| 228 } |
| 229 const gfx::RectF WebMediaPlayerCast::GetBoundaryRectangle() { |
| 230 return gfx::RectF(); |
| 231 } |
| 232 #endif // defined(VIDEO_HOLE) |
| 233 |
| 234 void WebMediaPlayerCast::DrawRemotePlaybackText( |
| 235 const std::string& remote_playback_message) { |
| 236 DVLOG(1) << __FUNCTION__; |
| 237 // DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 238 |
| 239 // TODO(johnme): Should redraw this frame if the layer bounds change; but |
| 240 // there seems no easy way to listen for the layer resizing (as opposed to |
| 241 // OnVideoSizeChanged, which is when the frame sizes of the video file |
| 242 // change). Perhaps have to poll (on main thread of course)? |
| 243 gfx::Size canvas_size = webmediaplayer_->GetCanvasSize(); |
| 244 if (!canvas_size.width()) |
| 245 return; |
| 246 |
| 247 SkBitmap bitmap; |
| 248 bitmap.allocN32Pixels(canvas_size.width(), canvas_size.height()); |
| 249 |
| 250 // Create the canvas and draw the "Casting to <Chromecast>" text on it. |
| 251 SkCanvas canvas(bitmap); |
| 252 canvas.drawColor(SK_ColorBLACK); |
| 253 |
| 254 const SkScalar kTextSize(40); |
| 255 const SkScalar kMinPadding(40); |
| 256 |
| 257 SkPaint paint; |
| 258 paint.setAntiAlias(true); |
| 259 paint.setFilterQuality(kHigh_SkFilterQuality); |
| 260 paint.setColor(SK_ColorWHITE); |
| 261 paint.setTypeface(SkTypeface::CreateFromName("sans", SkTypeface::kBold)); |
| 262 paint.setTextSize(kTextSize); |
| 263 |
| 264 // Calculate the vertical margin from the top |
| 265 SkPaint::FontMetrics font_metrics; |
| 266 paint.getFontMetrics(&font_metrics); |
| 267 SkScalar sk_vertical_margin = kMinPadding - font_metrics.fAscent; |
| 268 |
| 269 // Measure the width of the entire text to display |
| 270 size_t display_text_width = paint.measureText(remote_playback_message.c_str(), |
| 271 remote_playback_message.size()); |
| 272 std::string display_text(remote_playback_message); |
| 273 |
| 274 if (display_text_width + (kMinPadding * 2) > canvas_size.width()) { |
| 275 // The text is too long to fit in one line, truncate it and append ellipsis |
| 276 // to the end. |
| 277 |
| 278 // First, figure out how much of the canvas the '...' will take up. |
| 279 const std::string kTruncationEllipsis("\xE2\x80\xA6"); |
| 280 SkScalar sk_ellipse_width = paint.measureText(kTruncationEllipsis.c_str(), |
| 281 kTruncationEllipsis.size()); |
| 282 |
| 283 // Then calculate how much of the text can be drawn with the '...' appended |
| 284 // to the end of the string. |
| 285 SkScalar sk_max_original_text_width(canvas_size.width() - |
| 286 (kMinPadding * 2) - sk_ellipse_width); |
| 287 size_t sk_max_original_text_length = paint.breakText( |
| 288 remote_playback_message.c_str(), remote_playback_message.size(), |
| 289 sk_max_original_text_width); |
| 290 |
| 291 // Remove the part of the string that doesn't fit and append '...'. |
| 292 display_text.erase( |
| 293 sk_max_original_text_length, |
| 294 remote_playback_message.size() - sk_max_original_text_length); |
| 295 display_text.append(kTruncationEllipsis); |
| 296 display_text_width = |
| 297 paint.measureText(display_text.c_str(), display_text.size()); |
| 298 } |
| 299 |
| 300 // Center the text horizontally. |
| 301 SkScalar sk_horizontal_margin = |
| 302 (canvas_size.width() - display_text_width) / 2.0; |
| 303 canvas.drawText(display_text.c_str(), display_text.size(), |
| 304 sk_horizontal_margin, sk_vertical_margin, paint); |
| 305 |
| 306 Context3D context_3d; |
| 307 if (!context_3d_cb_.is_null()) |
| 308 context_3d = context_3d_cb_.Run(); |
| 309 if (!context_3d.gl) |
| 310 return; |
| 311 |
| 312 GLES2Interface* gl = context_3d.gl; |
| 313 GLuint remote_playback_texture_id = 0; |
| 314 gl->GenTextures(1, &remote_playback_texture_id); |
| 315 GLuint texture_target = GL_TEXTURE_2D; |
| 316 gl->BindTexture(texture_target, remote_playback_texture_id); |
| 317 gl->TexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 318 gl->TexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 319 gl->TexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 320 gl->TexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 321 |
| 322 { |
| 323 SkAutoLockPixels lock(bitmap); |
| 324 gl->TexImage2D(texture_target, 0 /* level */, GL_RGBA /* internalformat */, |
| 325 bitmap.width(), bitmap.height(), 0 /* border */, |
| 326 GL_RGBA /* format */, GL_UNSIGNED_BYTE /* type */, |
| 327 bitmap.getPixels()); |
| 328 } |
| 329 |
| 330 gpu::Mailbox texture_mailbox; |
| 331 gl->GenMailboxCHROMIUM(texture_mailbox.name); |
| 332 gl->ProduceTextureCHROMIUM(texture_target, texture_mailbox.name); |
| 333 gl->Flush(); |
| 334 gpu::SyncToken texture_mailbox_sync_token(gl->InsertSyncPointCHROMIUM()); |
| 335 |
| 336 scoped_refptr<VideoFrame> new_frame = VideoFrame::WrapNativeTexture( |
| 337 media::PIXEL_FORMAT_ARGB, |
| 338 gpu::MailboxHolder(texture_mailbox, texture_mailbox_sync_token, |
| 339 texture_target), |
| 340 media::BindToCurrentLoop(base::Bind(&OnReleaseTexture, context_3d_cb_, |
| 341 remote_playback_texture_id)), |
| 342 canvas_size /* coded_size */, gfx::Rect(canvas_size) /* visible_rect */, |
| 343 canvas_size /* natural_size */, base::TimeDelta() /* timestamp */); |
| 344 |
| 345 webmediaplayer_->SuspendForRemote(new_frame); |
| 346 } |
| 347 |
| 348 } // namespace media |
| 349 |
| 350 #endif |
OLD | NEW |