Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / media / android / webmediaplayer_android.cc
blobe04d8f436f37a02e8306469be59bc6b656fbbfd5
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
3 // found in the LICENSE file.
5 #include "content/renderer/media/android/webmediaplayer_android.h"
7 #include <limits>
9 #include "base/android/build_info.h"
10 #include "base/bind.h"
11 #include "base/callback_helpers.h"
12 #include "base/command_line.h"
13 #include "base/files/file_path.h"
14 #include "base/logging.h"
15 #include "base/metrics/histogram.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "cc/blink/web_layer_impl.h"
20 #include "cc/layers/video_layer.h"
21 #include "content/public/common/content_client.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/common/renderer_preferences.h"
24 #include "content/public/renderer/render_frame.h"
25 #include "content/renderer/media/android/renderer_demuxer_android.h"
26 #include "content/renderer/media/android/renderer_media_player_manager.h"
27 #include "content/renderer/media/crypto/render_cdm_factory.h"
28 #include "content/renderer/media/crypto/renderer_cdm_manager.h"
29 #include "content/renderer/render_frame_impl.h"
30 #include "content/renderer/render_thread_impl.h"
31 #include "content/renderer/render_view_impl.h"
32 #include "gpu/GLES2/gl2extchromium.h"
33 #include "gpu/command_buffer/client/gles2_interface.h"
34 #include "gpu/command_buffer/common/mailbox_holder.h"
35 #include "media/base/android/media_common_android.h"
36 #include "media/base/android/media_player_android.h"
37 #include "media/base/bind_to_current_loop.h"
38 #include "media/base/cdm_context.h"
39 #include "media/base/key_systems.h"
40 #include "media/base/media_keys.h"
41 #include "media/base/media_log.h"
42 #include "media/base/media_switches.h"
43 #include "media/base/video_frame.h"
44 #include "media/blink/webcontentdecryptionmodule_impl.h"
45 #include "media/blink/webmediaplayer_delegate.h"
46 #include "media/blink/webmediaplayer_util.h"
47 #include "net/base/mime_util.h"
48 #include "third_party/WebKit/public/platform/Platform.h"
49 #include "third_party/WebKit/public/platform/WebContentDecryptionModuleResult.h"
50 #include "third_party/WebKit/public/platform/WebEncryptedMediaTypes.h"
51 #include "third_party/WebKit/public/platform/WebGraphicsContext3DProvider.h"
52 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h"
53 #include "third_party/WebKit/public/platform/WebMediaPlayerEncryptedMediaClient.h"
54 #include "third_party/WebKit/public/platform/WebString.h"
55 #include "third_party/WebKit/public/platform/WebURL.h"
56 #include "third_party/WebKit/public/web/WebDocument.h"
57 #include "third_party/WebKit/public/web/WebFrame.h"
58 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
59 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
60 #include "third_party/WebKit/public/web/WebView.h"
61 #include "third_party/skia/include/core/SkCanvas.h"
62 #include "third_party/skia/include/core/SkPaint.h"
63 #include "third_party/skia/include/core/SkTypeface.h"
64 #include "third_party/skia/include/gpu/GrContext.h"
65 #include "third_party/skia/include/gpu/SkGrPixelRef.h"
66 #include "ui/gfx/image/image.h"
68 static const uint32 kGLTextureExternalOES = 0x8D65;
69 static const int kSDKVersionToSupportSecurityOriginCheck = 20;
71 using blink::WebMediaPlayer;
72 using blink::WebSize;
73 using blink::WebString;
74 using blink::WebURL;
75 using gpu::gles2::GLES2Interface;
76 using media::MediaPlayerAndroid;
77 using media::VideoFrame;
79 namespace {
80 // Prefix for histograms related to Encrypted Media Extensions.
81 const char* kMediaEme = "Media.EME.";
83 // File-static function is to allow it to run even after WMPA is deleted.
84 void OnReleaseTexture(
85 const scoped_refptr<content::StreamTextureFactory>& factories,
86 uint32 texture_id,
87 uint32 release_sync_point) {
88 GLES2Interface* gl = factories->ContextGL();
89 gl->WaitSyncPointCHROMIUM(release_sync_point);
90 gl->DeleteTextures(1, &texture_id);
91 // Flush to ensure that the stream texture gets deleted in a timely fashion.
92 gl->ShallowFlushCHROMIUM();
95 bool IsSkBitmapProperlySizedTexture(const SkBitmap* bitmap,
96 const gfx::Size& size) {
97 return bitmap->getTexture() && bitmap->width() == size.width() &&
98 bitmap->height() == size.height();
101 bool AllocateSkBitmapTexture(GrContext* gr,
102 SkBitmap* bitmap,
103 const gfx::Size& size) {
104 DCHECK(gr);
105 GrTextureDesc desc;
106 // Use kRGBA_8888_GrPixelConfig, not kSkia8888_GrPixelConfig, to avoid
107 // RGBA to BGRA conversion.
108 desc.fConfig = kRGBA_8888_GrPixelConfig;
109 // kRenderTarget_GrTextureFlagBit avoids a copy before readback in skia.
110 desc.fFlags = kRenderTarget_GrSurfaceFlag;
111 desc.fSampleCnt = 0;
112 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
113 desc.fWidth = size.width();
114 desc.fHeight = size.height();
115 skia::RefPtr<GrTexture> texture = skia::AdoptRef(
116 gr->textureProvider()->refScratchTexture(
117 desc, GrTextureProvider::kExact_ScratchTexMatch));
118 if (!texture.get())
119 return false;
121 SkImageInfo info = SkImageInfo::MakeN32Premul(desc.fWidth, desc.fHeight);
122 SkGrPixelRef* pixel_ref = SkNEW_ARGS(SkGrPixelRef, (info, texture.get()));
123 if (!pixel_ref)
124 return false;
125 bitmap->setInfo(info);
126 bitmap->setPixelRef(pixel_ref)->unref();
127 return true;
130 class SyncPointClientImpl : public media::VideoFrame::SyncPointClient {
131 public:
132 explicit SyncPointClientImpl(
133 blink::WebGraphicsContext3D* web_graphics_context)
134 : web_graphics_context_(web_graphics_context) {}
135 ~SyncPointClientImpl() override {}
136 uint32 InsertSyncPoint() override {
137 return web_graphics_context_->insertSyncPoint();
139 void WaitSyncPoint(uint32 sync_point) override {
140 web_graphics_context_->waitSyncPoint(sync_point);
143 private:
144 blink::WebGraphicsContext3D* web_graphics_context_;
147 } // namespace
149 namespace content {
151 WebMediaPlayerAndroid::WebMediaPlayerAndroid(
152 blink::WebFrame* frame,
153 blink::WebMediaPlayerClient* client,
154 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
155 base::WeakPtr<media::WebMediaPlayerDelegate> delegate,
156 RendererMediaPlayerManager* player_manager,
157 media::CdmFactory* cdm_factory,
158 media::MediaPermission* media_permission,
159 blink::WebContentDecryptionModule* initial_cdm,
160 scoped_refptr<StreamTextureFactory> factory,
161 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
162 media::MediaLog* media_log)
163 : RenderFrameObserver(RenderFrame::FromWebFrame(frame)),
164 frame_(frame),
165 client_(client),
166 encrypted_client_(encrypted_client),
167 delegate_(delegate),
168 buffered_(static_cast<size_t>(1)),
169 media_task_runner_(task_runner),
170 ignore_metadata_duration_change_(false),
171 pending_seek_(false),
172 seeking_(false),
173 did_loading_progress_(false),
174 player_manager_(player_manager),
175 cdm_factory_(cdm_factory),
176 media_permission_(media_permission),
177 network_state_(WebMediaPlayer::NetworkStateEmpty),
178 ready_state_(WebMediaPlayer::ReadyStateHaveNothing),
179 texture_id_(0),
180 stream_id_(0),
181 is_player_initialized_(false),
182 is_playing_(false),
183 needs_establish_peer_(true),
184 has_size_info_(false),
185 // Compositor thread does not exist in layout tests.
186 compositor_loop_(
187 RenderThreadImpl::current()->compositor_task_runner().get()
188 ? RenderThreadImpl::current()->compositor_task_runner()
189 : base::ThreadTaskRunnerHandle::Get()),
190 stream_texture_factory_(factory),
191 needs_external_surface_(false),
192 is_fullscreen_(false),
193 video_frame_provider_client_(NULL),
194 player_type_(MEDIA_PLAYER_TYPE_URL),
195 is_remote_(false),
196 media_log_(media_log),
197 init_data_type_(media::EmeInitDataType::UNKNOWN),
198 cdm_context_(NULL),
199 allow_stored_credentials_(false),
200 is_local_resource_(false),
201 interpolator_(&default_tick_clock_),
202 weak_factory_(this) {
203 DCHECK(player_manager_);
204 DCHECK(cdm_factory_);
206 DCHECK(main_thread_checker_.CalledOnValidThread());
207 stream_texture_factory_->AddObserver(this);
209 player_id_ = player_manager_->RegisterMediaPlayer(this);
211 #if defined(VIDEO_HOLE)
212 const RendererPreferences& prefs =
213 static_cast<RenderFrameImpl*>(render_frame())
214 ->render_view()
215 ->renderer_preferences();
216 force_use_overlay_embedded_video_ = prefs.use_view_overlay_for_all_video;
217 if (force_use_overlay_embedded_video_ ||
218 player_manager_->ShouldUseVideoOverlayForEmbeddedEncryptedVideo()) {
219 // Defer stream texture creation until we are sure it's necessary.
220 needs_establish_peer_ = false;
221 current_frame_ = VideoFrame::CreateBlackFrame(gfx::Size(1, 1));
223 #endif // defined(VIDEO_HOLE)
224 TryCreateStreamTextureProxyIfNeeded();
225 interpolator_.SetUpperBound(base::TimeDelta());
227 if (initial_cdm) {
228 cdm_context_ =
229 media::ToWebContentDecryptionModuleImpl(initial_cdm)->GetCdmContext();
233 WebMediaPlayerAndroid::~WebMediaPlayerAndroid() {
234 DCHECK(main_thread_checker_.CalledOnValidThread());
235 SetVideoFrameProviderClient(NULL);
236 client_->setWebLayer(NULL);
238 if (is_player_initialized_)
239 player_manager_->DestroyPlayer(player_id_);
241 player_manager_->UnregisterMediaPlayer(player_id_);
243 if (stream_id_) {
244 GLES2Interface* gl = stream_texture_factory_->ContextGL();
245 gl->DeleteTextures(1, &texture_id_);
246 // Flush to ensure that the stream texture gets deleted in a timely fashion.
247 gl->ShallowFlushCHROMIUM();
248 texture_id_ = 0;
249 texture_mailbox_ = gpu::Mailbox();
250 stream_id_ = 0;
254 base::AutoLock auto_lock(current_frame_lock_);
255 current_frame_ = NULL;
258 if (delegate_)
259 delegate_->PlayerGone(this);
261 stream_texture_factory_->RemoveObserver(this);
263 if (media_source_delegate_) {
264 // Part of |media_source_delegate_| needs to be stopped on the media thread.
265 // Wait until |media_source_delegate_| is fully stopped before tearing
266 // down other objects.
267 base::WaitableEvent waiter(false, false);
268 media_source_delegate_->Stop(
269 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&waiter)));
270 waiter.Wait();
274 void WebMediaPlayerAndroid::load(LoadType load_type,
275 const blink::WebURL& url,
276 CORSMode cors_mode) {
277 DCHECK(main_thread_checker_.CalledOnValidThread());
279 media::ReportMetrics(load_type, GURL(url),
280 GURL(frame_->document().securityOrigin().toString()));
282 switch (load_type) {
283 case LoadTypeURL:
284 player_type_ = MEDIA_PLAYER_TYPE_URL;
285 break;
287 case LoadTypeMediaSource:
288 player_type_ = MEDIA_PLAYER_TYPE_MEDIA_SOURCE;
289 break;
291 case LoadTypeMediaStream:
292 CHECK(false) << "WebMediaPlayerAndroid doesn't support MediaStream on "
293 "this platform";
294 return;
297 url_ = url;
298 is_local_resource_ = IsLocalResource();
299 int demuxer_client_id = 0;
300 if (player_type_ != MEDIA_PLAYER_TYPE_URL) {
301 RendererDemuxerAndroid* demuxer =
302 RenderThreadImpl::current()->renderer_demuxer();
303 demuxer_client_id = demuxer->GetNextDemuxerClientID();
305 media_source_delegate_.reset(new MediaSourceDelegate(
306 demuxer, demuxer_client_id, media_task_runner_, media_log_));
308 if (player_type_ == MEDIA_PLAYER_TYPE_MEDIA_SOURCE) {
309 media_source_delegate_->InitializeMediaSource(
310 base::Bind(&WebMediaPlayerAndroid::OnMediaSourceOpened,
311 weak_factory_.GetWeakPtr()),
312 base::Bind(&WebMediaPlayerAndroid::OnEncryptedMediaInitData,
313 weak_factory_.GetWeakPtr()),
314 base::Bind(&WebMediaPlayerAndroid::SetDecryptorReadyCB,
315 weak_factory_.GetWeakPtr()),
316 base::Bind(&WebMediaPlayerAndroid::UpdateNetworkState,
317 weak_factory_.GetWeakPtr()),
318 base::Bind(&WebMediaPlayerAndroid::OnDurationChanged,
319 weak_factory_.GetWeakPtr()),
320 base::Bind(&WebMediaPlayerAndroid::OnWaitingForDecryptionKey,
321 weak_factory_.GetWeakPtr()));
322 InitializePlayer(url_, frame_->document().firstPartyForCookies(),
323 true, demuxer_client_id);
325 } else {
326 info_loader_.reset(
327 new MediaInfoLoader(
328 url,
329 cors_mode,
330 base::Bind(&WebMediaPlayerAndroid::DidLoadMediaInfo,
331 weak_factory_.GetWeakPtr())));
332 info_loader_->Start(frame_);
335 UpdateNetworkState(WebMediaPlayer::NetworkStateLoading);
336 UpdateReadyState(WebMediaPlayer::ReadyStateHaveNothing);
339 void WebMediaPlayerAndroid::DidLoadMediaInfo(
340 MediaInfoLoader::Status status,
341 const GURL& redirected_url,
342 const GURL& first_party_for_cookies,
343 bool allow_stored_credentials) {
344 DCHECK(main_thread_checker_.CalledOnValidThread());
345 DCHECK(!media_source_delegate_);
346 if (status == MediaInfoLoader::kFailed) {
347 info_loader_.reset();
348 UpdateNetworkState(WebMediaPlayer::NetworkStateNetworkError);
349 return;
351 redirected_url_ = redirected_url;
352 InitializePlayer(
353 redirected_url, first_party_for_cookies, allow_stored_credentials, 0);
355 UpdateNetworkState(WebMediaPlayer::NetworkStateIdle);
358 bool WebMediaPlayerAndroid::IsLocalResource() {
359 if (url_.SchemeIsFile() || url_.SchemeIsBlob())
360 return true;
362 std::string host = url_.host();
363 if (!host.compare("localhost") || !host.compare("127.0.0.1") ||
364 !host.compare("[::1]")) {
365 return true;
368 return false;
371 void WebMediaPlayerAndroid::play() {
372 DCHECK(main_thread_checker_.CalledOnValidThread());
374 // For HLS streams, some devices cannot detect the video size unless a surface
375 // texture is bind to it. See http://crbug.com/400145.
376 #if defined(VIDEO_HOLE)
377 if ((hasVideo() || IsHLSStream()) && needs_external_surface_ &&
378 !is_fullscreen_) {
379 DCHECK(!needs_establish_peer_);
380 player_manager_->RequestExternalSurface(player_id_, last_computed_rect_);
382 #endif // defined(VIDEO_HOLE)
384 TryCreateStreamTextureProxyIfNeeded();
385 // There is no need to establish the surface texture peer for fullscreen
386 // video.
387 if ((hasVideo() || IsHLSStream()) && needs_establish_peer_ &&
388 !is_fullscreen_) {
389 EstablishSurfaceTexturePeer();
392 if (paused())
393 player_manager_->Start(player_id_);
394 UpdatePlayingState(true);
395 UpdateNetworkState(WebMediaPlayer::NetworkStateLoading);
398 void WebMediaPlayerAndroid::pause() {
399 DCHECK(main_thread_checker_.CalledOnValidThread());
400 Pause(true);
403 void WebMediaPlayerAndroid::requestRemotePlayback() {
404 player_manager_->RequestRemotePlayback(player_id_);
407 void WebMediaPlayerAndroid::requestRemotePlaybackControl() {
408 player_manager_->RequestRemotePlaybackControl(player_id_);
411 void WebMediaPlayerAndroid::seek(double seconds) {
412 DCHECK(main_thread_checker_.CalledOnValidThread());
413 DVLOG(1) << __FUNCTION__ << "(" << seconds << ")";
415 base::TimeDelta new_seek_time = media::ConvertSecondsToTimestamp(seconds);
417 if (seeking_) {
418 if (new_seek_time == seek_time_) {
419 if (media_source_delegate_) {
420 if (!pending_seek_) {
421 // If using media source demuxer, only suppress redundant seeks if
422 // there is no pending seek. This enforces that any pending seek that
423 // results in a demuxer seek is preceded by matching
424 // CancelPendingSeek() and StartWaitingForSeek() calls.
425 return;
427 } else {
428 // Suppress all redundant seeks if unrestricted by media source
429 // demuxer API.
430 pending_seek_ = false;
431 return;
435 pending_seek_ = true;
436 pending_seek_time_ = new_seek_time;
438 if (media_source_delegate_)
439 media_source_delegate_->CancelPendingSeek(pending_seek_time_);
441 // Later, OnSeekComplete will trigger the pending seek.
442 return;
445 seeking_ = true;
446 seek_time_ = new_seek_time;
448 if (media_source_delegate_)
449 media_source_delegate_->StartWaitingForSeek(seek_time_);
451 // Kick off the asynchronous seek!
452 player_manager_->Seek(player_id_, seek_time_);
455 bool WebMediaPlayerAndroid::supportsSave() const {
456 return false;
459 void WebMediaPlayerAndroid::setRate(double rate) {
460 NOTIMPLEMENTED();
463 void WebMediaPlayerAndroid::setVolume(double volume) {
464 DCHECK(main_thread_checker_.CalledOnValidThread());
465 player_manager_->SetVolume(player_id_, volume);
468 void WebMediaPlayerAndroid::setSinkId(
469 const blink::WebString& device_id,
470 media::WebSetSinkIdCB* raw_web_callbacks) {
471 DCHECK(main_thread_checker_.CalledOnValidThread());
472 scoped_ptr<media::WebSetSinkIdCB> web_callbacks(raw_web_callbacks);
473 web_callbacks->onError(new blink::WebSetSinkIdError(
474 blink::WebSetSinkIdError::ErrorTypeNotSupported, "Not Supported"));
477 bool WebMediaPlayerAndroid::hasVideo() const {
478 DCHECK(main_thread_checker_.CalledOnValidThread());
479 // If we have obtained video size information before, use it.
480 if (has_size_info_)
481 return !natural_size_.isEmpty();
483 // TODO(qinmin): need a better method to determine whether the current media
484 // content contains video. Android does not provide any function to do
485 // this.
486 // We don't know whether the current media content has video unless
487 // the player is prepared. If the player is not prepared, we fall back
488 // to the mime-type. There may be no mime-type on a redirect URL.
489 // In that case, we conservatively assume it contains video so that
490 // enterfullscreen call will not fail.
491 if (!url_.has_path())
492 return false;
493 std::string mime;
494 if (!net::GetMimeTypeFromFile(base::FilePath(url_.path()), &mime))
495 return true;
496 return mime.find("audio/") == std::string::npos;
499 bool WebMediaPlayerAndroid::hasAudio() const {
500 DCHECK(main_thread_checker_.CalledOnValidThread());
501 if (!url_.has_path())
502 return false;
503 std::string mime;
504 if (!net::GetMimeTypeFromFile(base::FilePath(url_.path()), &mime))
505 return true;
507 if (mime.find("audio/") != std::string::npos ||
508 mime.find("video/") != std::string::npos ||
509 mime.find("application/ogg") != std::string::npos ||
510 mime.find("application/x-mpegurl") != std::string::npos) {
511 return true;
513 return false;
516 bool WebMediaPlayerAndroid::isRemote() const {
517 return is_remote_;
520 bool WebMediaPlayerAndroid::paused() const {
521 return !is_playing_;
524 bool WebMediaPlayerAndroid::seeking() const {
525 return seeking_;
528 double WebMediaPlayerAndroid::duration() const {
529 DCHECK(main_thread_checker_.CalledOnValidThread());
530 // HTML5 spec requires duration to be NaN if readyState is HAVE_NOTHING
531 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
532 return std::numeric_limits<double>::quiet_NaN();
534 if (duration_ == media::kInfiniteDuration())
535 return std::numeric_limits<double>::infinity();
537 return duration_.InSecondsF();
540 double WebMediaPlayerAndroid::timelineOffset() const {
541 DCHECK(main_thread_checker_.CalledOnValidThread());
542 base::Time timeline_offset;
543 if (media_source_delegate_)
544 timeline_offset = media_source_delegate_->GetTimelineOffset();
546 if (timeline_offset.is_null())
547 return std::numeric_limits<double>::quiet_NaN();
549 return timeline_offset.ToJsTime();
552 double WebMediaPlayerAndroid::currentTime() const {
553 DCHECK(main_thread_checker_.CalledOnValidThread());
554 // If the player is processing a seek, return the seek time.
555 // Blink may still query us if updatePlaybackState() occurs while seeking.
556 if (seeking()) {
557 return pending_seek_ ?
558 pending_seek_time_.InSecondsF() : seek_time_.InSecondsF();
561 return std::min(
562 (const_cast<media::TimeDeltaInterpolator*>(
563 &interpolator_))->GetInterpolatedTime(), duration_).InSecondsF();
566 WebSize WebMediaPlayerAndroid::naturalSize() const {
567 return natural_size_;
570 WebMediaPlayer::NetworkState WebMediaPlayerAndroid::networkState() const {
571 return network_state_;
574 WebMediaPlayer::ReadyState WebMediaPlayerAndroid::readyState() const {
575 return ready_state_;
578 blink::WebTimeRanges WebMediaPlayerAndroid::buffered() const {
579 if (media_source_delegate_)
580 return media_source_delegate_->Buffered();
581 return buffered_;
584 blink::WebTimeRanges WebMediaPlayerAndroid::seekable() const {
585 if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata)
586 return blink::WebTimeRanges();
588 // TODO(dalecurtis): Technically this allows seeking on media which return an
589 // infinite duration. While not expected, disabling this breaks semi-live
590 // players, http://crbug.com/427412.
591 const blink::WebTimeRange seekable_range(0.0, duration());
592 return blink::WebTimeRanges(&seekable_range, 1);
595 bool WebMediaPlayerAndroid::didLoadingProgress() {
596 bool ret = did_loading_progress_;
597 did_loading_progress_ = false;
598 return ret;
601 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas,
602 const blink::WebRect& rect,
603 unsigned char alpha,
604 SkXfermode::Mode mode) {
605 DCHECK(main_thread_checker_.CalledOnValidThread());
606 scoped_ptr<blink::WebGraphicsContext3DProvider> provider =
607 scoped_ptr<blink::WebGraphicsContext3DProvider>(blink::Platform::current(
608 )->createSharedOffscreenGraphicsContext3DProvider());
609 if (!provider)
610 return;
611 blink::WebGraphicsContext3D* context3D = provider->context3d();
612 if (!context3D)
613 return;
615 // Copy video texture into a RGBA texture based bitmap first as video texture
616 // on Android is GL_TEXTURE_EXTERNAL_OES which is not supported by Skia yet.
617 // The bitmap's size needs to be the same as the video and use naturalSize()
618 // here. Check if we could reuse existing texture based bitmap.
619 // Otherwise, release existing texture based bitmap and allocate
620 // a new one based on video size.
621 if (!IsSkBitmapProperlySizedTexture(&bitmap_, naturalSize())) {
622 if (!AllocateSkBitmapTexture(provider->grContext(), &bitmap_,
623 naturalSize())) {
624 return;
628 unsigned textureId = static_cast<unsigned>(
629 (bitmap_.getTexture())->getTextureHandle());
630 if (!copyVideoTextureToPlatformTexture(context3D, textureId,
631 GL_RGBA, GL_UNSIGNED_BYTE, true, false)) {
632 return;
635 // Draw the texture based bitmap onto the Canvas. If the canvas is
636 // hardware based, this will do a GPU-GPU texture copy.
637 // If the canvas is software based, the texture based bitmap will be
638 // readbacked to system memory then draw onto the canvas.
639 SkRect dest;
640 dest.set(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
641 SkPaint paint;
642 paint.setAlpha(alpha);
643 paint.setXfermodeMode(mode);
644 // It is not necessary to pass the dest into the drawBitmap call since all
645 // the context have been set up before calling paintCurrentFrameInContext.
646 canvas->drawBitmapRect(bitmap_, dest, &paint);
649 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture(
650 blink::WebGraphicsContext3D* web_graphics_context,
651 unsigned int texture,
652 unsigned int internal_format,
653 unsigned int type,
654 bool premultiply_alpha,
655 bool flip_y) {
656 DCHECK(main_thread_checker_.CalledOnValidThread());
657 // Don't allow clients to copy an encrypted video frame.
658 if (needs_external_surface_)
659 return false;
661 scoped_refptr<VideoFrame> video_frame;
663 base::AutoLock auto_lock(current_frame_lock_);
664 video_frame = current_frame_;
667 if (!video_frame.get() || !video_frame->HasTextures())
668 return false;
669 DCHECK_EQ(1u, media::VideoFrame::NumPlanes(video_frame->format()));
670 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(0);
671 DCHECK((!is_remote_ &&
672 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES) ||
673 (is_remote_ && mailbox_holder.texture_target == GL_TEXTURE_2D));
675 web_graphics_context->waitSyncPoint(mailbox_holder.sync_point);
677 // Ensure the target of texture is set before copyTextureCHROMIUM, otherwise
678 // an invalid texture target may be used for copy texture.
679 uint32 src_texture =
680 web_graphics_context->createAndConsumeTextureCHROMIUM(
681 mailbox_holder.texture_target, mailbox_holder.mailbox.name);
683 // Application itself needs to take care of setting the right flip_y
684 // value down to get the expected result.
685 // flip_y==true means to reverse the video orientation while
686 // flip_y==false means to keep the intrinsic orientation.
687 web_graphics_context->copyTextureCHROMIUM(
688 GL_TEXTURE_2D, src_texture, texture, internal_format, type,
689 flip_y, premultiply_alpha, false);
691 web_graphics_context->deleteTexture(src_texture);
692 web_graphics_context->flush();
694 SyncPointClientImpl client(web_graphics_context);
695 video_frame->UpdateReleaseSyncPoint(&client);
696 return true;
699 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const {
700 DCHECK(main_thread_checker_.CalledOnValidThread());
701 if (player_type_ != MEDIA_PLAYER_TYPE_URL)
702 return true;
704 if (!info_loader_ || !info_loader_->HasSingleOrigin())
705 return false;
707 // TODO(qinmin): The url might be redirected when android media player
708 // requests the stream. As a result, we cannot guarantee there is only
709 // a single origin. Only if the HTTP request was made without credentials,
710 // we will honor the return value from HasSingleSecurityOriginInternal()
711 // in pre-L android versions.
712 // Check http://crbug.com/334204.
713 if (!allow_stored_credentials_)
714 return true;
716 return base::android::BuildInfo::GetInstance()->sdk_int() >=
717 kSDKVersionToSupportSecurityOriginCheck;
720 bool WebMediaPlayerAndroid::didPassCORSAccessCheck() const {
721 DCHECK(main_thread_checker_.CalledOnValidThread());
722 if (info_loader_)
723 return info_loader_->DidPassCORSAccessCheck();
724 return false;
727 double WebMediaPlayerAndroid::mediaTimeForTimeValue(double timeValue) const {
728 return media::ConvertSecondsToTimestamp(timeValue).InSecondsF();
731 unsigned WebMediaPlayerAndroid::decodedFrameCount() const {
732 if (media_source_delegate_)
733 return media_source_delegate_->DecodedFrameCount();
734 NOTIMPLEMENTED();
735 return 0;
738 unsigned WebMediaPlayerAndroid::droppedFrameCount() const {
739 if (media_source_delegate_)
740 return media_source_delegate_->DroppedFrameCount();
741 NOTIMPLEMENTED();
742 return 0;
745 unsigned WebMediaPlayerAndroid::audioDecodedByteCount() const {
746 if (media_source_delegate_)
747 return media_source_delegate_->AudioDecodedByteCount();
748 NOTIMPLEMENTED();
749 return 0;
752 unsigned WebMediaPlayerAndroid::videoDecodedByteCount() const {
753 if (media_source_delegate_)
754 return media_source_delegate_->VideoDecodedByteCount();
755 NOTIMPLEMENTED();
756 return 0;
759 void WebMediaPlayerAndroid::OnMediaMetadataChanged(
760 base::TimeDelta duration, int width, int height, bool success) {
761 DCHECK(main_thread_checker_.CalledOnValidThread());
762 bool need_to_signal_duration_changed = false;
764 if (is_local_resource_)
765 UpdateNetworkState(WebMediaPlayer::NetworkStateLoaded);
767 // For HLS streams, the reported duration may be zero for infinite streams.
768 // See http://crbug.com/501213.
769 if (duration.is_zero() && IsHLSStream())
770 duration = media::kInfiniteDuration();
772 // Update duration, if necessary, prior to ready state updates that may
773 // cause duration() query.
774 if (!ignore_metadata_duration_change_ && duration_ != duration) {
775 duration_ = duration;
776 if (is_local_resource_)
777 buffered_[0].end = duration_.InSecondsF();
778 // Client readyState transition from HAVE_NOTHING to HAVE_METADATA
779 // already triggers a durationchanged event. If this is a different
780 // transition, remember to signal durationchanged.
781 // Do not ever signal durationchanged on metadata change in MSE case
782 // because OnDurationChanged() handles this.
783 if (ready_state_ > WebMediaPlayer::ReadyStateHaveNothing &&
784 player_type_ != MEDIA_PLAYER_TYPE_MEDIA_SOURCE) {
785 need_to_signal_duration_changed = true;
789 if (ready_state_ != WebMediaPlayer::ReadyStateHaveEnoughData) {
790 UpdateReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
791 UpdateReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
794 // TODO(wolenetz): Should we just abort early and set network state to an
795 // error if success == false? See http://crbug.com/248399
796 if (success)
797 OnVideoSizeChanged(width, height);
799 if (need_to_signal_duration_changed)
800 client_->durationChanged();
803 void WebMediaPlayerAndroid::OnPlaybackComplete() {
804 // When playback is about to finish, android media player often stops
805 // at a time which is smaller than the duration. This makes webkit never
806 // know that the playback has finished. To solve this, we set the
807 // current time to media duration when OnPlaybackComplete() get called.
808 interpolator_.SetBounds(duration_, duration_);
809 client_->timeChanged();
811 // If the loop attribute is set, timeChanged() will update the current time
812 // to 0. It will perform a seek to 0. Issue a command to the player to start
813 // playing after seek completes.
814 if (seeking_ && seek_time_ == base::TimeDelta())
815 player_manager_->Start(player_id_);
818 void WebMediaPlayerAndroid::OnBufferingUpdate(int percentage) {
819 buffered_[0].end = duration() * percentage / 100;
820 did_loading_progress_ = true;
823 void WebMediaPlayerAndroid::OnSeekRequest(const base::TimeDelta& time_to_seek) {
824 DCHECK(main_thread_checker_.CalledOnValidThread());
825 client_->requestSeek(time_to_seek.InSecondsF());
828 void WebMediaPlayerAndroid::OnSeekComplete(
829 const base::TimeDelta& current_time) {
830 DCHECK(main_thread_checker_.CalledOnValidThread());
831 seeking_ = false;
832 if (pending_seek_) {
833 pending_seek_ = false;
834 seek(pending_seek_time_.InSecondsF());
835 return;
837 interpolator_.SetBounds(current_time, current_time);
839 UpdateReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
841 client_->timeChanged();
844 void WebMediaPlayerAndroid::OnMediaError(int error_type) {
845 switch (error_type) {
846 case MediaPlayerAndroid::MEDIA_ERROR_FORMAT:
847 UpdateNetworkState(WebMediaPlayer::NetworkStateFormatError);
848 break;
849 case MediaPlayerAndroid::MEDIA_ERROR_DECODE:
850 UpdateNetworkState(WebMediaPlayer::NetworkStateDecodeError);
851 break;
852 case MediaPlayerAndroid::MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
853 UpdateNetworkState(WebMediaPlayer::NetworkStateFormatError);
854 break;
855 case MediaPlayerAndroid::MEDIA_ERROR_INVALID_CODE:
856 break;
858 client_->repaint();
861 void WebMediaPlayerAndroid::OnVideoSizeChanged(int width, int height) {
862 DCHECK(main_thread_checker_.CalledOnValidThread());
864 // For HLS streams, a bogus empty size may be reported at first, followed by
865 // the actual size only once playback begins. See http://crbug.com/509972.
866 if (!has_size_info_ && width == 0 && height == 0 && IsHLSStream())
867 return;
869 has_size_info_ = true;
870 if (natural_size_.width == width && natural_size_.height == height)
871 return;
873 #if defined(VIDEO_HOLE)
874 // Use H/W surface for encrypted video.
875 // TODO(qinmin): Change this so that only EME needs the H/W surface
876 if (force_use_overlay_embedded_video_ ||
877 (media_source_delegate_ && media_source_delegate_->IsVideoEncrypted() &&
878 player_manager_->ShouldUseVideoOverlayForEmbeddedEncryptedVideo())) {
879 needs_external_surface_ = true;
880 if (!paused() && !is_fullscreen_)
881 player_manager_->RequestExternalSurface(player_id_, last_computed_rect_);
882 } else if (!stream_texture_proxy_) {
883 // Do deferred stream texture creation finally.
884 SetNeedsEstablishPeer(true);
885 TryCreateStreamTextureProxyIfNeeded();
887 #endif // defined(VIDEO_HOLE)
888 natural_size_.width = width;
889 natural_size_.height = height;
891 // When play() gets called, |natural_size_| may still be empty and
892 // EstablishSurfaceTexturePeer() will not get called. As a result, the video
893 // may play without a surface texture. When we finally get the valid video
894 // size here, we should call EstablishSurfaceTexturePeer() if it has not been
895 // previously called.
896 if (!paused() && needs_establish_peer_)
897 EstablishSurfaceTexturePeer();
899 ReallocateVideoFrame();
901 // For hidden video element (with style "display:none"), ensure the texture
902 // size is set.
903 if (!is_remote_ && cached_stream_texture_size_ != natural_size_) {
904 stream_texture_factory_->SetStreamTextureSize(
905 stream_id_, gfx::Size(natural_size_.width, natural_size_.height));
906 cached_stream_texture_size_ = natural_size_;
909 // Lazily allocate compositing layer.
910 if (!video_weblayer_) {
911 video_weblayer_.reset(new cc_blink::WebLayerImpl(
912 cc::VideoLayer::Create(cc_blink::WebLayerImpl::LayerSettings(), this,
913 media::VIDEO_ROTATION_0)));
914 client_->setWebLayer(video_weblayer_.get());
917 // TODO(qinmin): This is a hack. We need the media element to stop showing the
918 // poster image by forcing it to call setDisplayMode(video). Should move the
919 // logic into HTMLMediaElement.cpp.
920 client_->timeChanged();
923 void WebMediaPlayerAndroid::OnTimeUpdate(base::TimeDelta current_timestamp,
924 base::TimeTicks current_time_ticks) {
925 DCHECK(main_thread_checker_.CalledOnValidThread());
926 // Compensate the current_timestamp with the IPC latency.
927 base::TimeDelta lower_bound =
928 base::TimeTicks::Now() - current_time_ticks + current_timestamp;
929 base::TimeDelta upper_bound = lower_bound;
930 // We should get another time update in about |kTimeUpdateInterval|
931 // milliseconds.
932 if (is_playing_) {
933 upper_bound += base::TimeDelta::FromMilliseconds(
934 media::kTimeUpdateInterval);
936 // if the lower_bound is smaller than the current time, just use the current
937 // time so that the timer is always progressing.
938 lower_bound =
939 std::min(lower_bound, base::TimeDelta::FromSecondsD(currentTime()));
940 interpolator_.SetBounds(lower_bound, upper_bound);
943 void WebMediaPlayerAndroid::OnConnectedToRemoteDevice(
944 const std::string& remote_playback_message) {
945 DCHECK(main_thread_checker_.CalledOnValidThread());
946 DCHECK(!media_source_delegate_);
947 DrawRemotePlaybackText(remote_playback_message);
948 is_remote_ = true;
949 SetNeedsEstablishPeer(false);
950 client_->connectedToRemoteDevice();
953 void WebMediaPlayerAndroid::OnDisconnectedFromRemoteDevice() {
954 DCHECK(main_thread_checker_.CalledOnValidThread());
955 DCHECK(!media_source_delegate_);
956 SetNeedsEstablishPeer(true);
957 if (!paused())
958 EstablishSurfaceTexturePeer();
959 is_remote_ = false;
960 ReallocateVideoFrame();
961 client_->disconnectedFromRemoteDevice();
964 void WebMediaPlayerAndroid::OnDidExitFullscreen() {
965 // |needs_external_surface_| is always false on non-TV devices.
966 if (!needs_external_surface_)
967 SetNeedsEstablishPeer(true);
968 // We had the fullscreen surface connected to Android MediaPlayer,
969 // so reconnect our surface texture for embedded playback.
970 if (!paused() && needs_establish_peer_) {
971 TryCreateStreamTextureProxyIfNeeded();
972 EstablishSurfaceTexturePeer();
975 #if defined(VIDEO_HOLE)
976 if (!paused() && needs_external_surface_)
977 player_manager_->RequestExternalSurface(player_id_, last_computed_rect_);
978 #endif // defined(VIDEO_HOLE)
979 is_fullscreen_ = false;
980 client_->repaint();
983 void WebMediaPlayerAndroid::OnMediaPlayerPlay() {
984 UpdatePlayingState(true);
985 client_->playbackStateChanged();
988 void WebMediaPlayerAndroid::OnMediaPlayerPause() {
989 UpdatePlayingState(false);
990 client_->playbackStateChanged();
993 void WebMediaPlayerAndroid::OnRemoteRouteAvailabilityChanged(
994 bool routes_available) {
995 client_->remoteRouteAvailabilityChanged(routes_available);
998 void WebMediaPlayerAndroid::OnDurationChanged(const base::TimeDelta& duration) {
999 DCHECK(main_thread_checker_.CalledOnValidThread());
1000 // Only MSE |player_type_| registers this callback.
1001 DCHECK_EQ(player_type_, MEDIA_PLAYER_TYPE_MEDIA_SOURCE);
1003 // Cache the new duration value and trust it over any subsequent duration
1004 // values received in OnMediaMetadataChanged().
1005 duration_ = duration;
1006 ignore_metadata_duration_change_ = true;
1008 // Notify MediaPlayerClient that duration has changed, if > HAVE_NOTHING.
1009 if (ready_state_ > WebMediaPlayer::ReadyStateHaveNothing)
1010 client_->durationChanged();
1013 void WebMediaPlayerAndroid::UpdateNetworkState(
1014 WebMediaPlayer::NetworkState state) {
1015 DCHECK(main_thread_checker_.CalledOnValidThread());
1016 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing &&
1017 (state == WebMediaPlayer::NetworkStateNetworkError ||
1018 state == WebMediaPlayer::NetworkStateDecodeError)) {
1019 // Any error that occurs before reaching ReadyStateHaveMetadata should
1020 // be considered a format error.
1021 network_state_ = WebMediaPlayer::NetworkStateFormatError;
1022 } else {
1023 network_state_ = state;
1025 client_->networkStateChanged();
1028 void WebMediaPlayerAndroid::UpdateReadyState(
1029 WebMediaPlayer::ReadyState state) {
1030 ready_state_ = state;
1031 client_->readyStateChanged();
1034 void WebMediaPlayerAndroid::OnPlayerReleased() {
1035 // |needs_external_surface_| is always false on non-TV devices.
1036 if (!needs_external_surface_)
1037 needs_establish_peer_ = true;
1039 if (is_playing_)
1040 OnMediaPlayerPause();
1042 #if defined(VIDEO_HOLE)
1043 last_computed_rect_ = gfx::RectF();
1044 #endif // defined(VIDEO_HOLE)
1047 void WebMediaPlayerAndroid::ReleaseMediaResources() {
1048 switch (network_state_) {
1049 // Pause the media player and inform WebKit if the player is in a good
1050 // shape.
1051 case WebMediaPlayer::NetworkStateIdle:
1052 case WebMediaPlayer::NetworkStateLoading:
1053 case WebMediaPlayer::NetworkStateLoaded:
1054 Pause(false);
1055 client_->playbackStateChanged();
1056 break;
1057 // If a WebMediaPlayer instance has entered into one of these states,
1058 // the internal network state in HTMLMediaElement could be set to empty.
1059 // And calling playbackStateChanged() could get this object deleted.
1060 case WebMediaPlayer::NetworkStateEmpty:
1061 case WebMediaPlayer::NetworkStateFormatError:
1062 case WebMediaPlayer::NetworkStateNetworkError:
1063 case WebMediaPlayer::NetworkStateDecodeError:
1064 break;
1066 player_manager_->ReleaseResources(player_id_);
1067 if (!needs_external_surface_)
1068 SetNeedsEstablishPeer(true);
1071 void WebMediaPlayerAndroid::OnDestruct() {
1072 NOTREACHED() << "WebMediaPlayer should be destroyed before any "
1073 "RenderFrameObserver::OnDestruct() gets called when "
1074 "the RenderFrame goes away.";
1077 void WebMediaPlayerAndroid::InitializePlayer(
1078 const GURL& url,
1079 const GURL& first_party_for_cookies,
1080 bool allow_stored_credentials,
1081 int demuxer_client_id) {
1082 ReportHLSMetrics();
1084 allow_stored_credentials_ = allow_stored_credentials;
1085 player_manager_->Initialize(
1086 player_type_, player_id_, url, first_party_for_cookies, demuxer_client_id,
1087 frame_->document().url(), allow_stored_credentials);
1088 is_player_initialized_ = true;
1090 if (is_fullscreen_)
1091 player_manager_->EnterFullscreen(player_id_);
1093 if (cdm_context_)
1094 SetCdmInternal(base::Bind(&media::IgnoreCdmAttached));
1097 void WebMediaPlayerAndroid::Pause(bool is_media_related_action) {
1098 player_manager_->Pause(player_id_, is_media_related_action);
1099 UpdatePlayingState(false);
1102 void WebMediaPlayerAndroid::DrawRemotePlaybackText(
1103 const std::string& remote_playback_message) {
1104 DCHECK(main_thread_checker_.CalledOnValidThread());
1105 if (!video_weblayer_)
1106 return;
1108 // TODO(johnme): Should redraw this frame if the layer bounds change; but
1109 // there seems no easy way to listen for the layer resizing (as opposed to
1110 // OnVideoSizeChanged, which is when the frame sizes of the video file
1111 // change). Perhaps have to poll (on main thread of course)?
1112 gfx::Size video_size_css_px = video_weblayer_->bounds();
1113 float device_scale_factor = frame_->view()->deviceScaleFactor();
1114 // canvas_size will be the size in device pixels when pageScaleFactor == 1
1115 gfx::Size canvas_size(
1116 static_cast<int>(video_size_css_px.width() * device_scale_factor),
1117 static_cast<int>(video_size_css_px.height() * device_scale_factor));
1119 SkBitmap bitmap;
1120 bitmap.allocN32Pixels(canvas_size.width(), canvas_size.height());
1122 // Create the canvas and draw the "Casting to <Chromecast>" text on it.
1123 SkCanvas canvas(bitmap);
1124 canvas.drawColor(SK_ColorBLACK);
1126 const SkScalar kTextSize(40);
1127 const SkScalar kMinPadding(40);
1129 SkPaint paint;
1130 paint.setAntiAlias(true);
1131 paint.setFilterQuality(kHigh_SkFilterQuality);
1132 paint.setColor(SK_ColorWHITE);
1133 paint.setTypeface(SkTypeface::CreateFromName("sans", SkTypeface::kBold));
1134 paint.setTextSize(kTextSize);
1136 // Calculate the vertical margin from the top
1137 SkPaint::FontMetrics font_metrics;
1138 paint.getFontMetrics(&font_metrics);
1139 SkScalar sk_vertical_margin = kMinPadding - font_metrics.fAscent;
1141 // Measure the width of the entire text to display
1142 size_t display_text_width = paint.measureText(
1143 remote_playback_message.c_str(), remote_playback_message.size());
1144 std::string display_text(remote_playback_message);
1146 if (display_text_width + (kMinPadding * 2) > canvas_size.width()) {
1147 // The text is too long to fit in one line, truncate it and append ellipsis
1148 // to the end.
1150 // First, figure out how much of the canvas the '...' will take up.
1151 const std::string kTruncationEllipsis("\xE2\x80\xA6");
1152 SkScalar sk_ellipse_width = paint.measureText(
1153 kTruncationEllipsis.c_str(), kTruncationEllipsis.size());
1155 // Then calculate how much of the text can be drawn with the '...' appended
1156 // to the end of the string.
1157 SkScalar sk_max_original_text_width(
1158 canvas_size.width() - (kMinPadding * 2) - sk_ellipse_width);
1159 size_t sk_max_original_text_length = paint.breakText(
1160 remote_playback_message.c_str(),
1161 remote_playback_message.size(),
1162 sk_max_original_text_width);
1164 // Remove the part of the string that doesn't fit and append '...'.
1165 display_text.erase(sk_max_original_text_length,
1166 remote_playback_message.size() - sk_max_original_text_length);
1167 display_text.append(kTruncationEllipsis);
1168 display_text_width = paint.measureText(
1169 display_text.c_str(), display_text.size());
1172 // Center the text horizontally.
1173 SkScalar sk_horizontal_margin =
1174 (canvas_size.width() - display_text_width) / 2.0;
1175 canvas.drawText(display_text.c_str(),
1176 display_text.size(),
1177 sk_horizontal_margin,
1178 sk_vertical_margin,
1179 paint);
1181 GLES2Interface* gl = stream_texture_factory_->ContextGL();
1182 GLuint remote_playback_texture_id = 0;
1183 gl->GenTextures(1, &remote_playback_texture_id);
1184 GLuint texture_target = GL_TEXTURE_2D;
1185 gl->BindTexture(texture_target, remote_playback_texture_id);
1186 gl->TexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1187 gl->TexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1188 gl->TexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1189 gl->TexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1192 SkAutoLockPixels lock(bitmap);
1193 gl->TexImage2D(texture_target,
1194 0 /* level */,
1195 GL_RGBA /* internalformat */,
1196 bitmap.width(),
1197 bitmap.height(),
1198 0 /* border */,
1199 GL_RGBA /* format */,
1200 GL_UNSIGNED_BYTE /* type */,
1201 bitmap.getPixels());
1204 gpu::Mailbox texture_mailbox;
1205 gl->GenMailboxCHROMIUM(texture_mailbox.name);
1206 gl->ProduceTextureCHROMIUM(texture_target, texture_mailbox.name);
1207 gl->Flush();
1208 GLuint texture_mailbox_sync_point = gl->InsertSyncPointCHROMIUM();
1210 scoped_refptr<VideoFrame> new_frame = VideoFrame::WrapNativeTexture(
1211 media::PIXEL_FORMAT_ARGB,
1212 gpu::MailboxHolder(texture_mailbox, texture_target,
1213 texture_mailbox_sync_point),
1214 media::BindToCurrentLoop(base::Bind(&OnReleaseTexture,
1215 stream_texture_factory_,
1216 remote_playback_texture_id)),
1217 canvas_size /* coded_size */, gfx::Rect(canvas_size) /* visible_rect */,
1218 canvas_size /* natural_size */, base::TimeDelta() /* timestamp */);
1219 SetCurrentFrameInternal(new_frame);
1222 void WebMediaPlayerAndroid::ReallocateVideoFrame() {
1223 DCHECK(main_thread_checker_.CalledOnValidThread());
1224 if (needs_external_surface_) {
1225 // VideoFrame::CreateHoleFrame is only defined under VIDEO_HOLE.
1226 #if defined(VIDEO_HOLE)
1227 if (!natural_size_.isEmpty()) {
1228 // Now we finally know that "stream texture" and "video frame" won't
1229 // be needed. EME uses "external surface" and "video hole" instead.
1230 RemoveSurfaceTextureAndProxy();
1231 scoped_refptr<VideoFrame> new_frame =
1232 VideoFrame::CreateHoleFrame(natural_size_);
1233 SetCurrentFrameInternal(new_frame);
1234 // Force the client to grab the hole frame.
1235 client_->repaint();
1237 #else
1238 NOTIMPLEMENTED() << "Hole punching not supported without VIDEO_HOLE flag";
1239 #endif // defined(VIDEO_HOLE)
1240 } else if (!is_remote_ && texture_id_) {
1241 GLES2Interface* gl = stream_texture_factory_->ContextGL();
1242 GLuint texture_target = kGLTextureExternalOES;
1243 GLuint texture_id_ref = gl->CreateAndConsumeTextureCHROMIUM(
1244 texture_target, texture_mailbox_.name);
1245 gl->Flush();
1246 GLuint texture_mailbox_sync_point = gl->InsertSyncPointCHROMIUM();
1248 scoped_refptr<VideoFrame> new_frame = VideoFrame::WrapNativeTexture(
1249 media::PIXEL_FORMAT_ARGB,
1250 gpu::MailboxHolder(texture_mailbox_, texture_target,
1251 texture_mailbox_sync_point),
1252 media::BindToCurrentLoop(base::Bind(
1253 &OnReleaseTexture, stream_texture_factory_, texture_id_ref)),
1254 natural_size_, gfx::Rect(natural_size_), natural_size_,
1255 base::TimeDelta());
1256 SetCurrentFrameInternal(new_frame);
1260 void WebMediaPlayerAndroid::SetVideoFrameProviderClient(
1261 cc::VideoFrameProvider::Client* client) {
1262 // This is called from both the main renderer thread and the compositor
1263 // thread (when the main thread is blocked).
1265 // Set the callback target when a frame is produced. Need to do this before
1266 // StopUsingProvider to ensure we really stop using the client.
1267 if (stream_texture_proxy_)
1268 stream_texture_proxy_->BindToLoop(stream_id_, client, compositor_loop_);
1270 if (video_frame_provider_client_ && video_frame_provider_client_ != client)
1271 video_frame_provider_client_->StopUsingProvider();
1272 video_frame_provider_client_ = client;
1275 void WebMediaPlayerAndroid::SetCurrentFrameInternal(
1276 scoped_refptr<media::VideoFrame>& video_frame) {
1277 DCHECK(main_thread_checker_.CalledOnValidThread());
1278 base::AutoLock auto_lock(current_frame_lock_);
1279 current_frame_ = video_frame;
1282 bool WebMediaPlayerAndroid::UpdateCurrentFrame(base::TimeTicks deadline_min,
1283 base::TimeTicks deadline_max) {
1284 NOTIMPLEMENTED();
1285 return false;
1288 bool WebMediaPlayerAndroid::HasCurrentFrame() {
1289 base::AutoLock auto_lock(current_frame_lock_);
1290 return current_frame_;
1293 scoped_refptr<media::VideoFrame> WebMediaPlayerAndroid::GetCurrentFrame() {
1294 scoped_refptr<VideoFrame> video_frame;
1296 base::AutoLock auto_lock(current_frame_lock_);
1297 video_frame = current_frame_;
1300 return video_frame;
1303 void WebMediaPlayerAndroid::PutCurrentFrame() {
1306 void WebMediaPlayerAndroid::ResetStreamTextureProxy() {
1307 DCHECK(main_thread_checker_.CalledOnValidThread());
1309 RemoveSurfaceTextureAndProxy();
1311 TryCreateStreamTextureProxyIfNeeded();
1312 if (needs_establish_peer_ && is_playing_)
1313 EstablishSurfaceTexturePeer();
1316 void WebMediaPlayerAndroid::RemoveSurfaceTextureAndProxy() {
1317 DCHECK(main_thread_checker_.CalledOnValidThread());
1319 if (stream_id_) {
1320 GLES2Interface* gl = stream_texture_factory_->ContextGL();
1321 gl->DeleteTextures(1, &texture_id_);
1322 // Flush to ensure that the stream texture gets deleted in a timely fashion.
1323 gl->ShallowFlushCHROMIUM();
1324 texture_id_ = 0;
1325 texture_mailbox_ = gpu::Mailbox();
1326 stream_id_ = 0;
1328 stream_texture_proxy_.reset();
1329 needs_establish_peer_ = !needs_external_surface_ && !is_remote_ &&
1330 !is_fullscreen_ &&
1331 (hasVideo() || IsHLSStream());
1334 void WebMediaPlayerAndroid::TryCreateStreamTextureProxyIfNeeded() {
1335 DCHECK(main_thread_checker_.CalledOnValidThread());
1336 // Already created.
1337 if (stream_texture_proxy_)
1338 return;
1340 // No factory to create proxy.
1341 if (!stream_texture_factory_.get())
1342 return;
1344 // Not needed for hole punching.
1345 if (!needs_establish_peer_)
1346 return;
1348 stream_texture_proxy_.reset(stream_texture_factory_->CreateProxy());
1349 if (stream_texture_proxy_) {
1350 DoCreateStreamTexture();
1351 ReallocateVideoFrame();
1352 if (video_frame_provider_client_) {
1353 stream_texture_proxy_->BindToLoop(
1354 stream_id_, video_frame_provider_client_, compositor_loop_);
1359 void WebMediaPlayerAndroid::EstablishSurfaceTexturePeer() {
1360 DCHECK(main_thread_checker_.CalledOnValidThread());
1361 if (!stream_texture_proxy_)
1362 return;
1364 if (stream_texture_factory_.get() && stream_id_)
1365 stream_texture_factory_->EstablishPeer(stream_id_, player_id_);
1367 // Set the deferred size because the size was changed in remote mode.
1368 if (!is_remote_ && cached_stream_texture_size_ != natural_size_) {
1369 stream_texture_factory_->SetStreamTextureSize(
1370 stream_id_, gfx::Size(natural_size_.width, natural_size_.height));
1371 cached_stream_texture_size_ = natural_size_;
1374 needs_establish_peer_ = false;
1377 void WebMediaPlayerAndroid::DoCreateStreamTexture() {
1378 DCHECK(main_thread_checker_.CalledOnValidThread());
1379 DCHECK(!stream_id_);
1380 DCHECK(!texture_id_);
1381 stream_id_ = stream_texture_factory_->CreateStreamTexture(
1382 kGLTextureExternalOES, &texture_id_, &texture_mailbox_);
1385 void WebMediaPlayerAndroid::SetNeedsEstablishPeer(bool needs_establish_peer) {
1386 needs_establish_peer_ = needs_establish_peer;
1389 void WebMediaPlayerAndroid::setPoster(const blink::WebURL& poster) {
1390 player_manager_->SetPoster(player_id_, poster);
1393 void WebMediaPlayerAndroid::UpdatePlayingState(bool is_playing) {
1394 if (is_playing == is_playing_)
1395 return;
1397 is_playing_ = is_playing;
1399 if (is_playing)
1400 interpolator_.StartInterpolating();
1401 else
1402 interpolator_.StopInterpolating();
1404 if (delegate_) {
1405 if (is_playing)
1406 delegate_->DidPlay(this);
1407 else
1408 delegate_->DidPause(this);
1412 #if defined(VIDEO_HOLE)
1413 bool WebMediaPlayerAndroid::UpdateBoundaryRectangle() {
1414 if (!video_weblayer_)
1415 return false;
1417 // Compute the geometry of video frame layer.
1418 cc::Layer* layer = video_weblayer_->layer();
1419 gfx::RectF rect(layer->bounds());
1420 while (layer) {
1421 rect.Offset(layer->position().OffsetFromOrigin());
1422 layer = layer->parent();
1425 // Return false when the geometry hasn't been changed from the last time.
1426 if (last_computed_rect_ == rect)
1427 return false;
1429 // Store the changed geometry information when it is actually changed.
1430 last_computed_rect_ = rect;
1431 return true;
1434 const gfx::RectF WebMediaPlayerAndroid::GetBoundaryRectangle() {
1435 return last_computed_rect_;
1437 #endif
1439 // The following EME related code is copied from WebMediaPlayerImpl.
1440 // TODO(xhwang): Remove duplicate code between WebMediaPlayerAndroid and
1441 // WebMediaPlayerImpl.
1443 // Convert a WebString to ASCII, falling back on an empty string in the case
1444 // of a non-ASCII string.
1445 static std::string ToASCIIOrEmpty(const blink::WebString& string) {
1446 return base::IsStringASCII(string)
1447 ? base::UTF16ToASCII(base::StringPiece16(string))
1448 : std::string();
1451 // Helper functions to report media EME related stats to UMA. They follow the
1452 // convention of more commonly used macros UMA_HISTOGRAM_ENUMERATION and
1453 // UMA_HISTOGRAM_COUNTS. The reason that we cannot use those macros directly is
1454 // that UMA_* macros require the names to be constant throughout the process'
1455 // lifetime.
1457 static void EmeUMAHistogramEnumeration(const std::string& key_system,
1458 const std::string& method,
1459 int sample,
1460 int boundary_value) {
1461 base::LinearHistogram::FactoryGet(
1462 kMediaEme + media::GetKeySystemNameForUMA(key_system) + "." + method,
1463 1, boundary_value, boundary_value + 1,
1464 base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
1467 static void EmeUMAHistogramCounts(const std::string& key_system,
1468 const std::string& method,
1469 int sample) {
1470 // Use the same parameters as UMA_HISTOGRAM_COUNTS.
1471 base::Histogram::FactoryGet(
1472 kMediaEme + media::GetKeySystemNameForUMA(key_system) + "." + method,
1473 1, 1000000, 50, base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
1476 // Helper enum for reporting generateKeyRequest/addKey histograms.
1477 enum MediaKeyException {
1478 kUnknownResultId,
1479 kSuccess,
1480 kKeySystemNotSupported,
1481 kInvalidPlayerState,
1482 kMaxMediaKeyException
1485 static MediaKeyException MediaKeyExceptionForUMA(
1486 WebMediaPlayer::MediaKeyException e) {
1487 switch (e) {
1488 case WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported:
1489 return kKeySystemNotSupported;
1490 case WebMediaPlayer::MediaKeyExceptionInvalidPlayerState:
1491 return kInvalidPlayerState;
1492 case WebMediaPlayer::MediaKeyExceptionNoError:
1493 return kSuccess;
1494 default:
1495 return kUnknownResultId;
1499 // Helper for converting |key_system| name and exception |e| to a pair of enum
1500 // values from above, for reporting to UMA.
1501 static void ReportMediaKeyExceptionToUMA(const std::string& method,
1502 const std::string& key_system,
1503 WebMediaPlayer::MediaKeyException e) {
1504 MediaKeyException result_id = MediaKeyExceptionForUMA(e);
1505 DCHECK_NE(result_id, kUnknownResultId) << e;
1506 EmeUMAHistogramEnumeration(
1507 key_system, method, result_id, kMaxMediaKeyException);
1510 bool WebMediaPlayerAndroid::IsKeySystemSupported(
1511 const std::string& key_system) {
1512 // On Android, EME only works with MSE.
1513 return player_type_ == MEDIA_PLAYER_TYPE_MEDIA_SOURCE &&
1514 media::PrefixedIsSupportedConcreteKeySystem(key_system);
1517 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::generateKeyRequest(
1518 const WebString& key_system,
1519 const unsigned char* init_data,
1520 unsigned init_data_length) {
1521 DVLOG(1) << "generateKeyRequest: " << base::string16(key_system) << ": "
1522 << std::string(reinterpret_cast<const char*>(init_data),
1523 static_cast<size_t>(init_data_length));
1525 std::string ascii_key_system =
1526 media::GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
1528 WebMediaPlayer::MediaKeyException e =
1529 GenerateKeyRequestInternal(ascii_key_system, init_data, init_data_length);
1530 ReportMediaKeyExceptionToUMA("generateKeyRequest", ascii_key_system, e);
1531 return e;
1534 // Guess the type of |init_data|. This is only used to handle some corner cases
1535 // so we keep it as simple as possible without breaking major use cases.
1536 static media::EmeInitDataType GuessInitDataType(const unsigned char* init_data,
1537 unsigned init_data_length) {
1538 // Most WebM files use KeyId of 16 bytes. CENC init data is always >16 bytes.
1539 if (init_data_length == 16)
1540 return media::EmeInitDataType::WEBM;
1542 return media::EmeInitDataType::CENC;
1545 // TODO(xhwang): Report an error when there is encrypted stream but EME is
1546 // not enabled. Currently the player just doesn't start and waits for
1547 // ever.
1548 WebMediaPlayer::MediaKeyException
1549 WebMediaPlayerAndroid::GenerateKeyRequestInternal(
1550 const std::string& key_system,
1551 const unsigned char* init_data,
1552 unsigned init_data_length) {
1553 if (!IsKeySystemSupported(key_system))
1554 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
1556 if (!proxy_decryptor_) {
1557 DCHECK(current_key_system_.empty());
1558 proxy_decryptor_.reset(new media::ProxyDecryptor(
1559 media_permission_,
1560 player_manager_->ShouldUseVideoOverlayForEmbeddedEncryptedVideo(),
1561 base::Bind(&WebMediaPlayerAndroid::OnKeyAdded,
1562 weak_factory_.GetWeakPtr()),
1563 base::Bind(&WebMediaPlayerAndroid::OnKeyError,
1564 weak_factory_.GetWeakPtr()),
1565 base::Bind(&WebMediaPlayerAndroid::OnKeyMessage,
1566 weak_factory_.GetWeakPtr())));
1568 GURL security_origin(frame_->document().securityOrigin().toString());
1569 proxy_decryptor_->CreateCdm(
1570 cdm_factory_, key_system, security_origin,
1571 base::Bind(&WebMediaPlayerAndroid::OnCdmContextReady,
1572 weak_factory_.GetWeakPtr()));
1573 current_key_system_ = key_system;
1576 // We do not support run-time switching between key systems for now.
1577 DCHECK(!current_key_system_.empty());
1578 if (key_system != current_key_system_)
1579 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
1581 media::EmeInitDataType init_data_type = init_data_type_;
1582 if (init_data_type == media::EmeInitDataType::UNKNOWN)
1583 init_data_type = GuessInitDataType(init_data, init_data_length);
1585 proxy_decryptor_->GenerateKeyRequest(init_data_type, init_data,
1586 init_data_length);
1588 return WebMediaPlayer::MediaKeyExceptionNoError;
1591 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::addKey(
1592 const WebString& key_system,
1593 const unsigned char* key,
1594 unsigned key_length,
1595 const unsigned char* init_data,
1596 unsigned init_data_length,
1597 const WebString& session_id) {
1598 DVLOG(1) << "addKey: " << base::string16(key_system) << ": "
1599 << std::string(reinterpret_cast<const char*>(key),
1600 static_cast<size_t>(key_length)) << ", "
1601 << std::string(reinterpret_cast<const char*>(init_data),
1602 static_cast<size_t>(init_data_length)) << " ["
1603 << base::string16(session_id) << "]";
1605 std::string ascii_key_system =
1606 media::GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
1607 std::string ascii_session_id = ToASCIIOrEmpty(session_id);
1609 WebMediaPlayer::MediaKeyException e = AddKeyInternal(ascii_key_system,
1610 key,
1611 key_length,
1612 init_data,
1613 init_data_length,
1614 ascii_session_id);
1615 ReportMediaKeyExceptionToUMA("addKey", ascii_key_system, e);
1616 return e;
1619 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::AddKeyInternal(
1620 const std::string& key_system,
1621 const unsigned char* key,
1622 unsigned key_length,
1623 const unsigned char* init_data,
1624 unsigned init_data_length,
1625 const std::string& session_id) {
1626 DCHECK(key);
1627 DCHECK_GT(key_length, 0u);
1629 if (!IsKeySystemSupported(key_system))
1630 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
1632 if (current_key_system_.empty() || key_system != current_key_system_)
1633 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
1635 proxy_decryptor_->AddKey(
1636 key, key_length, init_data, init_data_length, session_id);
1637 return WebMediaPlayer::MediaKeyExceptionNoError;
1640 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::cancelKeyRequest(
1641 const WebString& key_system,
1642 const WebString& session_id) {
1643 DVLOG(1) << "cancelKeyRequest: " << base::string16(key_system) << ": "
1644 << " [" << base::string16(session_id) << "]";
1646 std::string ascii_key_system =
1647 media::GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
1648 std::string ascii_session_id = ToASCIIOrEmpty(session_id);
1650 WebMediaPlayer::MediaKeyException e =
1651 CancelKeyRequestInternal(ascii_key_system, ascii_session_id);
1652 ReportMediaKeyExceptionToUMA("cancelKeyRequest", ascii_key_system, e);
1653 return e;
1656 WebMediaPlayer::MediaKeyException
1657 WebMediaPlayerAndroid::CancelKeyRequestInternal(const std::string& key_system,
1658 const std::string& session_id) {
1659 if (!IsKeySystemSupported(key_system))
1660 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
1662 if (current_key_system_.empty() || key_system != current_key_system_)
1663 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
1665 proxy_decryptor_->CancelKeyRequest(session_id);
1666 return WebMediaPlayer::MediaKeyExceptionNoError;
1669 void WebMediaPlayerAndroid::setContentDecryptionModule(
1670 blink::WebContentDecryptionModule* cdm,
1671 blink::WebContentDecryptionModuleResult result) {
1672 DCHECK(main_thread_checker_.CalledOnValidThread());
1674 // Once the CDM is set it can't be cleared as there may be frames being
1675 // decrypted on other threads. So fail this request.
1676 // http://crbug.com/462365#c7.
1677 if (!cdm) {
1678 result.completeWithError(
1679 blink::WebContentDecryptionModuleExceptionInvalidStateError, 0,
1680 "The existing MediaKeys object cannot be removed at this time.");
1681 return;
1684 cdm_context_ = media::ToWebContentDecryptionModuleImpl(cdm)->GetCdmContext();
1686 if (is_player_initialized_) {
1687 SetCdmInternal(media::BindToCurrentLoop(
1688 base::Bind(&WebMediaPlayerAndroid::ContentDecryptionModuleAttached,
1689 weak_factory_.GetWeakPtr(), result)));
1690 } else {
1691 // No pipeline/decoder connected, so resolve the promise. When something
1692 // is connected, setting the CDM will happen in SetDecryptorReadyCB().
1693 ContentDecryptionModuleAttached(result, true);
1697 void WebMediaPlayerAndroid::ContentDecryptionModuleAttached(
1698 blink::WebContentDecryptionModuleResult result,
1699 bool success) {
1700 if (success) {
1701 result.complete();
1702 return;
1705 result.completeWithError(
1706 blink::WebContentDecryptionModuleExceptionNotSupportedError,
1708 "Unable to set MediaKeys object");
1711 void WebMediaPlayerAndroid::OnKeyAdded(const std::string& session_id) {
1712 EmeUMAHistogramCounts(current_key_system_, "KeyAdded", 1);
1714 encrypted_client_->keyAdded(
1715 WebString::fromUTF8(media::GetPrefixedKeySystemName(current_key_system_)),
1716 WebString::fromUTF8(session_id));
1719 void WebMediaPlayerAndroid::OnKeyError(const std::string& session_id,
1720 media::MediaKeys::KeyError error_code,
1721 uint32 system_code) {
1722 EmeUMAHistogramEnumeration(current_key_system_, "KeyError",
1723 error_code, media::MediaKeys::kMaxKeyError);
1725 unsigned short short_system_code = 0;
1726 if (system_code > std::numeric_limits<unsigned short>::max()) {
1727 LOG(WARNING) << "system_code exceeds unsigned short limit.";
1728 short_system_code = std::numeric_limits<unsigned short>::max();
1729 } else {
1730 short_system_code = static_cast<unsigned short>(system_code);
1733 encrypted_client_->keyError(
1734 WebString::fromUTF8(media::GetPrefixedKeySystemName(current_key_system_)),
1735 WebString::fromUTF8(session_id),
1736 static_cast<blink::WebMediaPlayerEncryptedMediaClient::MediaKeyErrorCode>(
1737 error_code),
1738 short_system_code);
1741 void WebMediaPlayerAndroid::OnKeyMessage(const std::string& session_id,
1742 const std::vector<uint8>& message,
1743 const GURL& destination_url) {
1744 DCHECK(destination_url.is_empty() || destination_url.is_valid());
1746 encrypted_client_->keyMessage(
1747 WebString::fromUTF8(media::GetPrefixedKeySystemName(current_key_system_)),
1748 WebString::fromUTF8(session_id), message.empty() ? NULL : &message[0],
1749 message.size(), destination_url);
1752 void WebMediaPlayerAndroid::OnMediaSourceOpened(
1753 blink::WebMediaSource* web_media_source) {
1754 client_->mediaSourceOpened(web_media_source);
1757 void WebMediaPlayerAndroid::OnEncryptedMediaInitData(
1758 media::EmeInitDataType init_data_type,
1759 const std::vector<uint8>& init_data) {
1760 DCHECK(main_thread_checker_.CalledOnValidThread());
1762 // Do not fire NeedKey event if encrypted media is not enabled.
1763 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
1764 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
1765 return;
1768 UMA_HISTOGRAM_COUNTS(kMediaEme + std::string("NeedKey"), 1);
1770 DCHECK(init_data_type != media::EmeInitDataType::UNKNOWN);
1771 DLOG_IF(WARNING, init_data_type_ != media::EmeInitDataType::UNKNOWN &&
1772 init_data_type != init_data_type_)
1773 << "Mixed init data type not supported. The new type is ignored.";
1774 if (init_data_type_ == media::EmeInitDataType::UNKNOWN)
1775 init_data_type_ = init_data_type;
1777 encrypted_client_->encrypted(ConvertToWebInitDataType(init_data_type),
1778 vector_as_array(&init_data), init_data.size());
1781 void WebMediaPlayerAndroid::OnWaitingForDecryptionKey() {
1782 encrypted_client_->didBlockPlaybackWaitingForKey();
1784 // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called
1785 // when a key has been successfully added (e.g. OnSessionKeysChange() with
1786 // |has_additional_usable_key| = true). http://crbug.com/461903
1787 encrypted_client_->didResumePlaybackBlockedForKey();
1790 void WebMediaPlayerAndroid::OnCdmContextReady(media::CdmContext* cdm_context) {
1791 DCHECK(!cdm_context_);
1793 if (!cdm_context) {
1794 LOG(ERROR) << "CdmContext not available (e.g. CDM creation failed).";
1795 return;
1798 cdm_context_ = cdm_context;
1800 if (is_player_initialized_)
1801 SetCdmInternal(base::Bind(&media::IgnoreCdmAttached));
1804 void WebMediaPlayerAndroid::SetCdmInternal(
1805 const media::CdmAttachedCB& cdm_attached_cb) {
1806 DCHECK(cdm_context_ && is_player_initialized_);
1807 DCHECK(cdm_context_->GetDecryptor() ||
1808 cdm_context_->GetCdmId() != media::CdmContext::kInvalidCdmId)
1809 << "CDM should support either a Decryptor or a CDM ID.";
1811 media::Decryptor* decryptor = cdm_context_->GetDecryptor();
1813 // Note:
1814 // - If |decryptor| is non-null, only handles |decryptor_ready_cb_| and
1815 // ignores the CDM ID.
1816 // - If |decryptor| is null (in which case the CDM ID should be valid),
1817 // returns any pending |decryptor_ready_cb_| with null, so that
1818 // MediaSourceDelegate will fall back to use a browser side (IPC-based) CDM,
1819 // then calls SetCdm() through the |player_manager_|.
1821 if (decryptor) {
1822 if (!decryptor_ready_cb_.is_null()) {
1823 base::ResetAndReturn(&decryptor_ready_cb_)
1824 .Run(decryptor, cdm_attached_cb);
1825 } else {
1826 cdm_attached_cb.Run(true);
1828 return;
1831 // |decryptor| is null.
1832 if (!decryptor_ready_cb_.is_null()) {
1833 base::ResetAndReturn(&decryptor_ready_cb_)
1834 .Run(nullptr, base::Bind(&media::IgnoreCdmAttached));
1837 DCHECK(cdm_context_->GetCdmId() != media::CdmContext::kInvalidCdmId);
1838 player_manager_->SetCdm(player_id_, cdm_context_->GetCdmId());
1839 cdm_attached_cb.Run(true);
1842 void WebMediaPlayerAndroid::SetDecryptorReadyCB(
1843 const media::DecryptorReadyCB& decryptor_ready_cb) {
1844 DCHECK(main_thread_checker_.CalledOnValidThread());
1845 DCHECK(is_player_initialized_);
1847 // Cancels the previous decryptor request.
1848 if (decryptor_ready_cb.is_null()) {
1849 if (!decryptor_ready_cb_.is_null()) {
1850 base::ResetAndReturn(&decryptor_ready_cb_)
1851 .Run(NULL, base::Bind(&media::IgnoreCdmAttached));
1853 return;
1856 // TODO(xhwang): Support multiple decryptor notification request (e.g. from
1857 // video and audio). The current implementation is okay for the current
1858 // media pipeline since we initialize audio and video decoders in sequence.
1859 // But WebMediaPlayerImpl should not depend on media pipeline's implementation
1860 // detail.
1861 DCHECK(decryptor_ready_cb_.is_null());
1863 if (cdm_context_) {
1864 decryptor_ready_cb.Run(cdm_context_->GetDecryptor(),
1865 base::Bind(&media::IgnoreCdmAttached));
1866 return;
1869 decryptor_ready_cb_ = decryptor_ready_cb;
1872 void WebMediaPlayerAndroid::enterFullscreen() {
1873 if (is_player_initialized_)
1874 player_manager_->EnterFullscreen(player_id_);
1875 SetNeedsEstablishPeer(false);
1876 is_fullscreen_ = true;
1879 bool WebMediaPlayerAndroid::IsHLSStream() const {
1880 std::string mime;
1881 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_;
1882 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime))
1883 return false;
1884 return !mime.compare("application/x-mpegurl");
1887 void WebMediaPlayerAndroid::ReportHLSMetrics() const {
1888 if (player_type_ != MEDIA_PLAYER_TYPE_URL)
1889 return;
1891 bool is_hls = IsHLSStream();
1892 UMA_HISTOGRAM_BOOLEAN("Media.Android.IsHttpLiveStreamingMedia", is_hls);
1893 if (is_hls) {
1894 media::RecordOriginOfHLSPlayback(
1895 GURL(frame_->document().securityOrigin().toString()));
1899 } // namespace content