| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/android/webmediaplayer_android.h" | 5 #include "content/renderer/media/android/webmediaplayer_android.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/android/build_info.h" | 9 #include "base/android/build_info.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "gpu/command_buffer/client/gles2_interface.h" | 34 #include "gpu/command_buffer/client/gles2_interface.h" |
| 35 #include "gpu/command_buffer/common/mailbox_holder.h" | 35 #include "gpu/command_buffer/common/mailbox_holder.h" |
| 36 #include "media/base/android/media_player_android.h" | 36 #include "media/base/android/media_player_android.h" |
| 37 #include "media/base/bind_to_current_loop.h" | 37 #include "media/base/bind_to_current_loop.h" |
| 38 // TODO(xhwang): Remove when we remove prefixed EME implementation. | 38 // TODO(xhwang): Remove when we remove prefixed EME implementation. |
| 39 #include "media/base/media_keys.h" | 39 #include "media/base/media_keys.h" |
| 40 #include "media/base/media_log.h" | 40 #include "media/base/media_log.h" |
| 41 #include "media/base/media_switches.h" | 41 #include "media/base/media_switches.h" |
| 42 #include "media/base/video_frame.h" | 42 #include "media/base/video_frame.h" |
| 43 #include "net/base/mime_util.h" | 43 #include "net/base/mime_util.h" |
| 44 #include "third_party/WebKit/public/platform/Platform.h" | |
| 45 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h" | 44 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h" |
| 46 #include "third_party/WebKit/public/platform/WebGraphicsContext3DProvider.h" | 45 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" |
| 47 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" | 46 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" |
| 48 #include "third_party/WebKit/public/platform/WebString.h" | 47 #include "third_party/WebKit/public/platform/WebString.h" |
| 49 #include "third_party/WebKit/public/platform/WebURL.h" | 48 #include "third_party/WebKit/public/platform/WebURL.h" |
| 50 #include "third_party/WebKit/public/web/WebDocument.h" | 49 #include "third_party/WebKit/public/web/WebDocument.h" |
| 51 #include "third_party/WebKit/public/web/WebFrame.h" | 50 #include "third_party/WebKit/public/web/WebFrame.h" |
| 52 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" | 51 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" |
| 53 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 52 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
| 54 #include "third_party/WebKit/public/web/WebView.h" | 53 #include "third_party/WebKit/public/web/WebView.h" |
| 54 #include "third_party/skia/include/core/SkBitmap.h" |
| 55 #include "third_party/skia/include/core/SkCanvas.h" | 55 #include "third_party/skia/include/core/SkCanvas.h" |
| 56 #include "third_party/skia/include/core/SkPaint.h" | |
| 57 #include "third_party/skia/include/core/SkTypeface.h" | 56 #include "third_party/skia/include/core/SkTypeface.h" |
| 58 #include "ui/gfx/image/image.h" | 57 #include "ui/gfx/image/image.h" |
| 58 #include "webkit/common/gpu/context_provider_web_context.h" |
| 59 #include "webkit/common/gpu/webgraphicscontext3d_impl.h" |
| 59 | 60 |
| 60 static const uint32 kGLTextureExternalOES = 0x8D65; | 61 static const uint32 kGLTextureExternalOES = 0x8D65; |
| 61 static const int kSDKVersionToSupportSecurityOriginCheck = 20; | 62 static const int kSDKVersionToSupportSecurityOriginCheck = 20; |
| 62 | 63 |
| 63 using blink::WebMediaPlayer; | 64 using blink::WebMediaPlayer; |
| 64 using blink::WebSize; | 65 using blink::WebSize; |
| 65 using blink::WebString; | 66 using blink::WebString; |
| 66 using blink::WebTimeRanges; | 67 using blink::WebTimeRanges; |
| 67 using blink::WebURL; | 68 using blink::WebURL; |
| 68 using gpu::gles2::GLES2Interface; | 69 using gpu::gles2::GLES2Interface; |
| 69 using media::MediaPlayerAndroid; | 70 using media::MediaPlayerAndroid; |
| 70 using media::VideoFrame; | 71 using media::VideoFrame; |
| 71 | 72 |
| 72 namespace { | 73 namespace { |
| 73 // Prefix for histograms related to Encrypted Media Extensions. | 74 // Prefix for histograms related to Encrypted Media Extensions. |
| 74 const char* kMediaEme = "Media.EME."; | 75 const char* kMediaEme = "Media.EME."; |
| 75 | 76 |
| 76 // File-static function is to allow it to run even after WMPA is deleted. | 77 // File-static function is to allow it to run even after WMPA is deleted. |
| 77 void OnReleaseTexture( | 78 void OnReleaseTexture( |
| 78 const scoped_refptr<content::StreamTextureFactory>& factories, | 79 const scoped_refptr<content::StreamTextureFactory>& factories, |
| 79 uint32 texture_id, | 80 uint32 texture_id, |
| 80 uint32 release_sync_point) { | 81 uint32 release_sync_point) { |
| 81 GLES2Interface* gl = factories->ContextGL(); | 82 GLES2Interface* gl = factories->ContextGL(); |
| 82 gl->WaitSyncPointCHROMIUM(release_sync_point); | 83 gl->WaitSyncPointCHROMIUM(release_sync_point); |
| 83 gl->DeleteTextures(1, &texture_id); | 84 gl->DeleteTextures(1, &texture_id); |
| 84 } | 85 } |
| 85 | 86 |
| 86 class SyncPointClientImpl : public media::VideoFrame::SyncPointClient { | |
| 87 public: | |
| 88 explicit SyncPointClientImpl( | |
| 89 blink::WebGraphicsContext3D* web_graphics_context) | |
| 90 : web_graphics_context_(web_graphics_context) {} | |
| 91 virtual ~SyncPointClientImpl() {} | |
| 92 virtual uint32 InsertSyncPoint() OVERRIDE { | |
| 93 return web_graphics_context_->insertSyncPoint(); | |
| 94 } | |
| 95 virtual void WaitSyncPoint(uint32 sync_point) OVERRIDE { | |
| 96 web_graphics_context_->waitSyncPoint(sync_point); | |
| 97 } | |
| 98 | |
| 99 private: | |
| 100 blink::WebGraphicsContext3D* web_graphics_context_; | |
| 101 }; | |
| 102 | |
| 103 // Used for calls to decryptor_ready_cb_ where the result can be ignored. | 87 // Used for calls to decryptor_ready_cb_ where the result can be ignored. |
| 104 void DoNothing(bool) { | 88 void DoNothing(bool) { |
| 105 } | 89 } |
| 106 | 90 |
| 107 } // namespace | 91 } // namespace |
| 108 | 92 |
| 109 namespace content { | 93 namespace content { |
| 110 | 94 |
| 111 WebMediaPlayerAndroid::WebMediaPlayerAndroid( | 95 WebMediaPlayerAndroid::WebMediaPlayerAndroid( |
| 112 blink::WebFrame* frame, | 96 blink::WebFrame* frame, |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 | 459 |
| 476 return duration(); | 460 return duration(); |
| 477 } | 461 } |
| 478 | 462 |
| 479 bool WebMediaPlayerAndroid::didLoadingProgress() { | 463 bool WebMediaPlayerAndroid::didLoadingProgress() { |
| 480 bool ret = did_loading_progress_; | 464 bool ret = did_loading_progress_; |
| 481 did_loading_progress_ = false; | 465 did_loading_progress_ = false; |
| 482 return ret; | 466 return ret; |
| 483 } | 467 } |
| 484 | 468 |
| 485 bool WebMediaPlayerAndroid::EnsureTextureBackedSkBitmap(GrContext* gr, | |
| 486 SkBitmap& bitmap, | |
| 487 const WebSize& size, | |
| 488 GrSurfaceOrigin origin, | |
| 489 GrPixelConfig config) { | |
| 490 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
| 491 if (!bitmap.getTexture() || bitmap.width() != size.width | |
| 492 || bitmap.height() != size.height) { | |
| 493 if (!gr) | |
| 494 return false; | |
| 495 GrTextureDesc desc; | |
| 496 desc.fConfig = config; | |
| 497 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; | |
| 498 desc.fSampleCnt = 0; | |
| 499 desc.fOrigin = origin; | |
| 500 desc.fWidth = size.width; | |
| 501 desc.fHeight = size.height; | |
| 502 skia::RefPtr<GrTexture> texture; | |
| 503 texture = skia::AdoptRef(gr->createUncachedTexture(desc, 0, 0)); | |
| 504 if (!texture.get()) | |
| 505 return false; | |
| 506 | |
| 507 SkImageInfo info = SkImageInfo::MakeN32Premul(desc.fWidth, desc.fHeight); | |
| 508 SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, texture.get())); | |
| 509 if (!pixelRef) | |
| 510 return false; | |
| 511 bitmap.setInfo(info); | |
| 512 bitmap.setPixelRef(pixelRef)->unref(); | |
| 513 } | |
| 514 | |
| 515 return true; | |
| 516 } | |
| 517 | |
| 518 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas, | 469 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas, |
| 519 const blink::WebRect& rect, | 470 const blink::WebRect& rect, |
| 520 unsigned char alpha) { | 471 unsigned char alpha) { |
| 521 paint(canvas, rect, alpha, SkXfermode::kSrcOver_Mode); | 472 paint(canvas, rect, alpha, SkXfermode::kSrcOver_Mode); |
| 522 } | 473 } |
| 523 | 474 |
| 524 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas, | 475 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas, |
| 525 const blink::WebRect& rect, | 476 const blink::WebRect& rect, |
| 526 unsigned char alpha, | 477 unsigned char alpha, |
| 527 SkXfermode::Mode mode) { | 478 SkXfermode::Mode mode) { |
| 528 DCHECK(main_thread_checker_.CalledOnValidThread()); | 479 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 529 scoped_ptr<blink::WebGraphicsContext3DProvider> provider = | 480 // Don't allow clients to copy an encrypted video frame. |
| 530 scoped_ptr<blink::WebGraphicsContext3DProvider>(blink::Platform::current( | 481 if (needs_external_surface_) |
| 531 )->createSharedOffscreenGraphicsContext3DProvider()); | |
| 532 if (!provider) | |
| 533 return; | |
| 534 blink::WebGraphicsContext3D* context3D = provider->context3d(); | |
| 535 if (!context3D || !context3D->makeContextCurrent()) | |
| 536 return; | 482 return; |
| 537 | 483 |
| 538 // Copy video texture into a RGBA texture based bitmap first as video texture | 484 scoped_refptr<VideoFrame> video_frame; |
| 539 // on Android is GL_TEXTURE_EXTERNAL_OES which is not supported by Skia yet. | 485 { |
| 540 // The bitmap's size needs to be the same as the video and use naturalSize() | 486 base::AutoLock auto_lock(current_frame_lock_); |
| 541 // here. Check if we could reuse existing texture based bitmap. | 487 video_frame = current_frame_; |
| 542 // Otherwise, release existing texture based bitmap and allocate | |
| 543 // a new one based on video size. | |
| 544 if (!EnsureTextureBackedSkBitmap(provider->grContext(), bitmap_, | |
| 545 naturalSize(), kTopLeft_GrSurfaceOrigin, kSkia8888_GrPixelConfig)) { | |
| 546 return; | |
| 547 } | 488 } |
| 548 | 489 |
| 549 unsigned textureId = static_cast<unsigned>( | 490 gfx::Rect gfx_rect(rect); |
| 550 (bitmap_.getTexture())->getTextureHandle()); | 491 scoped_refptr<cc::ContextProvider> context_provider = |
| 551 if (!copyVideoTextureToPlatformTexture(context3D, textureId, 0, | 492 RenderThreadImpl::current()->SharedMainThreadContextProvider(); |
| 552 GL_RGBA, GL_UNSIGNED_BYTE, true, false)) { | 493 media::SkCanvasVideoRenderer::Context3DProvider provider{ |
| 553 return; | 494 context_provider->ContextGL(), context_provider->GrContext()}; |
| 554 } | 495 skcanvas_video_renderer_.Paint(video_frame.get(), |
| 555 | 496 canvas, |
| 556 // Draw the texture based bitmap onto the Canvas. If the canvas is | 497 gfx_rect, |
| 557 // hardware based, this will do a GPU-GPU texture copy. | 498 alpha, |
| 558 // If the canvas is software based, the texture based bitmap will be | 499 mode, |
| 559 // readbacked to system memory then draw onto the canvas. | 500 media::VIDEO_ROTATION_0, |
| 560 SkRect dest; | 501 &provider); |
| 561 dest.set(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height); | |
| 562 SkPaint paint; | |
| 563 paint.setAlpha(alpha); | |
| 564 paint.setXfermodeMode(mode); | |
| 565 // It is not necessary to pass the dest into the drawBitmap call since all | |
| 566 // the context have been set up before calling paintCurrentFrameInContext. | |
| 567 canvas->drawBitmapRect(bitmap_, 0, dest, &paint); | |
| 568 } | 502 } |
| 569 | 503 |
| 570 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture( | 504 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture( |
| 571 blink::WebGraphicsContext3D* web_graphics_context, | 505 blink::WebGraphicsContext3D* web_graphics_context, |
| 572 unsigned int texture, | 506 unsigned int texture, |
| 573 unsigned int level, | 507 unsigned int level, |
| 574 unsigned int internal_format, | 508 unsigned int internal_format, |
| 575 unsigned int type, | 509 unsigned int type, |
| 576 bool premultiply_alpha, | 510 bool premultiply_alpha, |
| 577 bool flip_y) { | 511 bool flip_y) { |
| 578 DCHECK(main_thread_checker_.CalledOnValidThread()); | 512 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 579 // Don't allow clients to copy an encrypted video frame. | 513 // Don't allow clients to copy an encrypted video frame. |
| 580 if (needs_external_surface_) | 514 if (needs_external_surface_) |
| 581 return false; | 515 return false; |
| 582 | 516 |
| 583 scoped_refptr<VideoFrame> video_frame; | 517 scoped_refptr<VideoFrame> video_frame; |
| 584 { | 518 { |
| 585 base::AutoLock auto_lock(current_frame_lock_); | 519 base::AutoLock auto_lock(current_frame_lock_); |
| 586 video_frame = current_frame_; | 520 video_frame = current_frame_; |
| 587 } | 521 } |
| 588 | 522 |
| 589 if (!video_frame || | 523 if (!video_frame || |
| 590 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE) | 524 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE) |
| 591 return false; | 525 return false; |
| 526 |
| 527 #if DCHECK_IS_ON |
| 592 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder(); | 528 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder(); |
| 593 DCHECK((!is_remote_ && | 529 DCHECK((!is_remote_ && |
| 594 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) || | 530 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) || |
| 595 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D)); | 531 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D)); |
| 532 #endif |
| 596 | 533 |
| 597 web_graphics_context->waitSyncPoint(mailbox_holder->sync_point); | 534 // For hidden video element (with style "display:none"), ensure the texture |
| 535 // size is set. |
| 536 if (!is_remote_ && |
| 537 (cached_stream_texture_size_.width != natural_size_.width || |
| 538 cached_stream_texture_size_.height != natural_size_.height)) { |
| 539 stream_texture_factory_->SetStreamTextureSize( |
| 540 stream_id_, gfx::Size(natural_size_.width, natural_size_.height)); |
| 541 cached_stream_texture_size_ = natural_size_; |
| 542 } |
| 598 | 543 |
| 599 // Ensure the target of texture is set before copyTextureCHROMIUM, otherwise | 544 // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to |
| 600 // an invalid texture target may be used for copy texture. | 545 // GLES2Interface. |
| 601 uint32 src_texture = web_graphics_context->createAndConsumeTextureCHROMIUM( | 546 gpu::gles2::GLES2Interface* gl = |
| 602 mailbox_holder->texture_target, mailbox_holder->mailbox.name); | 547 static_cast<webkit::gpu::WebGraphicsContext3DImpl*>(web_graphics_context) |
| 603 | 548 ->GetGLInterface(); |
| 604 // The video is stored in an unmultiplied format, so premultiply if | 549 skcanvas_video_renderer_.CopyVideoFrameToTexture(gl, |
| 605 // necessary. | 550 video_frame.get(), |
| 606 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, | 551 texture, |
| 607 premultiply_alpha); | 552 level, |
| 608 | 553 internal_format, |
| 609 // Application itself needs to take care of setting the right flip_y | 554 type, |
| 610 // value down to get the expected result. | 555 premultiply_alpha, |
| 611 // flip_y==true means to reverse the video orientation while | 556 flip_y); |
| 612 // flip_y==false means to keep the intrinsic orientation. | |
| 613 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y); | |
| 614 web_graphics_context->copyTextureCHROMIUM(GL_TEXTURE_2D, src_texture, | |
| 615 texture, level, internal_format, | |
| 616 type); | |
| 617 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false); | |
| 618 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, | |
| 619 false); | |
| 620 | |
| 621 web_graphics_context->deleteTexture(src_texture); | |
| 622 web_graphics_context->flush(); | |
| 623 | |
| 624 SyncPointClientImpl client(web_graphics_context); | |
| 625 video_frame->UpdateReleaseSyncPoint(&client); | |
| 626 return true; | 557 return true; |
| 627 } | 558 } |
| 628 | 559 |
| 629 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const { | 560 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const { |
| 630 DCHECK(main_thread_checker_.CalledOnValidThread()); | 561 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 631 if (player_type_ != MEDIA_PLAYER_TYPE_URL) | 562 if (player_type_ != MEDIA_PLAYER_TYPE_URL) |
| 632 return true; | 563 return true; |
| 633 | 564 |
| 634 if (!info_loader_ || !info_loader_->HasSingleOrigin()) | 565 if (!info_loader_ || !info_loader_->HasSingleOrigin()) |
| 635 return false; | 566 return false; |
| (...skipping 1104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1740 } | 1671 } |
| 1741 | 1672 |
| 1742 bool WebMediaPlayerAndroid::IsHLSStream() const { | 1673 bool WebMediaPlayerAndroid::IsHLSStream() const { |
| 1743 std::string mime; | 1674 std::string mime; |
| 1744 if (!net::GetMimeTypeFromFile(base::FilePath(url_.path()), &mime)) | 1675 if (!net::GetMimeTypeFromFile(base::FilePath(url_.path()), &mime)) |
| 1745 return false; | 1676 return false; |
| 1746 return !mime.compare("application/x-mpegurl"); | 1677 return !mime.compare("application/x-mpegurl"); |
| 1747 } | 1678 } |
| 1748 | 1679 |
| 1749 } // namespace content | 1680 } // namespace content |
| OLD | NEW |