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 25 matching lines...) Expand all Loading... |
36 #include "media/base/cdm_context.h" | 36 #include "media/base/cdm_context.h" |
37 #include "media/base/key_systems.h" | 37 #include "media/base/key_systems.h" |
38 #include "media/base/media_keys.h" | 38 #include "media/base/media_keys.h" |
39 #include "media/base/media_log.h" | 39 #include "media/base/media_log.h" |
40 #include "media/base/media_switches.h" | 40 #include "media/base/media_switches.h" |
41 #include "media/base/video_frame.h" | 41 #include "media/base/video_frame.h" |
42 #include "media/blink/webcontentdecryptionmodule_impl.h" | 42 #include "media/blink/webcontentdecryptionmodule_impl.h" |
43 #include "media/blink/webmediaplayer_delegate.h" | 43 #include "media/blink/webmediaplayer_delegate.h" |
44 #include "media/blink/webmediaplayer_util.h" | 44 #include "media/blink/webmediaplayer_util.h" |
45 #include "net/base/mime_util.h" | 45 #include "net/base/mime_util.h" |
46 #include "third_party/WebKit/public/platform/Platform.h" | |
47 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h" | 46 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h" |
48 #include "third_party/WebKit/public/platform/WebGraphicsContext3DProvider.h" | 47 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" |
49 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" | 48 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" |
50 #include "third_party/WebKit/public/platform/WebString.h" | 49 #include "third_party/WebKit/public/platform/WebString.h" |
51 #include "third_party/WebKit/public/platform/WebURL.h" | 50 #include "third_party/WebKit/public/platform/WebURL.h" |
52 #include "third_party/WebKit/public/web/WebDocument.h" | 51 #include "third_party/WebKit/public/web/WebDocument.h" |
53 #include "third_party/WebKit/public/web/WebFrame.h" | 52 #include "third_party/WebKit/public/web/WebFrame.h" |
54 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" | 53 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" |
55 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 54 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
56 #include "third_party/WebKit/public/web/WebView.h" | 55 #include "third_party/WebKit/public/web/WebView.h" |
57 #include "third_party/skia/include/core/SkCanvas.h" | 56 #include "third_party/skia/include/core/SkCanvas.h" |
58 #include "third_party/skia/include/core/SkPaint.h" | |
59 #include "third_party/skia/include/core/SkTypeface.h" | 57 #include "third_party/skia/include/core/SkTypeface.h" |
60 #include "ui/gfx/image/image.h" | 58 #include "ui/gfx/image/image.h" |
| 59 #include "webkit/common/gpu/context_provider_web_context.h" |
| 60 #include "webkit/common/gpu/webgraphicscontext3d_impl.h" |
61 | 61 |
62 static const uint32 kGLTextureExternalOES = 0x8D65; | 62 static const uint32 kGLTextureExternalOES = 0x8D65; |
63 static const int kSDKVersionToSupportSecurityOriginCheck = 20; | 63 static const int kSDKVersionToSupportSecurityOriginCheck = 20; |
64 | 64 |
65 using blink::WebMediaPlayer; | 65 using blink::WebMediaPlayer; |
66 using blink::WebSize; | 66 using blink::WebSize; |
67 using blink::WebString; | 67 using blink::WebString; |
68 using blink::WebURL; | 68 using blink::WebURL; |
69 using gpu::gles2::GLES2Interface; | 69 using gpu::gles2::GLES2Interface; |
70 using media::MediaPlayerAndroid; | 70 using media::MediaPlayerAndroid; |
71 using media::VideoFrame; | 71 using media::VideoFrame; |
72 | 72 |
73 namespace { | 73 namespace { |
74 // Prefix for histograms related to Encrypted Media Extensions. | 74 // Prefix for histograms related to Encrypted Media Extensions. |
75 const char* kMediaEme = "Media.EME."; | 75 const char* kMediaEme = "Media.EME."; |
76 | 76 |
77 // 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. |
78 void OnReleaseTexture( | 78 void OnReleaseTexture( |
79 const scoped_refptr<content::StreamTextureFactory>& factories, | 79 const scoped_refptr<content::StreamTextureFactory>& factories, |
80 uint32 texture_id, | 80 uint32 texture_id, |
81 uint32 release_sync_point) { | 81 uint32 release_sync_point) { |
82 GLES2Interface* gl = factories->ContextGL(); | 82 GLES2Interface* gl = factories->ContextGL(); |
83 gl->WaitSyncPointCHROMIUM(release_sync_point); | 83 gl->WaitSyncPointCHROMIUM(release_sync_point); |
84 gl->DeleteTextures(1, &texture_id); | 84 gl->DeleteTextures(1, &texture_id); |
85 } | 85 } |
86 | 86 |
87 class SyncPointClientImpl : public media::VideoFrame::SyncPointClient { | |
88 public: | |
89 explicit SyncPointClientImpl( | |
90 blink::WebGraphicsContext3D* web_graphics_context) | |
91 : web_graphics_context_(web_graphics_context) {} | |
92 virtual ~SyncPointClientImpl() {} | |
93 virtual uint32 InsertSyncPoint() override { | |
94 return web_graphics_context_->insertSyncPoint(); | |
95 } | |
96 virtual void WaitSyncPoint(uint32 sync_point) override { | |
97 web_graphics_context_->waitSyncPoint(sync_point); | |
98 } | |
99 | |
100 private: | |
101 blink::WebGraphicsContext3D* web_graphics_context_; | |
102 }; | |
103 | |
104 } // namespace | 87 } // namespace |
105 | 88 |
106 namespace content { | 89 namespace content { |
107 | 90 |
108 WebMediaPlayerAndroid::WebMediaPlayerAndroid( | 91 WebMediaPlayerAndroid::WebMediaPlayerAndroid( |
109 blink::WebFrame* frame, | 92 blink::WebFrame* frame, |
110 blink::WebMediaPlayerClient* client, | 93 blink::WebMediaPlayerClient* client, |
111 base::WeakPtr<media::WebMediaPlayerDelegate> delegate, | 94 base::WeakPtr<media::WebMediaPlayerDelegate> delegate, |
112 RendererMediaPlayerManager* player_manager, | 95 RendererMediaPlayerManager* player_manager, |
113 RendererCdmManager* cdm_manager, | 96 RendererCdmManager* cdm_manager, |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 const blink::WebTimeRange seekable_range(0.0, duration()); | 515 const blink::WebTimeRange seekable_range(0.0, duration()); |
533 return blink::WebTimeRanges(&seekable_range, 1); | 516 return blink::WebTimeRanges(&seekable_range, 1); |
534 } | 517 } |
535 | 518 |
536 bool WebMediaPlayerAndroid::didLoadingProgress() { | 519 bool WebMediaPlayerAndroid::didLoadingProgress() { |
537 bool ret = did_loading_progress_; | 520 bool ret = did_loading_progress_; |
538 did_loading_progress_ = false; | 521 did_loading_progress_ = false; |
539 return ret; | 522 return ret; |
540 } | 523 } |
541 | 524 |
542 bool WebMediaPlayerAndroid::EnsureTextureBackedSkBitmap(GrContext* gr, | |
543 SkBitmap& bitmap, | |
544 const WebSize& size, | |
545 GrSurfaceOrigin origin, | |
546 GrPixelConfig config) { | |
547 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
548 if (!bitmap.getTexture() || bitmap.width() != size.width | |
549 || bitmap.height() != size.height) { | |
550 if (!gr) | |
551 return false; | |
552 GrTextureDesc desc; | |
553 desc.fConfig = config; | |
554 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; | |
555 desc.fSampleCnt = 0; | |
556 desc.fOrigin = origin; | |
557 desc.fWidth = size.width; | |
558 desc.fHeight = size.height; | |
559 skia::RefPtr<GrTexture> texture; | |
560 texture = skia::AdoptRef(gr->createUncachedTexture(desc, 0, 0)); | |
561 if (!texture.get()) | |
562 return false; | |
563 | |
564 SkImageInfo info = SkImageInfo::MakeN32Premul(desc.fWidth, desc.fHeight); | |
565 SkGrPixelRef* pixelRef = SkNEW_ARGS(SkGrPixelRef, (info, texture.get())); | |
566 if (!pixelRef) | |
567 return false; | |
568 bitmap.setInfo(info); | |
569 bitmap.setPixelRef(pixelRef)->unref(); | |
570 } | |
571 | |
572 return true; | |
573 } | |
574 | |
575 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas, | 525 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas, |
576 const blink::WebRect& rect, | 526 const blink::WebRect& rect, |
577 unsigned char alpha, | 527 unsigned char alpha, |
578 SkXfermode::Mode mode) { | 528 SkXfermode::Mode mode) { |
579 DCHECK(main_thread_checker_.CalledOnValidThread()); | 529 DCHECK(main_thread_checker_.CalledOnValidThread()); |
580 scoped_ptr<blink::WebGraphicsContext3DProvider> provider = | 530 // Don't allow clients to copy an encrypted video frame. |
581 scoped_ptr<blink::WebGraphicsContext3DProvider>(blink::Platform::current( | 531 if (needs_external_surface_) |
582 )->createSharedOffscreenGraphicsContext3DProvider()); | |
583 if (!provider) | |
584 return; | |
585 blink::WebGraphicsContext3D* context3D = provider->context3d(); | |
586 if (!context3D) | |
587 return; | 532 return; |
588 | 533 |
589 // Copy video texture into a RGBA texture based bitmap first as video texture | 534 scoped_refptr<VideoFrame> video_frame; |
590 // on Android is GL_TEXTURE_EXTERNAL_OES which is not supported by Skia yet. | 535 { |
591 // The bitmap's size needs to be the same as the video and use naturalSize() | 536 base::AutoLock auto_lock(current_frame_lock_); |
592 // here. Check if we could reuse existing texture based bitmap. | 537 video_frame = current_frame_; |
593 // Otherwise, release existing texture based bitmap and allocate | |
594 // a new one based on video size. | |
595 if (!EnsureTextureBackedSkBitmap(provider->grContext(), bitmap_, | |
596 naturalSize(), kTopLeft_GrSurfaceOrigin, kSkia8888_GrPixelConfig)) { | |
597 return; | |
598 } | 538 } |
599 | 539 |
600 unsigned textureId = static_cast<unsigned>( | 540 gfx::Rect gfx_rect(rect); |
601 (bitmap_.getTexture())->getTextureHandle()); | 541 media::Context3D context_3d; |
602 if (!copyVideoTextureToPlatformTexture(context3D, textureId, 0, | 542 if (video_frame.get() && |
603 GL_RGBA, GL_UNSIGNED_BYTE, true, false)) { | 543 video_frame->format() == media::VideoFrame::NATIVE_TEXTURE) { |
604 return; | 544 scoped_refptr<cc::ContextProvider> context_provider = |
| 545 RenderThreadImpl::current()->SharedMainThreadContextProvider(); |
| 546 // GPU Process crashed. |
| 547 if (!context_provider.get()) |
| 548 return; |
| 549 context_3d = media::Context3D(context_provider->ContextGL(), |
| 550 context_provider->GrContext()); |
605 } | 551 } |
606 | 552 skcanvas_video_renderer_.Paint(video_frame, canvas, gfx_rect, alpha, mode, |
607 // Draw the texture based bitmap onto the Canvas. If the canvas is | 553 media::VIDEO_ROTATION_0, context_3d); |
608 // hardware based, this will do a GPU-GPU texture copy. | |
609 // If the canvas is software based, the texture based bitmap will be | |
610 // readbacked to system memory then draw onto the canvas. | |
611 SkRect dest; | |
612 dest.set(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height); | |
613 SkPaint paint; | |
614 paint.setAlpha(alpha); | |
615 paint.setXfermodeMode(mode); | |
616 // It is not necessary to pass the dest into the drawBitmap call since all | |
617 // the context have been set up before calling paintCurrentFrameInContext. | |
618 canvas->drawBitmapRect(bitmap_, 0, dest, &paint); | |
619 } | 554 } |
620 | 555 |
621 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture( | 556 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture( |
622 blink::WebGraphicsContext3D* web_graphics_context, | 557 blink::WebGraphicsContext3D* web_graphics_context, |
623 unsigned int texture, | 558 unsigned int texture, |
624 unsigned int level, | 559 unsigned int level, |
625 unsigned int internal_format, | 560 unsigned int internal_format, |
626 unsigned int type, | 561 unsigned int type, |
627 bool premultiply_alpha, | 562 bool premultiply_alpha, |
628 bool flip_y) { | 563 bool flip_y) { |
629 DCHECK(main_thread_checker_.CalledOnValidThread()); | 564 DCHECK(main_thread_checker_.CalledOnValidThread()); |
630 // Don't allow clients to copy an encrypted video frame. | 565 // Don't allow clients to copy an encrypted video frame. |
631 if (needs_external_surface_) | 566 if (needs_external_surface_) |
632 return false; | 567 return false; |
633 | 568 |
634 scoped_refptr<VideoFrame> video_frame; | 569 scoped_refptr<VideoFrame> video_frame; |
635 { | 570 { |
636 base::AutoLock auto_lock(current_frame_lock_); | 571 base::AutoLock auto_lock(current_frame_lock_); |
637 video_frame = current_frame_; | 572 video_frame = current_frame_; |
638 } | 573 } |
639 | 574 |
640 if (!video_frame.get() || | 575 if (!video_frame.get() || |
641 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE) | 576 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE) |
642 return false; | 577 return false; |
| 578 |
| 579 #if DCHECK_IS_ON |
643 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder(); | 580 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder(); |
644 DCHECK((!is_remote_ && | 581 DCHECK((!is_remote_ && |
645 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) || | 582 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) || |
646 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D)); | 583 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D)); |
| 584 #endif |
647 | 585 |
648 web_graphics_context->waitSyncPoint(mailbox_holder->sync_point); | 586 // TODO(dshwang): need more elegant way to convert WebGraphicsContext3D to |
649 | 587 // GLES2Interface. |
650 // Ensure the target of texture is set before copyTextureCHROMIUM, otherwise | 588 gpu::gles2::GLES2Interface* gl = |
651 // an invalid texture target may be used for copy texture. | 589 static_cast<webkit::gpu::WebGraphicsContext3DImpl*>(web_graphics_context) |
652 uint32 src_texture = web_graphics_context->createAndConsumeTextureCHROMIUM( | 590 ->GetGLInterface(); |
653 mailbox_holder->texture_target, mailbox_holder->mailbox.name); | 591 media::SkCanvasVideoRenderer::CopyVideoFrameTextureToGLTexture( |
654 | 592 gl, video_frame.get(), texture, level, internal_format, type, |
655 // The video is stored in an unmultiplied format, so premultiply if | 593 premultiply_alpha, flip_y); |
656 // necessary. | |
657 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, | |
658 premultiply_alpha); | |
659 | |
660 // Application itself needs to take care of setting the right flip_y | |
661 // value down to get the expected result. | |
662 // flip_y==true means to reverse the video orientation while | |
663 // flip_y==false means to keep the intrinsic orientation. | |
664 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y); | |
665 web_graphics_context->copyTextureCHROMIUM(GL_TEXTURE_2D, src_texture, | |
666 texture, level, internal_format, | |
667 type); | |
668 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false); | |
669 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM, | |
670 false); | |
671 | |
672 web_graphics_context->deleteTexture(src_texture); | |
673 web_graphics_context->flush(); | |
674 | |
675 SyncPointClientImpl client(web_graphics_context); | |
676 video_frame->UpdateReleaseSyncPoint(&client); | |
677 return true; | 594 return true; |
678 } | 595 } |
679 | 596 |
680 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const { | 597 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const { |
681 DCHECK(main_thread_checker_.CalledOnValidThread()); | 598 DCHECK(main_thread_checker_.CalledOnValidThread()); |
682 if (player_type_ != MEDIA_PLAYER_TYPE_URL) | 599 if (player_type_ != MEDIA_PLAYER_TYPE_URL) |
683 return true; | 600 return true; |
684 | 601 |
685 if (!info_loader_ || !info_loader_->HasSingleOrigin()) | 602 if (!info_loader_ || !info_loader_->HasSingleOrigin()) |
686 return false; | 603 return false; |
(...skipping 1102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1789 | 1706 |
1790 bool WebMediaPlayerAndroid::IsHLSStream() const { | 1707 bool WebMediaPlayerAndroid::IsHLSStream() const { |
1791 std::string mime; | 1708 std::string mime; |
1792 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_; | 1709 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_; |
1793 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime)) | 1710 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime)) |
1794 return false; | 1711 return false; |
1795 return !mime.compare("application/x-mpegurl"); | 1712 return !mime.compare("application/x-mpegurl"); |
1796 } | 1713 } |
1797 | 1714 |
1798 } // namespace content | 1715 } // namespace content |
OLD | NEW |