Don't send a SHChangeNotify for creating an app icon when creating a shortcut.
[chromium-blink-merge.git] / content / renderer / media / android / webmediaplayer_android.cc
blob5876307ccd7ffd632aa6fb71fba219581a3f0b26
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/WebString.h"
54 #include "third_party/WebKit/public/platform/WebURL.h"
55 #include "third_party/WebKit/public/web/WebDocument.h"
56 #include "third_party/WebKit/public/web/WebFrame.h"
57 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
58 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
59 #include "third_party/WebKit/public/web/WebView.h"
60 #include "third_party/skia/include/core/SkCanvas.h"
61 #include "third_party/skia/include/core/SkPaint.h"
62 #include "third_party/skia/include/core/SkTypeface.h"
63 #include "third_party/skia/include/gpu/GrContext.h"
64 #include "third_party/skia/include/gpu/SkGrPixelRef.h"
65 #include "ui/gfx/image/image.h"
67 static const uint32 kGLTextureExternalOES = 0x8D65;
68 static const int kSDKVersionToSupportSecurityOriginCheck = 20;
70 using blink::WebMediaPlayer;
71 using blink::WebSize;
72 using blink::WebString;
73 using blink::WebURL;
74 using gpu::gles2::GLES2Interface;
75 using media::MediaPlayerAndroid;
76 using media::VideoFrame;
78 namespace {
79 // Prefix for histograms related to Encrypted Media Extensions.
80 const char* kMediaEme = "Media.EME.";
82 // File-static function is to allow it to run even after WMPA is deleted.
83 void OnReleaseTexture(
84 const scoped_refptr<content::StreamTextureFactory>& factories,
85 uint32 texture_id,
86 uint32 release_sync_point) {
87 GLES2Interface* gl = factories->ContextGL();
88 gl->WaitSyncPointCHROMIUM(release_sync_point);
89 gl->DeleteTextures(1, &texture_id);
92 bool IsSkBitmapProperlySizedTexture(const SkBitmap* bitmap,
93 const gfx::Size& size) {
94 return bitmap->getTexture() && bitmap->width() == size.width() &&
95 bitmap->height() == size.height();
98 bool AllocateSkBitmapTexture(GrContext* gr,
99 SkBitmap* bitmap,
100 const gfx::Size& size) {
101 DCHECK(gr);
102 GrTextureDesc desc;
103 // Use kRGBA_8888_GrPixelConfig, not kSkia8888_GrPixelConfig, to avoid
104 // RGBA to BGRA conversion.
105 desc.fConfig = kRGBA_8888_GrPixelConfig;
106 // kRenderTarget_GrTextureFlagBit avoids a copy before readback in skia.
107 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
108 desc.fSampleCnt = 0;
109 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
110 desc.fWidth = size.width();
111 desc.fHeight = size.height();
112 skia::RefPtr<GrTexture> texture = skia::AdoptRef(
113 gr->refScratchTexture(desc, GrContext::kExact_ScratchTexMatch));
114 if (!texture.get())
115 return false;
117 SkImageInfo info = SkImageInfo::MakeN32Premul(desc.fWidth, desc.fHeight);
118 SkGrPixelRef* pixel_ref = SkNEW_ARGS(SkGrPixelRef, (info, texture.get()));
119 if (!pixel_ref)
120 return false;
121 bitmap->setInfo(info);
122 bitmap->setPixelRef(pixel_ref)->unref();
123 return true;
126 static blink::WebEncryptedMediaInitDataType ConvertInitDataType(
127 const std::string& init_data_type) {
128 if (init_data_type == "cenc")
129 return blink::WebEncryptedMediaInitDataType::Cenc;
130 if (init_data_type == "keyids")
131 return blink::WebEncryptedMediaInitDataType::Keyids;
132 if (init_data_type == "webm")
133 return blink::WebEncryptedMediaInitDataType::Webm;
134 NOTREACHED() << "unexpected " << init_data_type;
135 return blink::WebEncryptedMediaInitDataType::Unknown;
138 class SyncPointClientImpl : public media::VideoFrame::SyncPointClient {
139 public:
140 explicit SyncPointClientImpl(
141 blink::WebGraphicsContext3D* web_graphics_context)
142 : web_graphics_context_(web_graphics_context) {}
143 ~SyncPointClientImpl() override {}
144 uint32 InsertSyncPoint() override {
145 return web_graphics_context_->insertSyncPoint();
147 void WaitSyncPoint(uint32 sync_point) override {
148 web_graphics_context_->waitSyncPoint(sync_point);
151 private:
152 blink::WebGraphicsContext3D* web_graphics_context_;
155 } // namespace
157 namespace content {
159 WebMediaPlayerAndroid::WebMediaPlayerAndroid(
160 blink::WebFrame* frame,
161 blink::WebMediaPlayerClient* client,
162 base::WeakPtr<media::WebMediaPlayerDelegate> delegate,
163 RendererMediaPlayerManager* player_manager,
164 RendererCdmManager* cdm_manager,
165 media::MediaPermission* media_permission,
166 blink::WebContentDecryptionModule* initial_cdm,
167 scoped_refptr<StreamTextureFactory> factory,
168 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
169 media::MediaLog* media_log)
170 : RenderFrameObserver(RenderFrame::FromWebFrame(frame)),
171 frame_(frame),
172 client_(client),
173 delegate_(delegate),
174 buffered_(static_cast<size_t>(1)),
175 media_task_runner_(task_runner),
176 ignore_metadata_duration_change_(false),
177 pending_seek_(false),
178 seeking_(false),
179 did_loading_progress_(false),
180 player_manager_(player_manager),
181 cdm_manager_(cdm_manager),
182 media_permission_(media_permission),
183 network_state_(WebMediaPlayer::NetworkStateEmpty),
184 ready_state_(WebMediaPlayer::ReadyStateHaveNothing),
185 texture_id_(0),
186 stream_id_(0),
187 is_player_initialized_(false),
188 is_playing_(false),
189 needs_establish_peer_(true),
190 has_size_info_(false),
191 // Compositor thread does not exist in layout tests.
192 compositor_loop_(
193 RenderThreadImpl::current()->compositor_message_loop_proxy().get()
194 ? RenderThreadImpl::current()->compositor_message_loop_proxy()
195 : base::MessageLoopProxy::current()),
196 stream_texture_factory_(factory),
197 needs_external_surface_(false),
198 is_fullscreen_(false),
199 video_frame_provider_client_(NULL),
200 player_type_(MEDIA_PLAYER_TYPE_URL),
201 is_remote_(false),
202 media_log_(media_log),
203 cdm_context_(NULL),
204 allow_stored_credentials_(false),
205 is_local_resource_(false),
206 interpolator_(&default_tick_clock_),
207 weak_factory_(this) {
208 DCHECK(player_manager_);
209 DCHECK(cdm_manager_);
211 DCHECK(main_thread_checker_.CalledOnValidThread());
212 stream_texture_factory_->AddObserver(this);
214 player_id_ = player_manager_->RegisterMediaPlayer(this);
216 #if defined(VIDEO_HOLE)
217 const RendererPreferences& prefs =
218 static_cast<RenderFrameImpl*>(render_frame())
219 ->render_view()
220 ->renderer_preferences();
221 force_use_overlay_embedded_video_ = prefs.use_view_overlay_for_all_video;
222 if (force_use_overlay_embedded_video_ ||
223 player_manager_->ShouldUseVideoOverlayForEmbeddedEncryptedVideo()) {
224 // Defer stream texture creation until we are sure it's necessary.
225 needs_establish_peer_ = false;
226 current_frame_ = VideoFrame::CreateBlackFrame(gfx::Size(1, 1));
228 #endif // defined(VIDEO_HOLE)
229 TryCreateStreamTextureProxyIfNeeded();
230 interpolator_.SetUpperBound(base::TimeDelta());
232 if (initial_cdm) {
233 cdm_context_ =
234 media::ToWebContentDecryptionModuleImpl(initial_cdm)->GetCdmContext();
238 WebMediaPlayerAndroid::~WebMediaPlayerAndroid() {
239 DCHECK(main_thread_checker_.CalledOnValidThread());
240 SetVideoFrameProviderClient(NULL);
241 client_->setWebLayer(NULL);
243 if (is_player_initialized_)
244 player_manager_->DestroyPlayer(player_id_);
246 player_manager_->UnregisterMediaPlayer(player_id_);
248 if (stream_id_) {
249 GLES2Interface* gl = stream_texture_factory_->ContextGL();
250 gl->DeleteTextures(1, &texture_id_);
251 texture_id_ = 0;
252 texture_mailbox_ = gpu::Mailbox();
253 stream_id_ = 0;
257 base::AutoLock auto_lock(current_frame_lock_);
258 current_frame_ = NULL;
261 if (delegate_)
262 delegate_->PlayerGone(this);
264 stream_texture_factory_->RemoveObserver(this);
266 if (media_source_delegate_) {
267 // Part of |media_source_delegate_| needs to be stopped on the media thread.
268 // Wait until |media_source_delegate_| is fully stopped before tearing
269 // down other objects.
270 base::WaitableEvent waiter(false, false);
271 media_source_delegate_->Stop(
272 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&waiter)));
273 waiter.Wait();
277 void WebMediaPlayerAndroid::load(LoadType load_type,
278 const blink::WebURL& url,
279 CORSMode cors_mode) {
280 DCHECK(main_thread_checker_.CalledOnValidThread());
281 media::ReportMediaSchemeUma(GURL(url));
283 switch (load_type) {
284 case LoadTypeURL:
285 player_type_ = MEDIA_PLAYER_TYPE_URL;
286 break;
288 case LoadTypeMediaSource:
289 player_type_ = MEDIA_PLAYER_TYPE_MEDIA_SOURCE;
290 break;
292 case LoadTypeMediaStream:
293 CHECK(false) << "WebMediaPlayerAndroid doesn't support MediaStream on "
294 "this platform";
295 return;
298 url_ = url;
299 is_local_resource_ = IsLocalResource();
300 int demuxer_client_id = 0;
301 if (player_type_ != MEDIA_PLAYER_TYPE_URL) {
302 RendererDemuxerAndroid* demuxer =
303 RenderThreadImpl::current()->renderer_demuxer();
304 demuxer_client_id = demuxer->GetNextDemuxerClientID();
306 media_source_delegate_.reset(new MediaSourceDelegate(
307 demuxer, demuxer_client_id, media_task_runner_, media_log_));
309 if (player_type_ == MEDIA_PLAYER_TYPE_MEDIA_SOURCE) {
310 media_source_delegate_->InitializeMediaSource(
311 base::Bind(&WebMediaPlayerAndroid::OnMediaSourceOpened,
312 weak_factory_.GetWeakPtr()),
313 base::Bind(&WebMediaPlayerAndroid::OnEncryptedMediaInitData,
314 weak_factory_.GetWeakPtr()),
315 base::Bind(&WebMediaPlayerAndroid::SetDecryptorReadyCB,
316 weak_factory_.GetWeakPtr()),
317 base::Bind(&WebMediaPlayerAndroid::UpdateNetworkState,
318 weak_factory_.GetWeakPtr()),
319 base::Bind(&WebMediaPlayerAndroid::OnDurationChanged,
320 weak_factory_.GetWeakPtr()),
321 base::Bind(&WebMediaPlayerAndroid::OnWaitingForDecryptionKey,
322 weak_factory_.GetWeakPtr()));
323 InitializePlayer(url_, frame_->document().firstPartyForCookies(),
324 true, demuxer_client_id);
326 } else {
327 info_loader_.reset(
328 new MediaInfoLoader(
329 url,
330 cors_mode,
331 base::Bind(&WebMediaPlayerAndroid::DidLoadMediaInfo,
332 weak_factory_.GetWeakPtr())));
333 info_loader_->Start(frame_);
336 UpdateNetworkState(WebMediaPlayer::NetworkStateLoading);
337 UpdateReadyState(WebMediaPlayer::ReadyStateHaveNothing);
338 UMA_HISTOGRAM_BOOLEAN(
339 "Media.MSE.Playback", player_type_ == MEDIA_PLAYER_TYPE_MEDIA_SOURCE);
342 void WebMediaPlayerAndroid::DidLoadMediaInfo(
343 MediaInfoLoader::Status status,
344 const GURL& redirected_url,
345 const GURL& first_party_for_cookies,
346 bool allow_stored_credentials) {
347 DCHECK(main_thread_checker_.CalledOnValidThread());
348 DCHECK(!media_source_delegate_);
349 if (status == MediaInfoLoader::kFailed) {
350 info_loader_.reset();
351 UpdateNetworkState(WebMediaPlayer::NetworkStateNetworkError);
352 return;
354 redirected_url_ = redirected_url;
355 InitializePlayer(
356 redirected_url, first_party_for_cookies, allow_stored_credentials, 0);
358 UpdateNetworkState(WebMediaPlayer::NetworkStateIdle);
361 bool WebMediaPlayerAndroid::IsLocalResource() {
362 if (url_.SchemeIsFile() || url_.SchemeIsBlob())
363 return true;
365 std::string host = url_.host();
366 if (!host.compare("localhost") || !host.compare("127.0.0.1") ||
367 !host.compare("[::1]")) {
368 return true;
371 return false;
374 void WebMediaPlayerAndroid::play() {
375 DCHECK(main_thread_checker_.CalledOnValidThread());
377 // For HLS streams, some devices cannot detect the video size unless a surface
378 // texture is bind to it. See http://crbug.com/400145.
379 #if defined(VIDEO_HOLE)
380 if ((hasVideo() || IsHLSStream()) && needs_external_surface_ &&
381 !is_fullscreen_) {
382 DCHECK(!needs_establish_peer_);
383 player_manager_->RequestExternalSurface(player_id_, last_computed_rect_);
385 #endif // defined(VIDEO_HOLE)
387 TryCreateStreamTextureProxyIfNeeded();
388 // There is no need to establish the surface texture peer for fullscreen
389 // video.
390 if ((hasVideo() || IsHLSStream()) && needs_establish_peer_ &&
391 !is_fullscreen_) {
392 EstablishSurfaceTexturePeer();
395 if (paused())
396 player_manager_->Start(player_id_);
397 UpdatePlayingState(true);
398 UpdateNetworkState(WebMediaPlayer::NetworkStateLoading);
401 void WebMediaPlayerAndroid::pause() {
402 DCHECK(main_thread_checker_.CalledOnValidThread());
403 Pause(true);
406 void WebMediaPlayerAndroid::requestRemotePlayback() {
407 player_manager_->RequestRemotePlayback(player_id_);
410 void WebMediaPlayerAndroid::requestRemotePlaybackControl() {
411 player_manager_->RequestRemotePlaybackControl(player_id_);
414 void WebMediaPlayerAndroid::seek(double seconds) {
415 DCHECK(main_thread_checker_.CalledOnValidThread());
416 DVLOG(1) << __FUNCTION__ << "(" << seconds << ")";
418 base::TimeDelta new_seek_time = media::ConvertSecondsToTimestamp(seconds);
420 if (seeking_) {
421 if (new_seek_time == seek_time_) {
422 if (media_source_delegate_) {
423 if (!pending_seek_) {
424 // If using media source demuxer, only suppress redundant seeks if
425 // there is no pending seek. This enforces that any pending seek that
426 // results in a demuxer seek is preceded by matching
427 // CancelPendingSeek() and StartWaitingForSeek() calls.
428 return;
430 } else {
431 // Suppress all redundant seeks if unrestricted by media source
432 // demuxer API.
433 pending_seek_ = false;
434 return;
438 pending_seek_ = true;
439 pending_seek_time_ = new_seek_time;
441 if (media_source_delegate_)
442 media_source_delegate_->CancelPendingSeek(pending_seek_time_);
444 // Later, OnSeekComplete will trigger the pending seek.
445 return;
448 seeking_ = true;
449 seek_time_ = new_seek_time;
451 if (media_source_delegate_)
452 media_source_delegate_->StartWaitingForSeek(seek_time_);
454 // Kick off the asynchronous seek!
455 player_manager_->Seek(player_id_, seek_time_);
458 bool WebMediaPlayerAndroid::supportsSave() const {
459 return false;
462 void WebMediaPlayerAndroid::setRate(double rate) {
463 NOTIMPLEMENTED();
466 void WebMediaPlayerAndroid::setVolume(double volume) {
467 DCHECK(main_thread_checker_.CalledOnValidThread());
468 player_manager_->SetVolume(player_id_, volume);
471 bool WebMediaPlayerAndroid::hasVideo() const {
472 DCHECK(main_thread_checker_.CalledOnValidThread());
473 // If we have obtained video size information before, use it.
474 if (has_size_info_)
475 return !natural_size_.isEmpty();
477 // TODO(qinmin): need a better method to determine whether the current media
478 // content contains video. Android does not provide any function to do
479 // this.
480 // We don't know whether the current media content has video unless
481 // the player is prepared. If the player is not prepared, we fall back
482 // to the mime-type. There may be no mime-type on a redirect URL.
483 // In that case, we conservatively assume it contains video so that
484 // enterfullscreen call will not fail.
485 if (!url_.has_path())
486 return false;
487 std::string mime;
488 if (!net::GetMimeTypeFromFile(base::FilePath(url_.path()), &mime))
489 return true;
490 return mime.find("audio/") == std::string::npos;
493 bool WebMediaPlayerAndroid::hasAudio() const {
494 DCHECK(main_thread_checker_.CalledOnValidThread());
495 if (!url_.has_path())
496 return false;
497 std::string mime;
498 if (!net::GetMimeTypeFromFile(base::FilePath(url_.path()), &mime))
499 return true;
501 if (mime.find("audio/") != std::string::npos ||
502 mime.find("video/") != std::string::npos ||
503 mime.find("application/ogg") != std::string::npos) {
504 return true;
506 return false;
509 bool WebMediaPlayerAndroid::isRemote() const {
510 return is_remote_;
513 bool WebMediaPlayerAndroid::paused() const {
514 return !is_playing_;
517 bool WebMediaPlayerAndroid::seeking() const {
518 return seeking_;
521 double WebMediaPlayerAndroid::duration() const {
522 DCHECK(main_thread_checker_.CalledOnValidThread());
523 // HTML5 spec requires duration to be NaN if readyState is HAVE_NOTHING
524 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
525 return std::numeric_limits<double>::quiet_NaN();
527 if (duration_ == media::kInfiniteDuration())
528 return std::numeric_limits<double>::infinity();
530 return duration_.InSecondsF();
533 double WebMediaPlayerAndroid::timelineOffset() const {
534 DCHECK(main_thread_checker_.CalledOnValidThread());
535 base::Time timeline_offset;
536 if (media_source_delegate_)
537 timeline_offset = media_source_delegate_->GetTimelineOffset();
539 if (timeline_offset.is_null())
540 return std::numeric_limits<double>::quiet_NaN();
542 return timeline_offset.ToJsTime();
545 double WebMediaPlayerAndroid::currentTime() const {
546 DCHECK(main_thread_checker_.CalledOnValidThread());
547 // If the player is processing a seek, return the seek time.
548 // Blink may still query us if updatePlaybackState() occurs while seeking.
549 if (seeking()) {
550 return pending_seek_ ?
551 pending_seek_time_.InSecondsF() : seek_time_.InSecondsF();
554 return std::min(
555 (const_cast<media::TimeDeltaInterpolator*>(
556 &interpolator_))->GetInterpolatedTime(), duration_).InSecondsF();
559 WebSize WebMediaPlayerAndroid::naturalSize() const {
560 return natural_size_;
563 WebMediaPlayer::NetworkState WebMediaPlayerAndroid::networkState() const {
564 return network_state_;
567 WebMediaPlayer::ReadyState WebMediaPlayerAndroid::readyState() const {
568 return ready_state_;
571 blink::WebTimeRanges WebMediaPlayerAndroid::buffered() const {
572 if (media_source_delegate_)
573 return media_source_delegate_->Buffered();
574 return buffered_;
577 blink::WebTimeRanges WebMediaPlayerAndroid::seekable() const {
578 if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata)
579 return blink::WebTimeRanges();
581 // TODO(dalecurtis): Technically this allows seeking on media which return an
582 // infinite duration. While not expected, disabling this breaks semi-live
583 // players, http://crbug.com/427412.
584 const blink::WebTimeRange seekable_range(0.0, duration());
585 return blink::WebTimeRanges(&seekable_range, 1);
588 bool WebMediaPlayerAndroid::didLoadingProgress() {
589 bool ret = did_loading_progress_;
590 did_loading_progress_ = false;
591 return ret;
594 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas,
595 const blink::WebRect& rect,
596 unsigned char alpha,
597 SkXfermode::Mode mode) {
598 DCHECK(main_thread_checker_.CalledOnValidThread());
599 scoped_ptr<blink::WebGraphicsContext3DProvider> provider =
600 scoped_ptr<blink::WebGraphicsContext3DProvider>(blink::Platform::current(
601 )->createSharedOffscreenGraphicsContext3DProvider());
602 if (!provider)
603 return;
604 blink::WebGraphicsContext3D* context3D = provider->context3d();
605 if (!context3D)
606 return;
608 // Copy video texture into a RGBA texture based bitmap first as video texture
609 // on Android is GL_TEXTURE_EXTERNAL_OES which is not supported by Skia yet.
610 // The bitmap's size needs to be the same as the video and use naturalSize()
611 // here. Check if we could reuse existing texture based bitmap.
612 // Otherwise, release existing texture based bitmap and allocate
613 // a new one based on video size.
614 if (!IsSkBitmapProperlySizedTexture(&bitmap_, naturalSize())) {
615 if (!AllocateSkBitmapTexture(provider->grContext(), &bitmap_,
616 naturalSize())) {
617 return;
621 unsigned textureId = static_cast<unsigned>(
622 (bitmap_.getTexture())->getTextureHandle());
623 if (!copyVideoTextureToPlatformTexture(context3D, textureId, 0,
624 GL_RGBA, GL_UNSIGNED_BYTE, true, false)) {
625 return;
628 // Draw the texture based bitmap onto the Canvas. If the canvas is
629 // hardware based, this will do a GPU-GPU texture copy.
630 // If the canvas is software based, the texture based bitmap will be
631 // readbacked to system memory then draw onto the canvas.
632 SkRect dest;
633 dest.set(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
634 SkPaint paint;
635 paint.setAlpha(alpha);
636 paint.setXfermodeMode(mode);
637 // It is not necessary to pass the dest into the drawBitmap call since all
638 // the context have been set up before calling paintCurrentFrameInContext.
639 canvas->drawBitmapRect(bitmap_, 0, dest, &paint);
642 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture(
643 blink::WebGraphicsContext3D* web_graphics_context,
644 unsigned int texture,
645 unsigned int level,
646 unsigned int internal_format,
647 unsigned int type,
648 bool premultiply_alpha,
649 bool flip_y) {
650 return copyVideoTextureToPlatformTexture(web_graphics_context, texture,
651 internal_format, type,
652 premultiply_alpha, flip_y);
655 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture(
656 blink::WebGraphicsContext3D* web_graphics_context,
657 unsigned int texture,
658 unsigned int internal_format,
659 unsigned int type,
660 bool premultiply_alpha,
661 bool flip_y) {
662 DCHECK(main_thread_checker_.CalledOnValidThread());
663 // Don't allow clients to copy an encrypted video frame.
664 if (needs_external_surface_)
665 return false;
667 scoped_refptr<VideoFrame> video_frame;
669 base::AutoLock auto_lock(current_frame_lock_);
670 video_frame = current_frame_;
673 if (!video_frame.get() ||
674 video_frame->format() != media::VideoFrame::NATIVE_TEXTURE)
675 return false;
676 const gpu::MailboxHolder* mailbox_holder = video_frame->mailbox_holder();
677 DCHECK((!is_remote_ &&
678 mailbox_holder->texture_target == GL_TEXTURE_EXTERNAL_OES) ||
679 (is_remote_ && mailbox_holder->texture_target == GL_TEXTURE_2D));
681 web_graphics_context->waitSyncPoint(mailbox_holder->sync_point);
683 // Ensure the target of texture is set before copyTextureCHROMIUM, otherwise
684 // an invalid texture target may be used for copy texture.
685 uint32 src_texture = web_graphics_context->createAndConsumeTextureCHROMIUM(
686 mailbox_holder->texture_target, mailbox_holder->mailbox.name);
688 // The video is stored in an unmultiplied format, so premultiply if
689 // necessary.
690 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
691 premultiply_alpha);
693 // Application itself needs to take care of setting the right flip_y
694 // value down to get the expected result.
695 // flip_y==true means to reverse the video orientation while
696 // flip_y==false means to keep the intrinsic orientation.
697 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, flip_y);
698 web_graphics_context->copyTextureCHROMIUM(GL_TEXTURE_2D, src_texture, texture,
699 0, internal_format, type);
700 web_graphics_context->pixelStorei(GL_UNPACK_FLIP_Y_CHROMIUM, false);
701 web_graphics_context->pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_CHROMIUM,
702 false);
704 web_graphics_context->deleteTexture(src_texture);
705 web_graphics_context->flush();
707 SyncPointClientImpl client(web_graphics_context);
708 video_frame->UpdateReleaseSyncPoint(&client);
709 return true;
712 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const {
713 DCHECK(main_thread_checker_.CalledOnValidThread());
714 if (player_type_ != MEDIA_PLAYER_TYPE_URL)
715 return true;
717 if (!info_loader_ || !info_loader_->HasSingleOrigin())
718 return false;
720 // TODO(qinmin): The url might be redirected when android media player
721 // requests the stream. As a result, we cannot guarantee there is only
722 // a single origin. Only if the HTTP request was made without credentials,
723 // we will honor the return value from HasSingleSecurityOriginInternal()
724 // in pre-L android versions.
725 // Check http://crbug.com/334204.
726 if (!allow_stored_credentials_)
727 return true;
729 return base::android::BuildInfo::GetInstance()->sdk_int() >=
730 kSDKVersionToSupportSecurityOriginCheck;
733 bool WebMediaPlayerAndroid::didPassCORSAccessCheck() const {
734 DCHECK(main_thread_checker_.CalledOnValidThread());
735 if (info_loader_)
736 return info_loader_->DidPassCORSAccessCheck();
737 return false;
740 double WebMediaPlayerAndroid::mediaTimeForTimeValue(double timeValue) const {
741 return media::ConvertSecondsToTimestamp(timeValue).InSecondsF();
744 unsigned WebMediaPlayerAndroid::decodedFrameCount() const {
745 if (media_source_delegate_)
746 return media_source_delegate_->DecodedFrameCount();
747 NOTIMPLEMENTED();
748 return 0;
751 unsigned WebMediaPlayerAndroid::droppedFrameCount() const {
752 if (media_source_delegate_)
753 return media_source_delegate_->DroppedFrameCount();
754 NOTIMPLEMENTED();
755 return 0;
758 unsigned WebMediaPlayerAndroid::audioDecodedByteCount() const {
759 if (media_source_delegate_)
760 return media_source_delegate_->AudioDecodedByteCount();
761 NOTIMPLEMENTED();
762 return 0;
765 unsigned WebMediaPlayerAndroid::videoDecodedByteCount() const {
766 if (media_source_delegate_)
767 return media_source_delegate_->VideoDecodedByteCount();
768 NOTIMPLEMENTED();
769 return 0;
772 void WebMediaPlayerAndroid::OnMediaMetadataChanged(
773 const base::TimeDelta& duration, int width, int height, bool success) {
774 DCHECK(main_thread_checker_.CalledOnValidThread());
775 bool need_to_signal_duration_changed = false;
777 if (is_local_resource_)
778 UpdateNetworkState(WebMediaPlayer::NetworkStateLoaded);
780 // Update duration, if necessary, prior to ready state updates that may
781 // cause duration() query.
782 if (!ignore_metadata_duration_change_ && duration_ != duration) {
783 duration_ = duration;
784 if (is_local_resource_)
785 buffered_[0].end = duration_.InSecondsF();
786 // Client readyState transition from HAVE_NOTHING to HAVE_METADATA
787 // already triggers a durationchanged event. If this is a different
788 // transition, remember to signal durationchanged.
789 // Do not ever signal durationchanged on metadata change in MSE case
790 // because OnDurationChanged() handles this.
791 if (ready_state_ > WebMediaPlayer::ReadyStateHaveNothing &&
792 player_type_ != MEDIA_PLAYER_TYPE_MEDIA_SOURCE) {
793 need_to_signal_duration_changed = true;
797 if (ready_state_ != WebMediaPlayer::ReadyStateHaveEnoughData) {
798 UpdateReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
799 UpdateReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
802 // TODO(wolenetz): Should we just abort early and set network state to an
803 // error if success == false? See http://crbug.com/248399
804 if (success)
805 OnVideoSizeChanged(width, height);
807 if (need_to_signal_duration_changed)
808 client_->durationChanged();
811 void WebMediaPlayerAndroid::OnPlaybackComplete() {
812 // When playback is about to finish, android media player often stops
813 // at a time which is smaller than the duration. This makes webkit never
814 // know that the playback has finished. To solve this, we set the
815 // current time to media duration when OnPlaybackComplete() get called.
816 interpolator_.SetBounds(duration_, duration_);
817 client_->timeChanged();
819 // If the loop attribute is set, timeChanged() will update the current time
820 // to 0. It will perform a seek to 0. Issue a command to the player to start
821 // playing after seek completes.
822 if (seeking_ && seek_time_ == base::TimeDelta())
823 player_manager_->Start(player_id_);
826 void WebMediaPlayerAndroid::OnBufferingUpdate(int percentage) {
827 buffered_[0].end = duration() * percentage / 100;
828 did_loading_progress_ = true;
831 void WebMediaPlayerAndroid::OnSeekRequest(const base::TimeDelta& time_to_seek) {
832 DCHECK(main_thread_checker_.CalledOnValidThread());
833 client_->requestSeek(time_to_seek.InSecondsF());
836 void WebMediaPlayerAndroid::OnSeekComplete(
837 const base::TimeDelta& current_time) {
838 DCHECK(main_thread_checker_.CalledOnValidThread());
839 seeking_ = false;
840 if (pending_seek_) {
841 pending_seek_ = false;
842 seek(pending_seek_time_.InSecondsF());
843 return;
845 interpolator_.SetBounds(current_time, current_time);
847 UpdateReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
849 client_->timeChanged();
852 void WebMediaPlayerAndroid::OnMediaError(int error_type) {
853 switch (error_type) {
854 case MediaPlayerAndroid::MEDIA_ERROR_FORMAT:
855 UpdateNetworkState(WebMediaPlayer::NetworkStateFormatError);
856 break;
857 case MediaPlayerAndroid::MEDIA_ERROR_DECODE:
858 UpdateNetworkState(WebMediaPlayer::NetworkStateDecodeError);
859 break;
860 case MediaPlayerAndroid::MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
861 UpdateNetworkState(WebMediaPlayer::NetworkStateFormatError);
862 break;
863 case MediaPlayerAndroid::MEDIA_ERROR_INVALID_CODE:
864 break;
866 client_->repaint();
869 void WebMediaPlayerAndroid::OnVideoSizeChanged(int width, int height) {
870 DCHECK(main_thread_checker_.CalledOnValidThread());
871 has_size_info_ = true;
872 if (natural_size_.width == width && natural_size_.height == height)
873 return;
875 #if defined(VIDEO_HOLE)
876 // Use H/W surface for encrypted video.
877 // TODO(qinmin): Change this so that only EME needs the H/W surface
878 if (force_use_overlay_embedded_video_ ||
879 (media_source_delegate_ && media_source_delegate_->IsVideoEncrypted() &&
880 player_manager_->ShouldUseVideoOverlayForEmbeddedEncryptedVideo())) {
881 needs_external_surface_ = true;
882 if (!paused() && !is_fullscreen_)
883 player_manager_->RequestExternalSurface(player_id_, last_computed_rect_);
884 } else if (!stream_texture_proxy_) {
885 // Do deferred stream texture creation finally.
886 SetNeedsEstablishPeer(true);
887 TryCreateStreamTextureProxyIfNeeded();
889 #endif // defined(VIDEO_HOLE)
890 natural_size_.width = width;
891 natural_size_.height = height;
893 // When play() gets called, |natural_size_| may still be empty and
894 // EstablishSurfaceTexturePeer() will not get called. As a result, the video
895 // may play without a surface texture. When we finally get the valid video
896 // size here, we should call EstablishSurfaceTexturePeer() if it has not been
897 // previously called.
898 if (!paused() && needs_establish_peer_)
899 EstablishSurfaceTexturePeer();
901 ReallocateVideoFrame();
903 // For hidden video element (with style "display:none"), ensure the texture
904 // size is set.
905 if (!is_remote_ && cached_stream_texture_size_ != natural_size_) {
906 stream_texture_factory_->SetStreamTextureSize(
907 stream_id_, gfx::Size(natural_size_.width, natural_size_.height));
908 cached_stream_texture_size_ = natural_size_;
911 // Lazily allocate compositing layer.
912 if (!video_weblayer_) {
913 video_weblayer_.reset(new cc_blink::WebLayerImpl(
914 cc::VideoLayer::Create(this, media::VIDEO_ROTATION_0)));
915 client_->setWebLayer(video_weblayer_.get());
918 // TODO(qinmin): This is a hack. We need the media element to stop showing the
919 // poster image by forcing it to call setDisplayMode(video). Should move the
920 // logic into HTMLMediaElement.cpp.
921 client_->timeChanged();
924 void WebMediaPlayerAndroid::OnTimeUpdate(base::TimeDelta current_timestamp,
925 base::TimeTicks current_time_ticks) {
926 DCHECK(main_thread_checker_.CalledOnValidThread());
927 // Compensate the current_timestamp with the IPC latency.
928 base::TimeDelta lower_bound =
929 base::TimeTicks::Now() - current_time_ticks + current_timestamp;
930 base::TimeDelta upper_bound = lower_bound;
931 // We should get another time update in about |kTimeUpdateInterval|
932 // milliseconds.
933 if (is_playing_) {
934 upper_bound += base::TimeDelta::FromMilliseconds(
935 media::kTimeUpdateInterval);
937 // if the lower_bound is smaller than the current time, just use the current
938 // time so that the timer is always progressing.
939 lower_bound =
940 std::min(lower_bound, base::TimeDelta::FromSecondsD(currentTime()));
941 interpolator_.SetBounds(lower_bound, upper_bound);
944 void WebMediaPlayerAndroid::OnConnectedToRemoteDevice(
945 const std::string& remote_playback_message) {
946 DCHECK(main_thread_checker_.CalledOnValidThread());
947 DCHECK(!media_source_delegate_);
948 DrawRemotePlaybackText(remote_playback_message);
949 is_remote_ = true;
950 SetNeedsEstablishPeer(false);
951 client_->connectedToRemoteDevice();
954 void WebMediaPlayerAndroid::OnDisconnectedFromRemoteDevice() {
955 DCHECK(main_thread_checker_.CalledOnValidThread());
956 DCHECK(!media_source_delegate_);
957 SetNeedsEstablishPeer(true);
958 if (!paused())
959 EstablishSurfaceTexturePeer();
960 is_remote_ = false;
961 ReallocateVideoFrame();
962 client_->disconnectedFromRemoteDevice();
965 void WebMediaPlayerAndroid::OnDidExitFullscreen() {
966 // |needs_external_surface_| is always false on non-TV devices.
967 if (!needs_external_surface_)
968 SetNeedsEstablishPeer(true);
969 // We had the fullscreen surface connected to Android MediaPlayer,
970 // so reconnect our surface texture for embedded playback.
971 if (!paused() && needs_establish_peer_)
972 EstablishSurfaceTexturePeer();
974 #if defined(VIDEO_HOLE)
975 if (!paused() && needs_external_surface_)
976 player_manager_->RequestExternalSurface(player_id_, last_computed_rect_);
977 #endif // defined(VIDEO_HOLE)
978 is_fullscreen_ = false;
979 client_->repaint();
982 void WebMediaPlayerAndroid::OnMediaPlayerPlay() {
983 UpdatePlayingState(true);
984 client_->playbackStateChanged();
987 void WebMediaPlayerAndroid::OnMediaPlayerPause() {
988 UpdatePlayingState(false);
989 client_->playbackStateChanged();
992 void WebMediaPlayerAndroid::OnRequestFullscreen() {
993 client_->requestFullscreen();
996 void WebMediaPlayerAndroid::OnRemoteRouteAvailabilityChanged(
997 bool routes_available) {
998 client_->remoteRouteAvailabilityChanged(routes_available);
1001 void WebMediaPlayerAndroid::OnDurationChanged(const base::TimeDelta& duration) {
1002 DCHECK(main_thread_checker_.CalledOnValidThread());
1003 // Only MSE |player_type_| registers this callback.
1004 DCHECK_EQ(player_type_, MEDIA_PLAYER_TYPE_MEDIA_SOURCE);
1006 // Cache the new duration value and trust it over any subsequent duration
1007 // values received in OnMediaMetadataChanged().
1008 duration_ = duration;
1009 ignore_metadata_duration_change_ = true;
1011 // Notify MediaPlayerClient that duration has changed, if > HAVE_NOTHING.
1012 if (ready_state_ > WebMediaPlayer::ReadyStateHaveNothing)
1013 client_->durationChanged();
1016 void WebMediaPlayerAndroid::UpdateNetworkState(
1017 WebMediaPlayer::NetworkState state) {
1018 DCHECK(main_thread_checker_.CalledOnValidThread());
1019 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing &&
1020 (state == WebMediaPlayer::NetworkStateNetworkError ||
1021 state == WebMediaPlayer::NetworkStateDecodeError)) {
1022 // Any error that occurs before reaching ReadyStateHaveMetadata should
1023 // be considered a format error.
1024 network_state_ = WebMediaPlayer::NetworkStateFormatError;
1025 } else {
1026 network_state_ = state;
1028 client_->networkStateChanged();
1031 void WebMediaPlayerAndroid::UpdateReadyState(
1032 WebMediaPlayer::ReadyState state) {
1033 ready_state_ = state;
1034 client_->readyStateChanged();
1037 void WebMediaPlayerAndroid::OnPlayerReleased() {
1038 // |needs_external_surface_| is always false on non-TV devices.
1039 if (!needs_external_surface_)
1040 needs_establish_peer_ = true;
1042 if (is_playing_)
1043 OnMediaPlayerPause();
1045 #if defined(VIDEO_HOLE)
1046 last_computed_rect_ = gfx::RectF();
1047 #endif // defined(VIDEO_HOLE)
1050 void WebMediaPlayerAndroid::ReleaseMediaResources() {
1051 switch (network_state_) {
1052 // Pause the media player and inform WebKit if the player is in a good
1053 // shape.
1054 case WebMediaPlayer::NetworkStateIdle:
1055 case WebMediaPlayer::NetworkStateLoading:
1056 case WebMediaPlayer::NetworkStateLoaded:
1057 Pause(false);
1058 client_->playbackStateChanged();
1059 break;
1060 // If a WebMediaPlayer instance has entered into one of these states,
1061 // the internal network state in HTMLMediaElement could be set to empty.
1062 // And calling playbackStateChanged() could get this object deleted.
1063 case WebMediaPlayer::NetworkStateEmpty:
1064 case WebMediaPlayer::NetworkStateFormatError:
1065 case WebMediaPlayer::NetworkStateNetworkError:
1066 case WebMediaPlayer::NetworkStateDecodeError:
1067 break;
1069 player_manager_->ReleaseResources(player_id_);
1070 if (!needs_external_surface_)
1071 SetNeedsEstablishPeer(true);
1074 void WebMediaPlayerAndroid::OnDestruct() {
1075 NOTREACHED() << "WebMediaPlayer should be destroyed before any "
1076 "RenderFrameObserver::OnDestruct() gets called when "
1077 "the RenderFrame goes away.";
1080 void WebMediaPlayerAndroid::InitializePlayer(
1081 const GURL& url,
1082 const GURL& first_party_for_cookies,
1083 bool allow_stored_credentials,
1084 int demuxer_client_id) {
1085 if (player_type_ == MEDIA_PLAYER_TYPE_URL) {
1086 UMA_HISTOGRAM_BOOLEAN("Media.Android.IsHttpLiveStreamingMedia",
1087 IsHLSStream());
1089 allow_stored_credentials_ = allow_stored_credentials;
1090 player_manager_->Initialize(
1091 player_type_, player_id_, url, first_party_for_cookies, demuxer_client_id,
1092 frame_->document().url(), allow_stored_credentials);
1093 is_player_initialized_ = true;
1095 if (is_fullscreen_)
1096 player_manager_->EnterFullscreen(player_id_);
1098 if (cdm_context_)
1099 SetCdmInternal(base::Bind(&media::IgnoreCdmAttached));
1102 void WebMediaPlayerAndroid::Pause(bool is_media_related_action) {
1103 player_manager_->Pause(player_id_, is_media_related_action);
1104 UpdatePlayingState(false);
1107 void WebMediaPlayerAndroid::DrawRemotePlaybackText(
1108 const std::string& remote_playback_message) {
1109 DCHECK(main_thread_checker_.CalledOnValidThread());
1110 if (!video_weblayer_)
1111 return;
1113 // TODO(johnme): Should redraw this frame if the layer bounds change; but
1114 // there seems no easy way to listen for the layer resizing (as opposed to
1115 // OnVideoSizeChanged, which is when the frame sizes of the video file
1116 // change). Perhaps have to poll (on main thread of course)?
1117 gfx::Size video_size_css_px = video_weblayer_->bounds();
1118 float device_scale_factor = frame_->view()->deviceScaleFactor();
1119 // canvas_size will be the size in device pixels when pageScaleFactor == 1
1120 gfx::Size canvas_size(
1121 static_cast<int>(video_size_css_px.width() * device_scale_factor),
1122 static_cast<int>(video_size_css_px.height() * device_scale_factor));
1124 SkBitmap bitmap;
1125 bitmap.allocN32Pixels(canvas_size.width(), canvas_size.height());
1127 // Create the canvas and draw the "Casting to <Chromecast>" text on it.
1128 SkCanvas canvas(bitmap);
1129 canvas.drawColor(SK_ColorBLACK);
1131 const SkScalar kTextSize(40);
1132 const SkScalar kMinPadding(40);
1134 SkPaint paint;
1135 paint.setAntiAlias(true);
1136 paint.setFilterQuality(kHigh_SkFilterQuality);
1137 paint.setColor(SK_ColorWHITE);
1138 paint.setTypeface(SkTypeface::CreateFromName("sans", SkTypeface::kBold));
1139 paint.setTextSize(kTextSize);
1141 // Calculate the vertical margin from the top
1142 SkPaint::FontMetrics font_metrics;
1143 paint.getFontMetrics(&font_metrics);
1144 SkScalar sk_vertical_margin = kMinPadding - font_metrics.fAscent;
1146 // Measure the width of the entire text to display
1147 size_t display_text_width = paint.measureText(
1148 remote_playback_message.c_str(), remote_playback_message.size());
1149 std::string display_text(remote_playback_message);
1151 if (display_text_width + (kMinPadding * 2) > canvas_size.width()) {
1152 // The text is too long to fit in one line, truncate it and append ellipsis
1153 // to the end.
1155 // First, figure out how much of the canvas the '...' will take up.
1156 const std::string kTruncationEllipsis("\xE2\x80\xA6");
1157 SkScalar sk_ellipse_width = paint.measureText(
1158 kTruncationEllipsis.c_str(), kTruncationEllipsis.size());
1160 // Then calculate how much of the text can be drawn with the '...' appended
1161 // to the end of the string.
1162 SkScalar sk_max_original_text_width(
1163 canvas_size.width() - (kMinPadding * 2) - sk_ellipse_width);
1164 size_t sk_max_original_text_length = paint.breakText(
1165 remote_playback_message.c_str(),
1166 remote_playback_message.size(),
1167 sk_max_original_text_width);
1169 // Remove the part of the string that doesn't fit and append '...'.
1170 display_text.erase(sk_max_original_text_length,
1171 remote_playback_message.size() - sk_max_original_text_length);
1172 display_text.append(kTruncationEllipsis);
1173 display_text_width = paint.measureText(
1174 display_text.c_str(), display_text.size());
1177 // Center the text horizontally.
1178 SkScalar sk_horizontal_margin =
1179 (canvas_size.width() - display_text_width) / 2.0;
1180 canvas.drawText(display_text.c_str(),
1181 display_text.size(),
1182 sk_horizontal_margin,
1183 sk_vertical_margin,
1184 paint);
1186 GLES2Interface* gl = stream_texture_factory_->ContextGL();
1187 GLuint remote_playback_texture_id = 0;
1188 gl->GenTextures(1, &remote_playback_texture_id);
1189 GLuint texture_target = GL_TEXTURE_2D;
1190 gl->BindTexture(texture_target, remote_playback_texture_id);
1191 gl->TexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1192 gl->TexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1193 gl->TexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1194 gl->TexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1197 SkAutoLockPixels lock(bitmap);
1198 gl->TexImage2D(texture_target,
1199 0 /* level */,
1200 GL_RGBA /* internalformat */,
1201 bitmap.width(),
1202 bitmap.height(),
1203 0 /* border */,
1204 GL_RGBA /* format */,
1205 GL_UNSIGNED_BYTE /* type */,
1206 bitmap.getPixels());
1209 gpu::Mailbox texture_mailbox;
1210 gl->GenMailboxCHROMIUM(texture_mailbox.name);
1211 gl->ProduceTextureCHROMIUM(texture_target, texture_mailbox.name);
1212 gl->Flush();
1213 GLuint texture_mailbox_sync_point = gl->InsertSyncPointCHROMIUM();
1215 scoped_refptr<VideoFrame> new_frame = VideoFrame::WrapNativeTexture(
1216 make_scoped_ptr(new gpu::MailboxHolder(texture_mailbox, texture_target,
1217 texture_mailbox_sync_point)),
1218 media::BindToCurrentLoop(base::Bind(&OnReleaseTexture,
1219 stream_texture_factory_,
1220 remote_playback_texture_id)),
1221 canvas_size /* coded_size */, gfx::Rect(canvas_size) /* visible_rect */,
1222 canvas_size /* natural_size */, base::TimeDelta() /* timestamp */,
1223 false /* allow overlay */);
1224 SetCurrentFrameInternal(new_frame);
1227 void WebMediaPlayerAndroid::ReallocateVideoFrame() {
1228 DCHECK(main_thread_checker_.CalledOnValidThread());
1229 if (needs_external_surface_) {
1230 // VideoFrame::CreateHoleFrame is only defined under VIDEO_HOLE.
1231 #if defined(VIDEO_HOLE)
1232 if (!natural_size_.isEmpty()) {
1233 // Now we finally know that "stream texture" and "video frame" won't
1234 // be needed. EME uses "external surface" and "video hole" instead.
1235 ResetStreamTextureProxy();
1236 scoped_refptr<VideoFrame> new_frame =
1237 VideoFrame::CreateHoleFrame(natural_size_);
1238 SetCurrentFrameInternal(new_frame);
1239 // Force the client to grab the hole frame.
1240 client_->repaint();
1242 #else
1243 NOTIMPLEMENTED() << "Hole punching not supported without VIDEO_HOLE flag";
1244 #endif // defined(VIDEO_HOLE)
1245 } else if (!is_remote_ && texture_id_) {
1246 GLES2Interface* gl = stream_texture_factory_->ContextGL();
1247 GLuint texture_target = kGLTextureExternalOES;
1248 GLuint texture_id_ref = gl->CreateAndConsumeTextureCHROMIUM(
1249 texture_target, texture_mailbox_.name);
1250 gl->Flush();
1251 GLuint texture_mailbox_sync_point = gl->InsertSyncPointCHROMIUM();
1253 scoped_refptr<VideoFrame> new_frame = VideoFrame::WrapNativeTexture(
1254 make_scoped_ptr(new gpu::MailboxHolder(texture_mailbox_, texture_target,
1255 texture_mailbox_sync_point)),
1256 media::BindToCurrentLoop(base::Bind(
1257 &OnReleaseTexture, stream_texture_factory_, texture_id_ref)),
1258 natural_size_, gfx::Rect(natural_size_), natural_size_,
1259 base::TimeDelta(), false);
1260 SetCurrentFrameInternal(new_frame);
1264 void WebMediaPlayerAndroid::SetVideoFrameProviderClient(
1265 cc::VideoFrameProvider::Client* client) {
1266 // This is called from both the main renderer thread and the compositor
1267 // thread (when the main thread is blocked).
1269 // Set the callback target when a frame is produced. Need to do this before
1270 // StopUsingProvider to ensure we really stop using the client.
1271 if (stream_texture_proxy_)
1272 stream_texture_proxy_->BindToLoop(stream_id_, client, compositor_loop_);
1274 if (video_frame_provider_client_ && video_frame_provider_client_ != client)
1275 video_frame_provider_client_->StopUsingProvider();
1276 video_frame_provider_client_ = client;
1279 void WebMediaPlayerAndroid::SetCurrentFrameInternal(
1280 scoped_refptr<media::VideoFrame>& video_frame) {
1281 DCHECK(main_thread_checker_.CalledOnValidThread());
1282 base::AutoLock auto_lock(current_frame_lock_);
1283 current_frame_ = video_frame;
1286 scoped_refptr<media::VideoFrame> WebMediaPlayerAndroid::GetCurrentFrame() {
1287 scoped_refptr<VideoFrame> video_frame;
1289 base::AutoLock auto_lock(current_frame_lock_);
1290 video_frame = current_frame_;
1293 return video_frame;
1296 void WebMediaPlayerAndroid::PutCurrentFrame(
1297 const scoped_refptr<media::VideoFrame>& frame) {
1300 void WebMediaPlayerAndroid::ResetStreamTextureProxy() {
1301 DCHECK(main_thread_checker_.CalledOnValidThread());
1303 if (stream_id_) {
1304 GLES2Interface* gl = stream_texture_factory_->ContextGL();
1305 gl->DeleteTextures(1, &texture_id_);
1306 texture_id_ = 0;
1307 texture_mailbox_ = gpu::Mailbox();
1308 stream_id_ = 0;
1310 stream_texture_proxy_.reset();
1311 needs_establish_peer_ = !needs_external_surface_ && !is_remote_ &&
1312 !is_fullscreen_ &&
1313 (hasVideo() || IsHLSStream());
1315 TryCreateStreamTextureProxyIfNeeded();
1316 if (needs_establish_peer_ && is_playing_)
1317 EstablishSurfaceTexturePeer();
1320 void WebMediaPlayerAndroid::TryCreateStreamTextureProxyIfNeeded() {
1321 DCHECK(main_thread_checker_.CalledOnValidThread());
1322 // Already created.
1323 if (stream_texture_proxy_)
1324 return;
1326 // No factory to create proxy.
1327 if (!stream_texture_factory_.get())
1328 return;
1330 // Not needed for hole punching.
1331 if (!needs_establish_peer_)
1332 return;
1334 stream_texture_proxy_.reset(stream_texture_factory_->CreateProxy());
1335 if (stream_texture_proxy_) {
1336 DoCreateStreamTexture();
1337 ReallocateVideoFrame();
1338 if (video_frame_provider_client_) {
1339 stream_texture_proxy_->BindToLoop(
1340 stream_id_, video_frame_provider_client_, compositor_loop_);
1345 void WebMediaPlayerAndroid::EstablishSurfaceTexturePeer() {
1346 DCHECK(main_thread_checker_.CalledOnValidThread());
1347 if (!stream_texture_proxy_)
1348 return;
1350 if (stream_texture_factory_.get() && stream_id_)
1351 stream_texture_factory_->EstablishPeer(stream_id_, player_id_);
1353 // Set the deferred size because the size was changed in remote mode.
1354 if (!is_remote_ && cached_stream_texture_size_ != natural_size_) {
1355 stream_texture_factory_->SetStreamTextureSize(
1356 stream_id_, gfx::Size(natural_size_.width, natural_size_.height));
1357 cached_stream_texture_size_ = natural_size_;
1360 needs_establish_peer_ = false;
1363 void WebMediaPlayerAndroid::DoCreateStreamTexture() {
1364 DCHECK(main_thread_checker_.CalledOnValidThread());
1365 DCHECK(!stream_id_);
1366 DCHECK(!texture_id_);
1367 stream_id_ = stream_texture_factory_->CreateStreamTexture(
1368 kGLTextureExternalOES, &texture_id_, &texture_mailbox_);
1371 void WebMediaPlayerAndroid::SetNeedsEstablishPeer(bool needs_establish_peer) {
1372 needs_establish_peer_ = needs_establish_peer;
1375 void WebMediaPlayerAndroid::setPoster(const blink::WebURL& poster) {
1376 player_manager_->SetPoster(player_id_, poster);
1379 void WebMediaPlayerAndroid::UpdatePlayingState(bool is_playing) {
1380 if (is_playing == is_playing_)
1381 return;
1383 is_playing_ = is_playing;
1385 if (is_playing)
1386 interpolator_.StartInterpolating();
1387 else
1388 interpolator_.StopInterpolating();
1390 if (delegate_) {
1391 if (is_playing)
1392 delegate_->DidPlay(this);
1393 else
1394 delegate_->DidPause(this);
1398 #if defined(VIDEO_HOLE)
1399 bool WebMediaPlayerAndroid::UpdateBoundaryRectangle() {
1400 if (!video_weblayer_)
1401 return false;
1403 // Compute the geometry of video frame layer.
1404 cc::Layer* layer = video_weblayer_->layer();
1405 gfx::RectF rect(layer->bounds());
1406 while (layer) {
1407 rect.Offset(layer->position().OffsetFromOrigin());
1408 layer = layer->parent();
1411 // Return false when the geometry hasn't been changed from the last time.
1412 if (last_computed_rect_ == rect)
1413 return false;
1415 // Store the changed geometry information when it is actually changed.
1416 last_computed_rect_ = rect;
1417 return true;
1420 const gfx::RectF WebMediaPlayerAndroid::GetBoundaryRectangle() {
1421 return last_computed_rect_;
1423 #endif
1425 // The following EME related code is copied from WebMediaPlayerImpl.
1426 // TODO(xhwang): Remove duplicate code between WebMediaPlayerAndroid and
1427 // WebMediaPlayerImpl.
1429 // Convert a WebString to ASCII, falling back on an empty string in the case
1430 // of a non-ASCII string.
1431 static std::string ToASCIIOrEmpty(const blink::WebString& string) {
1432 return base::IsStringASCII(string) ? base::UTF16ToASCII(string)
1433 : std::string();
1436 // Helper functions to report media EME related stats to UMA. They follow the
1437 // convention of more commonly used macros UMA_HISTOGRAM_ENUMERATION and
1438 // UMA_HISTOGRAM_COUNTS. The reason that we cannot use those macros directly is
1439 // that UMA_* macros require the names to be constant throughout the process'
1440 // lifetime.
1442 static void EmeUMAHistogramEnumeration(const std::string& key_system,
1443 const std::string& method,
1444 int sample,
1445 int boundary_value) {
1446 base::LinearHistogram::FactoryGet(
1447 kMediaEme + media::GetKeySystemNameForUMA(key_system) + "." + method,
1448 1, boundary_value, boundary_value + 1,
1449 base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
1452 static void EmeUMAHistogramCounts(const std::string& key_system,
1453 const std::string& method,
1454 int sample) {
1455 // Use the same parameters as UMA_HISTOGRAM_COUNTS.
1456 base::Histogram::FactoryGet(
1457 kMediaEme + media::GetKeySystemNameForUMA(key_system) + "." + method,
1458 1, 1000000, 50, base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
1461 // Helper enum for reporting generateKeyRequest/addKey histograms.
1462 enum MediaKeyException {
1463 kUnknownResultId,
1464 kSuccess,
1465 kKeySystemNotSupported,
1466 kInvalidPlayerState,
1467 kMaxMediaKeyException
1470 static MediaKeyException MediaKeyExceptionForUMA(
1471 WebMediaPlayer::MediaKeyException e) {
1472 switch (e) {
1473 case WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported:
1474 return kKeySystemNotSupported;
1475 case WebMediaPlayer::MediaKeyExceptionInvalidPlayerState:
1476 return kInvalidPlayerState;
1477 case WebMediaPlayer::MediaKeyExceptionNoError:
1478 return kSuccess;
1479 default:
1480 return kUnknownResultId;
1484 // Helper for converting |key_system| name and exception |e| to a pair of enum
1485 // values from above, for reporting to UMA.
1486 static void ReportMediaKeyExceptionToUMA(const std::string& method,
1487 const std::string& key_system,
1488 WebMediaPlayer::MediaKeyException e) {
1489 MediaKeyException result_id = MediaKeyExceptionForUMA(e);
1490 DCHECK_NE(result_id, kUnknownResultId) << e;
1491 EmeUMAHistogramEnumeration(
1492 key_system, method, result_id, kMaxMediaKeyException);
1495 bool WebMediaPlayerAndroid::IsKeySystemSupported(
1496 const std::string& key_system) {
1497 // On Android, EME only works with MSE.
1498 return player_type_ == MEDIA_PLAYER_TYPE_MEDIA_SOURCE &&
1499 media::PrefixedIsSupportedConcreteKeySystem(key_system);
1502 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::generateKeyRequest(
1503 const WebString& key_system,
1504 const unsigned char* init_data,
1505 unsigned init_data_length) {
1506 DVLOG(1) << "generateKeyRequest: " << base::string16(key_system) << ": "
1507 << std::string(reinterpret_cast<const char*>(init_data),
1508 static_cast<size_t>(init_data_length));
1510 std::string ascii_key_system =
1511 media::GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
1513 WebMediaPlayer::MediaKeyException e =
1514 GenerateKeyRequestInternal(ascii_key_system, init_data, init_data_length);
1515 ReportMediaKeyExceptionToUMA("generateKeyRequest", ascii_key_system, e);
1516 return e;
1519 // Guess the type of |init_data|. This is only used to handle some corner cases
1520 // so we keep it as simple as possible without breaking major use cases.
1521 static std::string GuessInitDataType(const unsigned char* init_data,
1522 unsigned init_data_length) {
1523 // Most WebM files use KeyId of 16 bytes. CENC init data is always >16 bytes.
1524 if (init_data_length == 16)
1525 return "webm";
1527 return "cenc";
1530 // TODO(xhwang): Report an error when there is encrypted stream but EME is
1531 // not enabled. Currently the player just doesn't start and waits for
1532 // ever.
1533 WebMediaPlayer::MediaKeyException
1534 WebMediaPlayerAndroid::GenerateKeyRequestInternal(
1535 const std::string& key_system,
1536 const unsigned char* init_data,
1537 unsigned init_data_length) {
1538 if (!IsKeySystemSupported(key_system))
1539 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
1541 // We do not support run-time switching between key systems for now.
1542 if (current_key_system_.empty()) {
1543 if (!proxy_decryptor_) {
1544 proxy_decryptor_.reset(new media::ProxyDecryptor(
1545 media_permission_,
1546 base::Bind(&WebMediaPlayerAndroid::OnKeyAdded,
1547 weak_factory_.GetWeakPtr()),
1548 base::Bind(&WebMediaPlayerAndroid::OnKeyError,
1549 weak_factory_.GetWeakPtr()),
1550 base::Bind(&WebMediaPlayerAndroid::OnKeyMessage,
1551 weak_factory_.GetWeakPtr())));
1554 GURL security_origin(frame_->document().securityOrigin().toString());
1555 RenderCdmFactory cdm_factory(cdm_manager_);
1556 if (!proxy_decryptor_->InitializeCDM(&cdm_factory, key_system,
1557 security_origin)) {
1558 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
1561 // Set the CDM onto the media player.
1562 cdm_context_ = proxy_decryptor_->GetCdmContext();
1564 if (is_player_initialized_)
1565 SetCdmInternal(base::Bind(&media::IgnoreCdmAttached));
1567 current_key_system_ = key_system;
1568 } else if (key_system != current_key_system_) {
1569 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
1572 std::string init_data_type = init_data_type_;
1573 if (init_data_type.empty())
1574 init_data_type = GuessInitDataType(init_data, init_data_length);
1576 // TODO(xhwang): We assume all streams are from the same container (thus have
1577 // the same "type") for now. In the future, the "type" should be passed down
1578 // from the application.
1579 if (!proxy_decryptor_->GenerateKeyRequest(
1580 init_data_type, init_data, init_data_length)) {
1581 current_key_system_.clear();
1582 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
1585 return WebMediaPlayer::MediaKeyExceptionNoError;
1588 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::addKey(
1589 const WebString& key_system,
1590 const unsigned char* key,
1591 unsigned key_length,
1592 const unsigned char* init_data,
1593 unsigned init_data_length,
1594 const WebString& session_id) {
1595 DVLOG(1) << "addKey: " << base::string16(key_system) << ": "
1596 << std::string(reinterpret_cast<const char*>(key),
1597 static_cast<size_t>(key_length)) << ", "
1598 << std::string(reinterpret_cast<const char*>(init_data),
1599 static_cast<size_t>(init_data_length)) << " ["
1600 << base::string16(session_id) << "]";
1602 std::string ascii_key_system =
1603 media::GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
1604 std::string ascii_session_id = ToASCIIOrEmpty(session_id);
1606 WebMediaPlayer::MediaKeyException e = AddKeyInternal(ascii_key_system,
1607 key,
1608 key_length,
1609 init_data,
1610 init_data_length,
1611 ascii_session_id);
1612 ReportMediaKeyExceptionToUMA("addKey", ascii_key_system, e);
1613 return e;
1616 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::AddKeyInternal(
1617 const std::string& key_system,
1618 const unsigned char* key,
1619 unsigned key_length,
1620 const unsigned char* init_data,
1621 unsigned init_data_length,
1622 const std::string& session_id) {
1623 DCHECK(key);
1624 DCHECK_GT(key_length, 0u);
1626 if (!IsKeySystemSupported(key_system))
1627 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
1629 if (current_key_system_.empty() || key_system != current_key_system_)
1630 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
1632 proxy_decryptor_->AddKey(
1633 key, key_length, init_data, init_data_length, session_id);
1634 return WebMediaPlayer::MediaKeyExceptionNoError;
1637 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::cancelKeyRequest(
1638 const WebString& key_system,
1639 const WebString& session_id) {
1640 DVLOG(1) << "cancelKeyRequest: " << base::string16(key_system) << ": "
1641 << " [" << base::string16(session_id) << "]";
1643 std::string ascii_key_system =
1644 media::GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
1645 std::string ascii_session_id = ToASCIIOrEmpty(session_id);
1647 WebMediaPlayer::MediaKeyException e =
1648 CancelKeyRequestInternal(ascii_key_system, ascii_session_id);
1649 ReportMediaKeyExceptionToUMA("cancelKeyRequest", ascii_key_system, e);
1650 return e;
1653 WebMediaPlayer::MediaKeyException
1654 WebMediaPlayerAndroid::CancelKeyRequestInternal(const std::string& key_system,
1655 const std::string& session_id) {
1656 if (!IsKeySystemSupported(key_system))
1657 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
1659 if (current_key_system_.empty() || key_system != current_key_system_)
1660 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
1662 proxy_decryptor_->CancelKeyRequest(session_id);
1663 return WebMediaPlayer::MediaKeyExceptionNoError;
1666 void WebMediaPlayerAndroid::setContentDecryptionModule(
1667 blink::WebContentDecryptionModule* cdm,
1668 blink::WebContentDecryptionModuleResult result) {
1669 DCHECK(main_thread_checker_.CalledOnValidThread());
1671 // TODO(xhwang): Support setMediaKeys(0) if necessary: http://crbug.com/330324
1672 if (!cdm) {
1673 result.completeWithError(
1674 blink::WebContentDecryptionModuleExceptionNotSupportedError,
1676 "Null MediaKeys object is not supported.");
1677 return;
1680 cdm_context_ = media::ToWebContentDecryptionModuleImpl(cdm)->GetCdmContext();
1682 if (is_player_initialized_) {
1683 SetCdmInternal(media::BindToCurrentLoop(
1684 base::Bind(&WebMediaPlayerAndroid::ContentDecryptionModuleAttached,
1685 weak_factory_.GetWeakPtr(), result)));
1686 } else {
1687 // No pipeline/decoder connected, so resolve the promise. When something
1688 // is connected, setting the CDM will happen in SetDecryptorReadyCB().
1689 ContentDecryptionModuleAttached(result, true);
1693 void WebMediaPlayerAndroid::ContentDecryptionModuleAttached(
1694 blink::WebContentDecryptionModuleResult result,
1695 bool success) {
1696 if (success) {
1697 result.complete();
1698 return;
1701 result.completeWithError(
1702 blink::WebContentDecryptionModuleExceptionNotSupportedError,
1704 "Unable to set MediaKeys object");
1707 void WebMediaPlayerAndroid::OnKeyAdded(const std::string& session_id) {
1708 EmeUMAHistogramCounts(current_key_system_, "KeyAdded", 1);
1710 client_->keyAdded(
1711 WebString::fromUTF8(media::GetPrefixedKeySystemName(current_key_system_)),
1712 WebString::fromUTF8(session_id));
1715 void WebMediaPlayerAndroid::OnKeyError(const std::string& session_id,
1716 media::MediaKeys::KeyError error_code,
1717 uint32 system_code) {
1718 EmeUMAHistogramEnumeration(current_key_system_, "KeyError",
1719 error_code, media::MediaKeys::kMaxKeyError);
1721 unsigned short short_system_code = 0;
1722 if (system_code > std::numeric_limits<unsigned short>::max()) {
1723 LOG(WARNING) << "system_code exceeds unsigned short limit.";
1724 short_system_code = std::numeric_limits<unsigned short>::max();
1725 } else {
1726 short_system_code = static_cast<unsigned short>(system_code);
1729 client_->keyError(
1730 WebString::fromUTF8(media::GetPrefixedKeySystemName(current_key_system_)),
1731 WebString::fromUTF8(session_id),
1732 static_cast<blink::WebMediaPlayerClient::MediaKeyErrorCode>(error_code),
1733 short_system_code);
1736 void WebMediaPlayerAndroid::OnKeyMessage(const std::string& session_id,
1737 const std::vector<uint8>& message,
1738 const GURL& destination_url) {
1739 DCHECK(destination_url.is_empty() || destination_url.is_valid());
1741 client_->keyMessage(
1742 WebString::fromUTF8(media::GetPrefixedKeySystemName(current_key_system_)),
1743 WebString::fromUTF8(session_id),
1744 message.empty() ? NULL : &message[0],
1745 message.size(),
1746 destination_url);
1749 void WebMediaPlayerAndroid::OnMediaSourceOpened(
1750 blink::WebMediaSource* web_media_source) {
1751 client_->mediaSourceOpened(web_media_source);
1754 // TODO(jrummell): |init_data_type| should be an enum. http://crbug.com/417440
1755 void WebMediaPlayerAndroid::OnEncryptedMediaInitData(
1756 const std::string& init_data_type,
1757 const std::vector<uint8>& init_data) {
1758 DCHECK(main_thread_checker_.CalledOnValidThread());
1760 // Do not fire NeedKey event if encrypted media is not enabled.
1761 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
1762 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
1763 return;
1766 UMA_HISTOGRAM_COUNTS(kMediaEme + std::string("NeedKey"), 1);
1768 DCHECK(!init_data_type.empty());
1769 DLOG_IF(WARNING,
1770 !init_data_type_.empty() && init_data_type != init_data_type_)
1771 << "Mixed init data type not supported. The new type is ignored.";
1772 if (init_data_type_.empty())
1773 init_data_type_ = init_data_type;
1775 client_->encrypted(ConvertInitDataType(init_data_type),
1776 vector_as_array(&init_data), init_data.size());
1779 void WebMediaPlayerAndroid::OnWaitingForDecryptionKey() {
1780 client_->didBlockPlaybackWaitingForKey();
1782 // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called
1783 // when a key has been successfully added (e.g. OnSessionKeysChange() with
1784 // |has_additional_usable_key| = true). http://crbug.com/461903
1785 client_->didResumePlaybackBlockedForKey();
1788 void WebMediaPlayerAndroid::SetCdmInternal(
1789 const media::CdmAttachedCB& cdm_attached_cb) {
1790 DCHECK(cdm_context_ && is_player_initialized_);
1791 DCHECK(cdm_context_->GetDecryptor() ||
1792 cdm_context_->GetCdmId() != media::CdmContext::kInvalidCdmId)
1793 << "CDM should support either a Decryptor or a CDM ID.";
1795 media::Decryptor* decryptor = cdm_context_->GetDecryptor();
1797 // Note:
1798 // - If |decryptor| is non-null, only handles |decryptor_ready_cb_| and
1799 // ignores the CDM ID.
1800 // - If |decryptor| is null (in which case the CDM ID should be valid),
1801 // returns any pending |decryptor_ready_cb_| with null, so that
1802 // MediaSourceDelegate will fall back to use a browser side (IPC-based) CDM,
1803 // then calls SetCdm() through the |player_manager_|.
1805 if (decryptor) {
1806 if (!decryptor_ready_cb_.is_null()) {
1807 base::ResetAndReturn(&decryptor_ready_cb_)
1808 .Run(decryptor, cdm_attached_cb);
1809 } else {
1810 cdm_attached_cb.Run(true);
1812 return;
1815 // |decryptor| is null.
1816 if (!decryptor_ready_cb_.is_null()) {
1817 base::ResetAndReturn(&decryptor_ready_cb_)
1818 .Run(nullptr, base::Bind(&media::IgnoreCdmAttached));
1821 DCHECK(cdm_context_->GetCdmId() != media::CdmContext::kInvalidCdmId);
1822 player_manager_->SetCdm(player_id_, cdm_context_->GetCdmId());
1823 cdm_attached_cb.Run(true);
1826 void WebMediaPlayerAndroid::SetDecryptorReadyCB(
1827 const media::DecryptorReadyCB& decryptor_ready_cb) {
1828 DCHECK(main_thread_checker_.CalledOnValidThread());
1829 DCHECK(is_player_initialized_);
1831 // Cancels the previous decryptor request.
1832 if (decryptor_ready_cb.is_null()) {
1833 if (!decryptor_ready_cb_.is_null()) {
1834 base::ResetAndReturn(&decryptor_ready_cb_)
1835 .Run(NULL, base::Bind(&media::IgnoreCdmAttached));
1837 return;
1840 // TODO(xhwang): Support multiple decryptor notification request (e.g. from
1841 // video and audio). The current implementation is okay for the current
1842 // media pipeline since we initialize audio and video decoders in sequence.
1843 // But WebMediaPlayerImpl should not depend on media pipeline's implementation
1844 // detail.
1845 DCHECK(decryptor_ready_cb_.is_null());
1847 if (cdm_context_) {
1848 decryptor_ready_cb.Run(cdm_context_->GetDecryptor(),
1849 base::Bind(&media::IgnoreCdmAttached));
1850 return;
1853 decryptor_ready_cb_ = decryptor_ready_cb;
1856 void WebMediaPlayerAndroid::enterFullscreen() {
1857 player_manager_->EnterFullscreen(player_id_);
1858 SetNeedsEstablishPeer(false);
1859 is_fullscreen_ = true;
1862 bool WebMediaPlayerAndroid::IsHLSStream() const {
1863 std::string mime;
1864 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_;
1865 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime))
1866 return false;
1867 return !mime.compare("application/x-mpegurl");
1870 } // namespace content