Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / content / renderer / media / android / webmediaplayer_android.cc
blob9d86f141aa927211b07009f219ad63c541181f7e
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/timestamp_constants.h"
44 #include "media/base/video_frame.h"
45 #include "media/blink/webcontentdecryptionmodule_impl.h"
46 #include "media/blink/webmediaplayer_delegate.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::LogHelper;
77 using media::MediaLog;
78 using media::MediaPlayerAndroid;
79 using media::VideoFrame;
81 namespace {
82 // Prefix for histograms related to Encrypted Media Extensions.
83 const char* kMediaEme = "Media.EME.";
85 // File-static function is to allow it to run even after WMPA is deleted.
86 void OnReleaseTexture(
87 const scoped_refptr<content::StreamTextureFactory>& factories,
88 uint32 texture_id,
89 uint32 release_sync_point) {
90 GLES2Interface* gl = factories->ContextGL();
91 gl->WaitSyncPointCHROMIUM(release_sync_point);
92 gl->DeleteTextures(1, &texture_id);
93 // Flush to ensure that the stream texture gets deleted in a timely fashion.
94 gl->ShallowFlushCHROMIUM();
97 bool IsSkBitmapProperlySizedTexture(const SkBitmap* bitmap,
98 const gfx::Size& size) {
99 return bitmap->getTexture() && bitmap->width() == size.width() &&
100 bitmap->height() == size.height();
103 bool AllocateSkBitmapTexture(GrContext* gr,
104 SkBitmap* bitmap,
105 const gfx::Size& size) {
106 DCHECK(gr);
107 GrTextureDesc desc;
108 // Use kRGBA_8888_GrPixelConfig, not kSkia8888_GrPixelConfig, to avoid
109 // RGBA to BGRA conversion.
110 desc.fConfig = kRGBA_8888_GrPixelConfig;
111 // kRenderTarget_GrTextureFlagBit avoids a copy before readback in skia.
112 desc.fFlags = kRenderTarget_GrSurfaceFlag;
113 desc.fSampleCnt = 0;
114 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
115 desc.fWidth = size.width();
116 desc.fHeight = size.height();
117 skia::RefPtr<GrTexture> texture = skia::AdoptRef(
118 gr->textureProvider()->refScratchTexture(
119 desc, GrTextureProvider::kExact_ScratchTexMatch));
120 if (!texture.get())
121 return false;
123 SkImageInfo info = SkImageInfo::MakeN32Premul(desc.fWidth, desc.fHeight);
124 SkGrPixelRef* pixel_ref = SkNEW_ARGS(SkGrPixelRef, (info, texture.get()));
125 if (!pixel_ref)
126 return false;
127 bitmap->setInfo(info);
128 bitmap->setPixelRef(pixel_ref)->unref();
129 return true;
132 class SyncPointClientImpl : public media::VideoFrame::SyncPointClient {
133 public:
134 explicit SyncPointClientImpl(
135 blink::WebGraphicsContext3D* web_graphics_context)
136 : web_graphics_context_(web_graphics_context) {}
137 ~SyncPointClientImpl() override {}
138 uint32 InsertSyncPoint() override {
139 return web_graphics_context_->insertSyncPoint();
141 void WaitSyncPoint(uint32 sync_point) override {
142 web_graphics_context_->waitSyncPoint(sync_point);
145 private:
146 blink::WebGraphicsContext3D* web_graphics_context_;
149 } // namespace
151 namespace content {
153 WebMediaPlayerAndroid::WebMediaPlayerAndroid(
154 blink::WebFrame* frame,
155 blink::WebMediaPlayerClient* client,
156 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
157 base::WeakPtr<media::WebMediaPlayerDelegate> delegate,
158 RendererMediaPlayerManager* player_manager,
159 media::CdmFactory* cdm_factory,
160 scoped_refptr<StreamTextureFactory> factory,
161 const media::WebMediaPlayerParams& params)
162 : RenderFrameObserver(RenderFrame::FromWebFrame(frame)),
163 frame_(frame),
164 client_(client),
165 encrypted_client_(encrypted_client),
166 delegate_(delegate),
167 defer_load_cb_(params.defer_load_cb()),
168 buffered_(static_cast<size_t>(1)),
169 media_task_runner_(params.media_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_(params.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 // Threaded compositing isn't enabled universally yet.
186 compositor_task_runner_(
187 params.compositor_task_runner()
188 ? params.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_(params.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 (params.initial_cdm()) {
228 cdm_context_ = media::ToWebContentDecryptionModuleImpl(params.initial_cdm())
229 ->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 if (!defer_load_cb_.is_null()) {
278 defer_load_cb_.Run(base::Bind(&WebMediaPlayerAndroid::DoLoad,
279 weak_factory_.GetWeakPtr(), load_type, url,
280 cors_mode));
281 return;
283 DoLoad(load_type, url, cors_mode);
286 void WebMediaPlayerAndroid::DoLoad(LoadType load_type,
287 const blink::WebURL& url,
288 CORSMode cors_mode) {
289 DCHECK(main_thread_checker_.CalledOnValidThread());
291 media::ReportMetrics(load_type, GURL(url),
292 GURL(frame_->document().securityOrigin().toString()));
294 switch (load_type) {
295 case LoadTypeURL:
296 player_type_ = MEDIA_PLAYER_TYPE_URL;
297 break;
299 case LoadTypeMediaSource:
300 player_type_ = MEDIA_PLAYER_TYPE_MEDIA_SOURCE;
301 break;
303 case LoadTypeMediaStream:
304 CHECK(false) << "WebMediaPlayerAndroid doesn't support MediaStream on "
305 "this platform";
306 return;
309 url_ = url;
310 is_local_resource_ = IsLocalResource();
311 int demuxer_client_id = 0;
312 if (player_type_ != MEDIA_PLAYER_TYPE_URL) {
313 RendererDemuxerAndroid* demuxer =
314 RenderThreadImpl::current()->renderer_demuxer();
315 demuxer_client_id = demuxer->GetNextDemuxerClientID();
317 media_source_delegate_.reset(new MediaSourceDelegate(
318 demuxer, demuxer_client_id, media_task_runner_, media_log_));
320 if (player_type_ == MEDIA_PLAYER_TYPE_MEDIA_SOURCE) {
321 media_source_delegate_->InitializeMediaSource(
322 base::Bind(&WebMediaPlayerAndroid::OnMediaSourceOpened,
323 weak_factory_.GetWeakPtr()),
324 base::Bind(&WebMediaPlayerAndroid::OnEncryptedMediaInitData,
325 weak_factory_.GetWeakPtr()),
326 base::Bind(&WebMediaPlayerAndroid::SetDecryptorReadyCB,
327 weak_factory_.GetWeakPtr()),
328 base::Bind(&WebMediaPlayerAndroid::UpdateNetworkState,
329 weak_factory_.GetWeakPtr()),
330 base::Bind(&WebMediaPlayerAndroid::OnDurationChanged,
331 weak_factory_.GetWeakPtr()),
332 base::Bind(&WebMediaPlayerAndroid::OnWaitingForDecryptionKey,
333 weak_factory_.GetWeakPtr()));
334 InitializePlayer(url_, frame_->document().firstPartyForCookies(),
335 true, demuxer_client_id);
337 } else {
338 info_loader_.reset(
339 new MediaInfoLoader(
340 url,
341 cors_mode,
342 base::Bind(&WebMediaPlayerAndroid::DidLoadMediaInfo,
343 weak_factory_.GetWeakPtr())));
344 info_loader_->Start(frame_);
347 UpdateNetworkState(WebMediaPlayer::NetworkStateLoading);
348 UpdateReadyState(WebMediaPlayer::ReadyStateHaveNothing);
351 void WebMediaPlayerAndroid::DidLoadMediaInfo(
352 MediaInfoLoader::Status status,
353 const GURL& redirected_url,
354 const GURL& first_party_for_cookies,
355 bool allow_stored_credentials) {
356 DCHECK(main_thread_checker_.CalledOnValidThread());
357 DCHECK(!media_source_delegate_);
358 if (status == MediaInfoLoader::kFailed) {
359 info_loader_.reset();
360 UpdateNetworkState(WebMediaPlayer::NetworkStateNetworkError);
361 return;
363 redirected_url_ = redirected_url;
364 InitializePlayer(
365 redirected_url, first_party_for_cookies, allow_stored_credentials, 0);
367 UpdateNetworkState(WebMediaPlayer::NetworkStateIdle);
370 bool WebMediaPlayerAndroid::IsLocalResource() {
371 if (url_.SchemeIsFile() || url_.SchemeIsBlob())
372 return true;
374 std::string host = url_.host();
375 if (!host.compare("localhost") || !host.compare("127.0.0.1") ||
376 !host.compare("[::1]")) {
377 return true;
380 return false;
383 void WebMediaPlayerAndroid::play() {
384 DCHECK(main_thread_checker_.CalledOnValidThread());
386 // For HLS streams, some devices cannot detect the video size unless a surface
387 // texture is bind to it. See http://crbug.com/400145.
388 #if defined(VIDEO_HOLE)
389 if ((hasVideo() || IsHLSStream()) && needs_external_surface_ &&
390 !is_fullscreen_) {
391 DCHECK(!needs_establish_peer_);
392 player_manager_->RequestExternalSurface(player_id_, last_computed_rect_);
394 #endif // defined(VIDEO_HOLE)
396 TryCreateStreamTextureProxyIfNeeded();
397 // There is no need to establish the surface texture peer for fullscreen
398 // video.
399 if ((hasVideo() || IsHLSStream()) && needs_establish_peer_ &&
400 !is_fullscreen_) {
401 EstablishSurfaceTexturePeer();
404 if (paused())
405 player_manager_->Start(player_id_);
406 UpdatePlayingState(true);
407 UpdateNetworkState(WebMediaPlayer::NetworkStateLoading);
410 void WebMediaPlayerAndroid::pause() {
411 DCHECK(main_thread_checker_.CalledOnValidThread());
412 Pause(true);
415 void WebMediaPlayerAndroid::requestRemotePlayback() {
416 player_manager_->RequestRemotePlayback(player_id_);
419 void WebMediaPlayerAndroid::requestRemotePlaybackControl() {
420 player_manager_->RequestRemotePlaybackControl(player_id_);
423 void WebMediaPlayerAndroid::seek(double seconds) {
424 DCHECK(main_thread_checker_.CalledOnValidThread());
425 DVLOG(1) << __FUNCTION__ << "(" << seconds << ")";
427 base::TimeDelta new_seek_time = base::TimeDelta::FromSecondsD(seconds);
429 if (seeking_) {
430 if (new_seek_time == seek_time_) {
431 if (media_source_delegate_) {
432 // Don't suppress any redundant in-progress MSE seek. There could have
433 // been changes to the underlying buffers after seeking the demuxer and
434 // before receiving OnSeekComplete() for the currently in-progress seek.
435 MEDIA_LOG(DEBUG, media_log_)
436 << "Detected MediaSource seek to same time as in-progress seek to "
437 << seek_time_ << ".";
438 } else {
439 // Suppress all redundant seeks if unrestricted by media source
440 // demuxer API.
441 pending_seek_ = false;
442 return;
446 pending_seek_ = true;
447 pending_seek_time_ = new_seek_time;
449 if (media_source_delegate_)
450 media_source_delegate_->CancelPendingSeek(pending_seek_time_);
452 // Later, OnSeekComplete will trigger the pending seek.
453 return;
456 seeking_ = true;
457 seek_time_ = new_seek_time;
459 if (media_source_delegate_)
460 media_source_delegate_->StartWaitingForSeek(seek_time_);
462 // Kick off the asynchronous seek!
463 player_manager_->Seek(player_id_, seek_time_);
466 bool WebMediaPlayerAndroid::supportsSave() const {
467 return false;
470 void WebMediaPlayerAndroid::setRate(double rate) {
471 NOTIMPLEMENTED();
474 void WebMediaPlayerAndroid::setVolume(double volume) {
475 DCHECK(main_thread_checker_.CalledOnValidThread());
476 player_manager_->SetVolume(player_id_, volume);
479 void WebMediaPlayerAndroid::setSinkId(
480 const blink::WebString& device_id,
481 media::WebSetSinkIdCB* raw_web_callbacks) {
482 DCHECK(main_thread_checker_.CalledOnValidThread());
483 scoped_ptr<media::WebSetSinkIdCB> web_callbacks(raw_web_callbacks);
484 web_callbacks->onError(new blink::WebSetSinkIdError(
485 blink::WebSetSinkIdError::ErrorTypeNotSupported, "Not Supported"));
488 bool WebMediaPlayerAndroid::hasVideo() const {
489 DCHECK(main_thread_checker_.CalledOnValidThread());
490 // If we have obtained video size information before, use it.
491 if (has_size_info_)
492 return !natural_size_.isEmpty();
494 // TODO(qinmin): need a better method to determine whether the current media
495 // content contains video. Android does not provide any function to do
496 // this.
497 // We don't know whether the current media content has video unless
498 // the player is prepared. If the player is not prepared, we fall back
499 // to the mime-type. There may be no mime-type on a redirect URL.
500 // In that case, we conservatively assume it contains video so that
501 // enterfullscreen call will not fail.
502 if (!url_.has_path())
503 return false;
504 std::string mime;
505 if (!net::GetMimeTypeFromFile(base::FilePath(url_.path()), &mime))
506 return true;
507 return mime.find("audio/") == std::string::npos;
510 bool WebMediaPlayerAndroid::hasAudio() const {
511 DCHECK(main_thread_checker_.CalledOnValidThread());
512 if (!url_.has_path())
513 return false;
514 std::string mime;
515 if (!net::GetMimeTypeFromFile(base::FilePath(url_.path()), &mime))
516 return true;
518 if (mime.find("audio/") != std::string::npos ||
519 mime.find("video/") != std::string::npos ||
520 mime.find("application/ogg") != std::string::npos ||
521 mime.find("application/x-mpegurl") != std::string::npos) {
522 return true;
524 return false;
527 bool WebMediaPlayerAndroid::isRemote() const {
528 return is_remote_;
531 bool WebMediaPlayerAndroid::paused() const {
532 return !is_playing_;
535 bool WebMediaPlayerAndroid::seeking() const {
536 return seeking_;
539 double WebMediaPlayerAndroid::duration() const {
540 DCHECK(main_thread_checker_.CalledOnValidThread());
541 // HTML5 spec requires duration to be NaN if readyState is HAVE_NOTHING
542 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing)
543 return std::numeric_limits<double>::quiet_NaN();
545 if (duration_ == media::kInfiniteDuration())
546 return std::numeric_limits<double>::infinity();
548 return duration_.InSecondsF();
551 double WebMediaPlayerAndroid::timelineOffset() const {
552 DCHECK(main_thread_checker_.CalledOnValidThread());
553 base::Time timeline_offset;
554 if (media_source_delegate_)
555 timeline_offset = media_source_delegate_->GetTimelineOffset();
557 if (timeline_offset.is_null())
558 return std::numeric_limits<double>::quiet_NaN();
560 return timeline_offset.ToJsTime();
563 double WebMediaPlayerAndroid::currentTime() const {
564 DCHECK(main_thread_checker_.CalledOnValidThread());
565 // If the player is processing a seek, return the seek time.
566 // Blink may still query us if updatePlaybackState() occurs while seeking.
567 if (seeking()) {
568 return pending_seek_ ?
569 pending_seek_time_.InSecondsF() : seek_time_.InSecondsF();
572 return std::min(
573 (const_cast<media::TimeDeltaInterpolator*>(
574 &interpolator_))->GetInterpolatedTime(), duration_).InSecondsF();
577 WebSize WebMediaPlayerAndroid::naturalSize() const {
578 return natural_size_;
581 WebMediaPlayer::NetworkState WebMediaPlayerAndroid::networkState() const {
582 return network_state_;
585 WebMediaPlayer::ReadyState WebMediaPlayerAndroid::readyState() const {
586 return ready_state_;
589 blink::WebTimeRanges WebMediaPlayerAndroid::buffered() const {
590 if (media_source_delegate_)
591 return media_source_delegate_->Buffered();
592 return buffered_;
595 blink::WebTimeRanges WebMediaPlayerAndroid::seekable() const {
596 if (ready_state_ < WebMediaPlayer::ReadyStateHaveMetadata)
597 return blink::WebTimeRanges();
599 // TODO(dalecurtis): Technically this allows seeking on media which return an
600 // infinite duration. While not expected, disabling this breaks semi-live
601 // players, http://crbug.com/427412.
602 const blink::WebTimeRange seekable_range(0.0, duration());
603 return blink::WebTimeRanges(&seekable_range, 1);
606 bool WebMediaPlayerAndroid::didLoadingProgress() {
607 bool ret = did_loading_progress_;
608 did_loading_progress_ = false;
609 return ret;
612 void WebMediaPlayerAndroid::paint(blink::WebCanvas* canvas,
613 const blink::WebRect& rect,
614 unsigned char alpha,
615 SkXfermode::Mode mode) {
616 DCHECK(main_thread_checker_.CalledOnValidThread());
617 scoped_ptr<blink::WebGraphicsContext3DProvider> provider =
618 scoped_ptr<blink::WebGraphicsContext3DProvider>(blink::Platform::current(
619 )->createSharedOffscreenGraphicsContext3DProvider());
620 if (!provider)
621 return;
622 blink::WebGraphicsContext3D* context3D = provider->context3d();
623 if (!context3D)
624 return;
626 // Copy video texture into a RGBA texture based bitmap first as video texture
627 // on Android is GL_TEXTURE_EXTERNAL_OES which is not supported by Skia yet.
628 // The bitmap's size needs to be the same as the video and use naturalSize()
629 // here. Check if we could reuse existing texture based bitmap.
630 // Otherwise, release existing texture based bitmap and allocate
631 // a new one based on video size.
632 if (!IsSkBitmapProperlySizedTexture(&bitmap_, naturalSize())) {
633 if (!AllocateSkBitmapTexture(provider->grContext(), &bitmap_,
634 naturalSize())) {
635 return;
639 unsigned textureId = static_cast<unsigned>(
640 (bitmap_.getTexture())->getTextureHandle());
641 if (!copyVideoTextureToPlatformTexture(context3D, textureId,
642 GL_RGBA, GL_UNSIGNED_BYTE, true, false)) {
643 return;
646 // Draw the texture based bitmap onto the Canvas. If the canvas is
647 // hardware based, this will do a GPU-GPU texture copy.
648 // If the canvas is software based, the texture based bitmap will be
649 // readbacked to system memory then draw onto the canvas.
650 SkRect dest;
651 dest.set(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
652 SkPaint paint;
653 paint.setAlpha(alpha);
654 paint.setXfermodeMode(mode);
655 // It is not necessary to pass the dest into the drawBitmap call since all
656 // the context have been set up before calling paintCurrentFrameInContext.
657 canvas->drawBitmapRect(bitmap_, dest, &paint);
660 bool WebMediaPlayerAndroid::copyVideoTextureToPlatformTexture(
661 blink::WebGraphicsContext3D* web_graphics_context,
662 unsigned int texture,
663 unsigned int internal_format,
664 unsigned int type,
665 bool premultiply_alpha,
666 bool flip_y) {
667 DCHECK(main_thread_checker_.CalledOnValidThread());
668 // Don't allow clients to copy an encrypted video frame.
669 if (needs_external_surface_)
670 return false;
672 scoped_refptr<VideoFrame> video_frame;
674 base::AutoLock auto_lock(current_frame_lock_);
675 video_frame = current_frame_;
678 if (!video_frame.get() || !video_frame->HasTextures())
679 return false;
680 DCHECK_EQ(1u, media::VideoFrame::NumPlanes(video_frame->format()));
681 const gpu::MailboxHolder& mailbox_holder = video_frame->mailbox_holder(0);
682 DCHECK((!is_remote_ &&
683 mailbox_holder.texture_target == GL_TEXTURE_EXTERNAL_OES) ||
684 (is_remote_ && mailbox_holder.texture_target == GL_TEXTURE_2D));
686 web_graphics_context->waitSyncPoint(mailbox_holder.sync_point);
688 // Ensure the target of texture is set before copyTextureCHROMIUM, otherwise
689 // an invalid texture target may be used for copy texture.
690 uint32 src_texture =
691 web_graphics_context->createAndConsumeTextureCHROMIUM(
692 mailbox_holder.texture_target, mailbox_holder.mailbox.name);
694 // Application itself needs to take care of setting the right flip_y
695 // value down to get the expected result.
696 // flip_y==true means to reverse the video orientation while
697 // flip_y==false means to keep the intrinsic orientation.
698 web_graphics_context->copyTextureCHROMIUM(
699 GL_TEXTURE_2D, src_texture, texture, internal_format, type,
700 flip_y, premultiply_alpha, false);
702 web_graphics_context->deleteTexture(src_texture);
703 web_graphics_context->flush();
705 SyncPointClientImpl client(web_graphics_context);
706 video_frame->UpdateReleaseSyncPoint(&client);
707 return true;
710 bool WebMediaPlayerAndroid::hasSingleSecurityOrigin() const {
711 DCHECK(main_thread_checker_.CalledOnValidThread());
712 if (player_type_ != MEDIA_PLAYER_TYPE_URL)
713 return true;
715 if (!info_loader_ || !info_loader_->HasSingleOrigin())
716 return false;
718 // TODO(qinmin): The url might be redirected when android media player
719 // requests the stream. As a result, we cannot guarantee there is only
720 // a single origin. Only if the HTTP request was made without credentials,
721 // we will honor the return value from HasSingleSecurityOriginInternal()
722 // in pre-L android versions.
723 // Check http://crbug.com/334204.
724 if (!allow_stored_credentials_)
725 return true;
727 return base::android::BuildInfo::GetInstance()->sdk_int() >=
728 kSDKVersionToSupportSecurityOriginCheck;
731 bool WebMediaPlayerAndroid::didPassCORSAccessCheck() const {
732 DCHECK(main_thread_checker_.CalledOnValidThread());
733 if (info_loader_)
734 return info_loader_->DidPassCORSAccessCheck();
735 return false;
738 double WebMediaPlayerAndroid::mediaTimeForTimeValue(double timeValue) const {
739 return base::TimeDelta::FromSecondsD(timeValue).InSecondsF();
742 unsigned WebMediaPlayerAndroid::decodedFrameCount() const {
743 if (media_source_delegate_)
744 return media_source_delegate_->DecodedFrameCount();
745 NOTIMPLEMENTED();
746 return 0;
749 unsigned WebMediaPlayerAndroid::droppedFrameCount() const {
750 if (media_source_delegate_)
751 return media_source_delegate_->DroppedFrameCount();
752 NOTIMPLEMENTED();
753 return 0;
756 unsigned WebMediaPlayerAndroid::audioDecodedByteCount() const {
757 if (media_source_delegate_)
758 return media_source_delegate_->AudioDecodedByteCount();
759 NOTIMPLEMENTED();
760 return 0;
763 unsigned WebMediaPlayerAndroid::videoDecodedByteCount() const {
764 if (media_source_delegate_)
765 return media_source_delegate_->VideoDecodedByteCount();
766 NOTIMPLEMENTED();
767 return 0;
770 void WebMediaPlayerAndroid::OnMediaMetadataChanged(
771 base::TimeDelta duration, int width, int height, bool success) {
772 DCHECK(main_thread_checker_.CalledOnValidThread());
773 bool need_to_signal_duration_changed = false;
775 if (is_local_resource_)
776 UpdateNetworkState(WebMediaPlayer::NetworkStateLoaded);
778 // For HLS streams, the reported duration may be zero for infinite streams.
779 // See http://crbug.com/501213.
780 if (duration.is_zero() && IsHLSStream())
781 duration = media::kInfiniteDuration();
783 // Update duration, if necessary, prior to ready state updates that may
784 // cause duration() query.
785 if (!ignore_metadata_duration_change_ && duration_ != duration) {
786 duration_ = duration;
787 if (is_local_resource_)
788 buffered_[0].end = duration_.InSecondsF();
789 // Client readyState transition from HAVE_NOTHING to HAVE_METADATA
790 // already triggers a durationchanged event. If this is a different
791 // transition, remember to signal durationchanged.
792 // Do not ever signal durationchanged on metadata change in MSE case
793 // because OnDurationChanged() handles this.
794 if (ready_state_ > WebMediaPlayer::ReadyStateHaveNothing &&
795 player_type_ != MEDIA_PLAYER_TYPE_MEDIA_SOURCE) {
796 need_to_signal_duration_changed = true;
800 if (ready_state_ != WebMediaPlayer::ReadyStateHaveEnoughData) {
801 UpdateReadyState(WebMediaPlayer::ReadyStateHaveMetadata);
802 UpdateReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
805 // TODO(wolenetz): Should we just abort early and set network state to an
806 // error if success == false? See http://crbug.com/248399
807 if (success)
808 OnVideoSizeChanged(width, height);
810 if (need_to_signal_duration_changed)
811 client_->durationChanged();
814 void WebMediaPlayerAndroid::OnPlaybackComplete() {
815 // When playback is about to finish, android media player often stops
816 // at a time which is smaller than the duration. This makes webkit never
817 // know that the playback has finished. To solve this, we set the
818 // current time to media duration when OnPlaybackComplete() get called.
819 interpolator_.SetBounds(duration_, duration_);
820 client_->timeChanged();
822 // If the loop attribute is set, timeChanged() will update the current time
823 // to 0. It will perform a seek to 0. Issue a command to the player to start
824 // playing after seek completes.
825 if (seeking_ && seek_time_ == base::TimeDelta())
826 player_manager_->Start(player_id_);
829 void WebMediaPlayerAndroid::OnBufferingUpdate(int percentage) {
830 buffered_[0].end = duration() * percentage / 100;
831 did_loading_progress_ = true;
833 if (percentage == 100 && network_state_ < WebMediaPlayer::NetworkStateLoaded)
834 UpdateNetworkState(WebMediaPlayer::NetworkStateLoaded);
837 void WebMediaPlayerAndroid::OnSeekRequest(const base::TimeDelta& time_to_seek) {
838 DCHECK(main_thread_checker_.CalledOnValidThread());
839 client_->requestSeek(time_to_seek.InSecondsF());
842 void WebMediaPlayerAndroid::OnSeekComplete(
843 const base::TimeDelta& current_time) {
844 DCHECK(main_thread_checker_.CalledOnValidThread());
845 seeking_ = false;
846 if (pending_seek_) {
847 pending_seek_ = false;
848 seek(pending_seek_time_.InSecondsF());
849 return;
851 interpolator_.SetBounds(current_time, current_time);
853 UpdateReadyState(WebMediaPlayer::ReadyStateHaveEnoughData);
855 client_->timeChanged();
858 void WebMediaPlayerAndroid::OnMediaError(int error_type) {
859 switch (error_type) {
860 case MediaPlayerAndroid::MEDIA_ERROR_FORMAT:
861 UpdateNetworkState(WebMediaPlayer::NetworkStateFormatError);
862 break;
863 case MediaPlayerAndroid::MEDIA_ERROR_DECODE:
864 UpdateNetworkState(WebMediaPlayer::NetworkStateDecodeError);
865 break;
866 case MediaPlayerAndroid::MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
867 UpdateNetworkState(WebMediaPlayer::NetworkStateFormatError);
868 break;
869 case MediaPlayerAndroid::MEDIA_ERROR_INVALID_CODE:
870 break;
872 client_->repaint();
875 void WebMediaPlayerAndroid::OnVideoSizeChanged(int width, int height) {
876 DCHECK(main_thread_checker_.CalledOnValidThread());
878 // For HLS streams, a bogus empty size may be reported at first, followed by
879 // the actual size only once playback begins. See http://crbug.com/509972.
880 if (!has_size_info_ && width == 0 && height == 0 && IsHLSStream())
881 return;
883 has_size_info_ = true;
884 if (natural_size_.width == width && natural_size_.height == height)
885 return;
887 #if defined(VIDEO_HOLE)
888 // Use H/W surface for encrypted video.
889 // TODO(qinmin): Change this so that only EME needs the H/W surface
890 if (force_use_overlay_embedded_video_ ||
891 (media_source_delegate_ && media_source_delegate_->IsVideoEncrypted() &&
892 player_manager_->ShouldUseVideoOverlayForEmbeddedEncryptedVideo())) {
893 needs_external_surface_ = true;
894 if (!paused() && !is_fullscreen_)
895 player_manager_->RequestExternalSurface(player_id_, last_computed_rect_);
896 } else if (!stream_texture_proxy_) {
897 // Do deferred stream texture creation finally.
898 SetNeedsEstablishPeer(true);
899 TryCreateStreamTextureProxyIfNeeded();
901 #endif // defined(VIDEO_HOLE)
902 natural_size_.width = width;
903 natural_size_.height = height;
905 // When play() gets called, |natural_size_| may still be empty and
906 // EstablishSurfaceTexturePeer() will not get called. As a result, the video
907 // may play without a surface texture. When we finally get the valid video
908 // size here, we should call EstablishSurfaceTexturePeer() if it has not been
909 // previously called.
910 if (!paused() && needs_establish_peer_)
911 EstablishSurfaceTexturePeer();
913 ReallocateVideoFrame();
915 // For hidden video element (with style "display:none"), ensure the texture
916 // size is set.
917 if (!is_remote_ && cached_stream_texture_size_ != natural_size_) {
918 stream_texture_factory_->SetStreamTextureSize(
919 stream_id_, gfx::Size(natural_size_.width, natural_size_.height));
920 cached_stream_texture_size_ = natural_size_;
923 // Lazily allocate compositing layer.
924 if (!video_weblayer_) {
925 video_weblayer_.reset(new cc_blink::WebLayerImpl(
926 cc::VideoLayer::Create(cc_blink::WebLayerImpl::LayerSettings(), this,
927 media::VIDEO_ROTATION_0)));
928 client_->setWebLayer(video_weblayer_.get());
932 void WebMediaPlayerAndroid::OnTimeUpdate(base::TimeDelta current_timestamp,
933 base::TimeTicks current_time_ticks) {
934 DCHECK(main_thread_checker_.CalledOnValidThread());
936 if (seeking())
937 return;
939 // Compensate the current_timestamp with the IPC latency.
940 base::TimeDelta lower_bound =
941 base::TimeTicks::Now() - current_time_ticks + current_timestamp;
942 base::TimeDelta upper_bound = lower_bound;
943 // We should get another time update in about |kTimeUpdateInterval|
944 // milliseconds.
945 if (is_playing_) {
946 upper_bound += base::TimeDelta::FromMilliseconds(
947 media::kTimeUpdateInterval);
949 // if the lower_bound is smaller than the current time, just use the current
950 // time so that the timer is always progressing.
951 lower_bound =
952 std::max(lower_bound, base::TimeDelta::FromSecondsD(currentTime()));
953 if (lower_bound > upper_bound)
954 upper_bound = lower_bound;
955 interpolator_.SetBounds(lower_bound, upper_bound);
958 void WebMediaPlayerAndroid::OnConnectedToRemoteDevice(
959 const std::string& remote_playback_message) {
960 DCHECK(main_thread_checker_.CalledOnValidThread());
961 DCHECK(!media_source_delegate_);
962 DrawRemotePlaybackText(remote_playback_message);
963 is_remote_ = true;
964 SetNeedsEstablishPeer(false);
965 client_->connectedToRemoteDevice();
968 void WebMediaPlayerAndroid::OnDisconnectedFromRemoteDevice() {
969 DCHECK(main_thread_checker_.CalledOnValidThread());
970 DCHECK(!media_source_delegate_);
971 SetNeedsEstablishPeer(true);
972 if (!paused())
973 EstablishSurfaceTexturePeer();
974 is_remote_ = false;
975 ReallocateVideoFrame();
976 client_->disconnectedFromRemoteDevice();
979 void WebMediaPlayerAndroid::OnDidExitFullscreen() {
980 // |needs_external_surface_| is always false on non-TV devices.
981 if (!needs_external_surface_)
982 SetNeedsEstablishPeer(true);
983 // We had the fullscreen surface connected to Android MediaPlayer,
984 // so reconnect our surface texture for embedded playback.
985 if (!paused() && needs_establish_peer_) {
986 TryCreateStreamTextureProxyIfNeeded();
987 EstablishSurfaceTexturePeer();
990 #if defined(VIDEO_HOLE)
991 if (!paused() && needs_external_surface_)
992 player_manager_->RequestExternalSurface(player_id_, last_computed_rect_);
993 #endif // defined(VIDEO_HOLE)
994 is_fullscreen_ = false;
995 client_->repaint();
998 void WebMediaPlayerAndroid::OnMediaPlayerPlay() {
999 UpdatePlayingState(true);
1000 client_->playbackStateChanged();
1003 void WebMediaPlayerAndroid::OnMediaPlayerPause() {
1004 UpdatePlayingState(false);
1005 client_->playbackStateChanged();
1008 void WebMediaPlayerAndroid::OnRemoteRouteAvailabilityChanged(
1009 bool routes_available) {
1010 client_->remoteRouteAvailabilityChanged(routes_available);
1013 void WebMediaPlayerAndroid::OnDurationChanged(const base::TimeDelta& duration) {
1014 DCHECK(main_thread_checker_.CalledOnValidThread());
1015 // Only MSE |player_type_| registers this callback.
1016 DCHECK_EQ(player_type_, MEDIA_PLAYER_TYPE_MEDIA_SOURCE);
1018 // Cache the new duration value and trust it over any subsequent duration
1019 // values received in OnMediaMetadataChanged().
1020 duration_ = duration;
1021 ignore_metadata_duration_change_ = true;
1023 // Notify MediaPlayerClient that duration has changed, if > HAVE_NOTHING.
1024 if (ready_state_ > WebMediaPlayer::ReadyStateHaveNothing)
1025 client_->durationChanged();
1028 void WebMediaPlayerAndroid::UpdateNetworkState(
1029 WebMediaPlayer::NetworkState state) {
1030 DCHECK(main_thread_checker_.CalledOnValidThread());
1031 if (ready_state_ == WebMediaPlayer::ReadyStateHaveNothing &&
1032 (state == WebMediaPlayer::NetworkStateNetworkError ||
1033 state == WebMediaPlayer::NetworkStateDecodeError)) {
1034 // Any error that occurs before reaching ReadyStateHaveMetadata should
1035 // be considered a format error.
1036 network_state_ = WebMediaPlayer::NetworkStateFormatError;
1037 } else {
1038 network_state_ = state;
1040 client_->networkStateChanged();
1043 void WebMediaPlayerAndroid::UpdateReadyState(
1044 WebMediaPlayer::ReadyState state) {
1045 ready_state_ = state;
1046 client_->readyStateChanged();
1049 void WebMediaPlayerAndroid::OnPlayerReleased() {
1050 // |needs_external_surface_| is always false on non-TV devices.
1051 if (!needs_external_surface_)
1052 needs_establish_peer_ = true;
1054 if (is_playing_)
1055 OnMediaPlayerPause();
1057 #if defined(VIDEO_HOLE)
1058 last_computed_rect_ = gfx::RectF();
1059 #endif // defined(VIDEO_HOLE)
1062 void WebMediaPlayerAndroid::ReleaseMediaResources() {
1063 switch (network_state_) {
1064 // Pause the media player and inform WebKit if the player is in a good
1065 // shape.
1066 case WebMediaPlayer::NetworkStateIdle:
1067 case WebMediaPlayer::NetworkStateLoading:
1068 case WebMediaPlayer::NetworkStateLoaded:
1069 Pause(false);
1070 client_->playbackStateChanged();
1071 break;
1072 // If a WebMediaPlayer instance has entered into one of these states,
1073 // the internal network state in HTMLMediaElement could be set to empty.
1074 // And calling playbackStateChanged() could get this object deleted.
1075 case WebMediaPlayer::NetworkStateEmpty:
1076 case WebMediaPlayer::NetworkStateFormatError:
1077 case WebMediaPlayer::NetworkStateNetworkError:
1078 case WebMediaPlayer::NetworkStateDecodeError:
1079 break;
1081 player_manager_->ReleaseResources(player_id_);
1082 if (!needs_external_surface_)
1083 SetNeedsEstablishPeer(true);
1086 void WebMediaPlayerAndroid::OnDestruct() {
1087 NOTREACHED() << "WebMediaPlayer should be destroyed before any "
1088 "RenderFrameObserver::OnDestruct() gets called when "
1089 "the RenderFrame goes away.";
1092 void WebMediaPlayerAndroid::InitializePlayer(
1093 const GURL& url,
1094 const GURL& first_party_for_cookies,
1095 bool allow_stored_credentials,
1096 int demuxer_client_id) {
1097 ReportHLSMetrics();
1099 allow_stored_credentials_ = allow_stored_credentials;
1100 player_manager_->Initialize(
1101 player_type_, player_id_, url, first_party_for_cookies, demuxer_client_id,
1102 frame_->document().url(), allow_stored_credentials);
1103 is_player_initialized_ = true;
1105 if (is_fullscreen_)
1106 player_manager_->EnterFullscreen(player_id_);
1108 if (cdm_context_)
1109 SetCdmInternal(base::Bind(&media::IgnoreCdmAttached));
1112 void WebMediaPlayerAndroid::Pause(bool is_media_related_action) {
1113 player_manager_->Pause(player_id_, is_media_related_action);
1114 UpdatePlayingState(false);
1117 void WebMediaPlayerAndroid::DrawRemotePlaybackText(
1118 const std::string& remote_playback_message) {
1119 DCHECK(main_thread_checker_.CalledOnValidThread());
1120 if (!video_weblayer_)
1121 return;
1123 // TODO(johnme): Should redraw this frame if the layer bounds change; but
1124 // there seems no easy way to listen for the layer resizing (as opposed to
1125 // OnVideoSizeChanged, which is when the frame sizes of the video file
1126 // change). Perhaps have to poll (on main thread of course)?
1127 gfx::Size video_size_css_px = video_weblayer_->bounds();
1128 float device_scale_factor = frame_->view()->deviceScaleFactor();
1129 // canvas_size will be the size in device pixels when pageScaleFactor == 1
1130 gfx::Size canvas_size(
1131 static_cast<int>(video_size_css_px.width() * device_scale_factor),
1132 static_cast<int>(video_size_css_px.height() * device_scale_factor));
1134 SkBitmap bitmap;
1135 bitmap.allocN32Pixels(canvas_size.width(), canvas_size.height());
1137 // Create the canvas and draw the "Casting to <Chromecast>" text on it.
1138 SkCanvas canvas(bitmap);
1139 canvas.drawColor(SK_ColorBLACK);
1141 const SkScalar kTextSize(40);
1142 const SkScalar kMinPadding(40);
1144 SkPaint paint;
1145 paint.setAntiAlias(true);
1146 paint.setFilterQuality(kHigh_SkFilterQuality);
1147 paint.setColor(SK_ColorWHITE);
1148 paint.setTypeface(SkTypeface::CreateFromName("sans", SkTypeface::kBold));
1149 paint.setTextSize(kTextSize);
1151 // Calculate the vertical margin from the top
1152 SkPaint::FontMetrics font_metrics;
1153 paint.getFontMetrics(&font_metrics);
1154 SkScalar sk_vertical_margin = kMinPadding - font_metrics.fAscent;
1156 // Measure the width of the entire text to display
1157 size_t display_text_width = paint.measureText(
1158 remote_playback_message.c_str(), remote_playback_message.size());
1159 std::string display_text(remote_playback_message);
1161 if (display_text_width + (kMinPadding * 2) > canvas_size.width()) {
1162 // The text is too long to fit in one line, truncate it and append ellipsis
1163 // to the end.
1165 // First, figure out how much of the canvas the '...' will take up.
1166 const std::string kTruncationEllipsis("\xE2\x80\xA6");
1167 SkScalar sk_ellipse_width = paint.measureText(
1168 kTruncationEllipsis.c_str(), kTruncationEllipsis.size());
1170 // Then calculate how much of the text can be drawn with the '...' appended
1171 // to the end of the string.
1172 SkScalar sk_max_original_text_width(
1173 canvas_size.width() - (kMinPadding * 2) - sk_ellipse_width);
1174 size_t sk_max_original_text_length = paint.breakText(
1175 remote_playback_message.c_str(),
1176 remote_playback_message.size(),
1177 sk_max_original_text_width);
1179 // Remove the part of the string that doesn't fit and append '...'.
1180 display_text.erase(sk_max_original_text_length,
1181 remote_playback_message.size() - sk_max_original_text_length);
1182 display_text.append(kTruncationEllipsis);
1183 display_text_width = paint.measureText(
1184 display_text.c_str(), display_text.size());
1187 // Center the text horizontally.
1188 SkScalar sk_horizontal_margin =
1189 (canvas_size.width() - display_text_width) / 2.0;
1190 canvas.drawText(display_text.c_str(),
1191 display_text.size(),
1192 sk_horizontal_margin,
1193 sk_vertical_margin,
1194 paint);
1196 GLES2Interface* gl = stream_texture_factory_->ContextGL();
1197 GLuint remote_playback_texture_id = 0;
1198 gl->GenTextures(1, &remote_playback_texture_id);
1199 GLuint texture_target = GL_TEXTURE_2D;
1200 gl->BindTexture(texture_target, remote_playback_texture_id);
1201 gl->TexParameteri(texture_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1202 gl->TexParameteri(texture_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1203 gl->TexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1204 gl->TexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1207 SkAutoLockPixels lock(bitmap);
1208 gl->TexImage2D(texture_target,
1209 0 /* level */,
1210 GL_RGBA /* internalformat */,
1211 bitmap.width(),
1212 bitmap.height(),
1213 0 /* border */,
1214 GL_RGBA /* format */,
1215 GL_UNSIGNED_BYTE /* type */,
1216 bitmap.getPixels());
1219 gpu::Mailbox texture_mailbox;
1220 gl->GenMailboxCHROMIUM(texture_mailbox.name);
1221 gl->ProduceTextureCHROMIUM(texture_target, texture_mailbox.name);
1222 gl->Flush();
1223 GLuint texture_mailbox_sync_point = gl->InsertSyncPointCHROMIUM();
1225 scoped_refptr<VideoFrame> new_frame = VideoFrame::WrapNativeTexture(
1226 media::PIXEL_FORMAT_ARGB,
1227 gpu::MailboxHolder(texture_mailbox, texture_target,
1228 texture_mailbox_sync_point),
1229 media::BindToCurrentLoop(base::Bind(&OnReleaseTexture,
1230 stream_texture_factory_,
1231 remote_playback_texture_id)),
1232 canvas_size /* coded_size */, gfx::Rect(canvas_size) /* visible_rect */,
1233 canvas_size /* natural_size */, base::TimeDelta() /* timestamp */);
1234 SetCurrentFrameInternal(new_frame);
1237 void WebMediaPlayerAndroid::ReallocateVideoFrame() {
1238 DCHECK(main_thread_checker_.CalledOnValidThread());
1239 if (needs_external_surface_) {
1240 // VideoFrame::CreateHoleFrame is only defined under VIDEO_HOLE.
1241 #if defined(VIDEO_HOLE)
1242 if (!natural_size_.isEmpty()) {
1243 // Now we finally know that "stream texture" and "video frame" won't
1244 // be needed. EME uses "external surface" and "video hole" instead.
1245 RemoveSurfaceTextureAndProxy();
1246 scoped_refptr<VideoFrame> new_frame =
1247 VideoFrame::CreateHoleFrame(natural_size_);
1248 SetCurrentFrameInternal(new_frame);
1249 // Force the client to grab the hole frame.
1250 client_->repaint();
1252 #else
1253 NOTIMPLEMENTED() << "Hole punching not supported without VIDEO_HOLE flag";
1254 #endif // defined(VIDEO_HOLE)
1255 } else if (!is_remote_ && texture_id_) {
1256 GLES2Interface* gl = stream_texture_factory_->ContextGL();
1257 GLuint texture_target = kGLTextureExternalOES;
1258 GLuint texture_id_ref = gl->CreateAndConsumeTextureCHROMIUM(
1259 texture_target, texture_mailbox_.name);
1260 gl->Flush();
1261 GLuint texture_mailbox_sync_point = gl->InsertSyncPointCHROMIUM();
1263 scoped_refptr<VideoFrame> new_frame = VideoFrame::WrapNativeTexture(
1264 media::PIXEL_FORMAT_ARGB,
1265 gpu::MailboxHolder(texture_mailbox_, texture_target,
1266 texture_mailbox_sync_point),
1267 media::BindToCurrentLoop(base::Bind(
1268 &OnReleaseTexture, stream_texture_factory_, texture_id_ref)),
1269 natural_size_, gfx::Rect(natural_size_), natural_size_,
1270 base::TimeDelta());
1271 SetCurrentFrameInternal(new_frame);
1275 void WebMediaPlayerAndroid::SetVideoFrameProviderClient(
1276 cc::VideoFrameProvider::Client* client) {
1277 // This is called from both the main renderer thread and the compositor
1278 // thread (when the main thread is blocked).
1280 // Set the callback target when a frame is produced. Need to do this before
1281 // StopUsingProvider to ensure we really stop using the client.
1282 if (stream_texture_proxy_) {
1283 stream_texture_proxy_->BindToLoop(stream_id_, client,
1284 compositor_task_runner_);
1287 if (video_frame_provider_client_ && video_frame_provider_client_ != client)
1288 video_frame_provider_client_->StopUsingProvider();
1289 video_frame_provider_client_ = client;
1292 void WebMediaPlayerAndroid::SetCurrentFrameInternal(
1293 scoped_refptr<media::VideoFrame>& video_frame) {
1294 DCHECK(main_thread_checker_.CalledOnValidThread());
1295 base::AutoLock auto_lock(current_frame_lock_);
1296 current_frame_ = video_frame;
1299 bool WebMediaPlayerAndroid::UpdateCurrentFrame(base::TimeTicks deadline_min,
1300 base::TimeTicks deadline_max) {
1301 NOTIMPLEMENTED();
1302 return false;
1305 bool WebMediaPlayerAndroid::HasCurrentFrame() {
1306 base::AutoLock auto_lock(current_frame_lock_);
1307 return current_frame_;
1310 scoped_refptr<media::VideoFrame> WebMediaPlayerAndroid::GetCurrentFrame() {
1311 scoped_refptr<VideoFrame> video_frame;
1313 base::AutoLock auto_lock(current_frame_lock_);
1314 video_frame = current_frame_;
1317 return video_frame;
1320 void WebMediaPlayerAndroid::PutCurrentFrame() {
1323 void WebMediaPlayerAndroid::ResetStreamTextureProxy() {
1324 DCHECK(main_thread_checker_.CalledOnValidThread());
1326 RemoveSurfaceTextureAndProxy();
1328 TryCreateStreamTextureProxyIfNeeded();
1329 if (needs_establish_peer_ && is_playing_)
1330 EstablishSurfaceTexturePeer();
1333 void WebMediaPlayerAndroid::RemoveSurfaceTextureAndProxy() {
1334 DCHECK(main_thread_checker_.CalledOnValidThread());
1336 if (stream_id_) {
1337 GLES2Interface* gl = stream_texture_factory_->ContextGL();
1338 gl->DeleteTextures(1, &texture_id_);
1339 // Flush to ensure that the stream texture gets deleted in a timely fashion.
1340 gl->ShallowFlushCHROMIUM();
1341 texture_id_ = 0;
1342 texture_mailbox_ = gpu::Mailbox();
1343 stream_id_ = 0;
1345 stream_texture_proxy_.reset();
1346 needs_establish_peer_ = !needs_external_surface_ && !is_remote_ &&
1347 !is_fullscreen_ &&
1348 (hasVideo() || IsHLSStream());
1351 void WebMediaPlayerAndroid::TryCreateStreamTextureProxyIfNeeded() {
1352 DCHECK(main_thread_checker_.CalledOnValidThread());
1353 // Already created.
1354 if (stream_texture_proxy_)
1355 return;
1357 // No factory to create proxy.
1358 if (!stream_texture_factory_.get())
1359 return;
1361 // Not needed for hole punching.
1362 if (!needs_establish_peer_)
1363 return;
1365 stream_texture_proxy_.reset(stream_texture_factory_->CreateProxy());
1366 if (stream_texture_proxy_) {
1367 DoCreateStreamTexture();
1368 ReallocateVideoFrame();
1369 if (video_frame_provider_client_) {
1370 stream_texture_proxy_->BindToLoop(
1371 stream_id_, video_frame_provider_client_, compositor_task_runner_);
1376 void WebMediaPlayerAndroid::EstablishSurfaceTexturePeer() {
1377 DCHECK(main_thread_checker_.CalledOnValidThread());
1378 if (!stream_texture_proxy_)
1379 return;
1381 if (stream_texture_factory_.get() && stream_id_)
1382 stream_texture_factory_->EstablishPeer(stream_id_, player_id_);
1384 // Set the deferred size because the size was changed in remote mode.
1385 if (!is_remote_ && cached_stream_texture_size_ != natural_size_) {
1386 stream_texture_factory_->SetStreamTextureSize(
1387 stream_id_, gfx::Size(natural_size_.width, natural_size_.height));
1388 cached_stream_texture_size_ = natural_size_;
1391 needs_establish_peer_ = false;
1394 void WebMediaPlayerAndroid::DoCreateStreamTexture() {
1395 DCHECK(main_thread_checker_.CalledOnValidThread());
1396 DCHECK(!stream_id_);
1397 DCHECK(!texture_id_);
1398 stream_id_ = stream_texture_factory_->CreateStreamTexture(
1399 kGLTextureExternalOES, &texture_id_, &texture_mailbox_);
1402 void WebMediaPlayerAndroid::SetNeedsEstablishPeer(bool needs_establish_peer) {
1403 needs_establish_peer_ = needs_establish_peer;
1406 void WebMediaPlayerAndroid::setPoster(const blink::WebURL& poster) {
1407 player_manager_->SetPoster(player_id_, poster);
1410 void WebMediaPlayerAndroid::UpdatePlayingState(bool is_playing) {
1411 if (is_playing == is_playing_)
1412 return;
1414 is_playing_ = is_playing;
1416 if (is_playing)
1417 interpolator_.StartInterpolating();
1418 else
1419 interpolator_.StopInterpolating();
1421 if (delegate_) {
1422 if (is_playing)
1423 delegate_->DidPlay(this);
1424 else
1425 delegate_->DidPause(this);
1429 #if defined(VIDEO_HOLE)
1430 bool WebMediaPlayerAndroid::UpdateBoundaryRectangle() {
1431 if (!video_weblayer_)
1432 return false;
1434 // Compute the geometry of video frame layer.
1435 cc::Layer* layer = video_weblayer_->layer();
1436 gfx::RectF rect(layer->bounds());
1437 while (layer) {
1438 rect.Offset(layer->position().OffsetFromOrigin());
1439 layer = layer->parent();
1442 // Return false when the geometry hasn't been changed from the last time.
1443 if (last_computed_rect_ == rect)
1444 return false;
1446 // Store the changed geometry information when it is actually changed.
1447 last_computed_rect_ = rect;
1448 return true;
1451 const gfx::RectF WebMediaPlayerAndroid::GetBoundaryRectangle() {
1452 return last_computed_rect_;
1454 #endif
1456 // The following EME related code is copied from WebMediaPlayerImpl.
1457 // TODO(xhwang): Remove duplicate code between WebMediaPlayerAndroid and
1458 // WebMediaPlayerImpl.
1460 // Convert a WebString to ASCII, falling back on an empty string in the case
1461 // of a non-ASCII string.
1462 static std::string ToASCIIOrEmpty(const blink::WebString& string) {
1463 return base::IsStringASCII(string)
1464 ? base::UTF16ToASCII(base::StringPiece16(string))
1465 : std::string();
1468 // Helper functions to report media EME related stats to UMA. They follow the
1469 // convention of more commonly used macros UMA_HISTOGRAM_ENUMERATION and
1470 // UMA_HISTOGRAM_COUNTS. The reason that we cannot use those macros directly is
1471 // that UMA_* macros require the names to be constant throughout the process'
1472 // lifetime.
1474 static void EmeUMAHistogramEnumeration(const std::string& key_system,
1475 const std::string& method,
1476 int sample,
1477 int boundary_value) {
1478 base::LinearHistogram::FactoryGet(
1479 kMediaEme + media::GetKeySystemNameForUMA(key_system) + "." + method,
1480 1, boundary_value, boundary_value + 1,
1481 base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
1484 static void EmeUMAHistogramCounts(const std::string& key_system,
1485 const std::string& method,
1486 int sample) {
1487 // Use the same parameters as UMA_HISTOGRAM_COUNTS.
1488 base::Histogram::FactoryGet(
1489 kMediaEme + media::GetKeySystemNameForUMA(key_system) + "." + method,
1490 1, 1000000, 50, base::Histogram::kUmaTargetedHistogramFlag)->Add(sample);
1493 // Helper enum for reporting generateKeyRequest/addKey histograms.
1494 enum MediaKeyException {
1495 kUnknownResultId,
1496 kSuccess,
1497 kKeySystemNotSupported,
1498 kInvalidPlayerState,
1499 kMaxMediaKeyException
1502 static MediaKeyException MediaKeyExceptionForUMA(
1503 WebMediaPlayer::MediaKeyException e) {
1504 switch (e) {
1505 case WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported:
1506 return kKeySystemNotSupported;
1507 case WebMediaPlayer::MediaKeyExceptionInvalidPlayerState:
1508 return kInvalidPlayerState;
1509 case WebMediaPlayer::MediaKeyExceptionNoError:
1510 return kSuccess;
1511 default:
1512 return kUnknownResultId;
1516 // Helper for converting |key_system| name and exception |e| to a pair of enum
1517 // values from above, for reporting to UMA.
1518 static void ReportMediaKeyExceptionToUMA(const std::string& method,
1519 const std::string& key_system,
1520 WebMediaPlayer::MediaKeyException e) {
1521 MediaKeyException result_id = MediaKeyExceptionForUMA(e);
1522 DCHECK_NE(result_id, kUnknownResultId) << e;
1523 EmeUMAHistogramEnumeration(
1524 key_system, method, result_id, kMaxMediaKeyException);
1527 bool WebMediaPlayerAndroid::IsKeySystemSupported(
1528 const std::string& key_system) {
1529 // On Android, EME only works with MSE.
1530 return player_type_ == MEDIA_PLAYER_TYPE_MEDIA_SOURCE &&
1531 media::PrefixedIsSupportedConcreteKeySystem(key_system);
1534 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::generateKeyRequest(
1535 const WebString& key_system,
1536 const unsigned char* init_data,
1537 unsigned init_data_length) {
1538 DVLOG(1) << "generateKeyRequest: " << base::string16(key_system) << ": "
1539 << std::string(reinterpret_cast<const char*>(init_data),
1540 static_cast<size_t>(init_data_length));
1542 std::string ascii_key_system =
1543 media::GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
1545 WebMediaPlayer::MediaKeyException e =
1546 GenerateKeyRequestInternal(ascii_key_system, init_data, init_data_length);
1547 ReportMediaKeyExceptionToUMA("generateKeyRequest", ascii_key_system, e);
1548 return e;
1551 // Guess the type of |init_data|. This is only used to handle some corner cases
1552 // so we keep it as simple as possible without breaking major use cases.
1553 static media::EmeInitDataType GuessInitDataType(const unsigned char* init_data,
1554 unsigned init_data_length) {
1555 // Most WebM files use KeyId of 16 bytes. CENC init data is always >16 bytes.
1556 if (init_data_length == 16)
1557 return media::EmeInitDataType::WEBM;
1559 return media::EmeInitDataType::CENC;
1562 // TODO(xhwang): Report an error when there is encrypted stream but EME is
1563 // not enabled. Currently the player just doesn't start and waits for
1564 // ever.
1565 WebMediaPlayer::MediaKeyException
1566 WebMediaPlayerAndroid::GenerateKeyRequestInternal(
1567 const std::string& key_system,
1568 const unsigned char* init_data,
1569 unsigned init_data_length) {
1570 if (!IsKeySystemSupported(key_system))
1571 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
1573 if (!proxy_decryptor_) {
1574 DCHECK(current_key_system_.empty());
1575 proxy_decryptor_.reset(new media::ProxyDecryptor(
1576 media_permission_,
1577 player_manager_->ShouldUseVideoOverlayForEmbeddedEncryptedVideo(),
1578 base::Bind(&WebMediaPlayerAndroid::OnKeyAdded,
1579 weak_factory_.GetWeakPtr()),
1580 base::Bind(&WebMediaPlayerAndroid::OnKeyError,
1581 weak_factory_.GetWeakPtr()),
1582 base::Bind(&WebMediaPlayerAndroid::OnKeyMessage,
1583 weak_factory_.GetWeakPtr())));
1585 GURL security_origin(frame_->document().securityOrigin().toString());
1586 proxy_decryptor_->CreateCdm(
1587 cdm_factory_, key_system, security_origin,
1588 base::Bind(&WebMediaPlayerAndroid::OnCdmContextReady,
1589 weak_factory_.GetWeakPtr()));
1590 current_key_system_ = key_system;
1593 // We do not support run-time switching between key systems for now.
1594 DCHECK(!current_key_system_.empty());
1595 if (key_system != current_key_system_)
1596 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
1598 media::EmeInitDataType init_data_type = init_data_type_;
1599 if (init_data_type == media::EmeInitDataType::UNKNOWN)
1600 init_data_type = GuessInitDataType(init_data, init_data_length);
1602 proxy_decryptor_->GenerateKeyRequest(init_data_type, init_data,
1603 init_data_length);
1605 return WebMediaPlayer::MediaKeyExceptionNoError;
1608 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::addKey(
1609 const WebString& key_system,
1610 const unsigned char* key,
1611 unsigned key_length,
1612 const unsigned char* init_data,
1613 unsigned init_data_length,
1614 const WebString& session_id) {
1615 DVLOG(1) << "addKey: " << base::string16(key_system) << ": "
1616 << std::string(reinterpret_cast<const char*>(key),
1617 static_cast<size_t>(key_length)) << ", "
1618 << std::string(reinterpret_cast<const char*>(init_data),
1619 static_cast<size_t>(init_data_length)) << " ["
1620 << base::string16(session_id) << "]";
1622 std::string ascii_key_system =
1623 media::GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
1624 std::string ascii_session_id = ToASCIIOrEmpty(session_id);
1626 WebMediaPlayer::MediaKeyException e = AddKeyInternal(ascii_key_system,
1627 key,
1628 key_length,
1629 init_data,
1630 init_data_length,
1631 ascii_session_id);
1632 ReportMediaKeyExceptionToUMA("addKey", ascii_key_system, e);
1633 return e;
1636 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::AddKeyInternal(
1637 const std::string& key_system,
1638 const unsigned char* key,
1639 unsigned key_length,
1640 const unsigned char* init_data,
1641 unsigned init_data_length,
1642 const std::string& session_id) {
1643 DCHECK(key);
1644 DCHECK_GT(key_length, 0u);
1646 if (!IsKeySystemSupported(key_system))
1647 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
1649 if (current_key_system_.empty() || key_system != current_key_system_)
1650 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
1652 proxy_decryptor_->AddKey(
1653 key, key_length, init_data, init_data_length, session_id);
1654 return WebMediaPlayer::MediaKeyExceptionNoError;
1657 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::cancelKeyRequest(
1658 const WebString& key_system,
1659 const WebString& session_id) {
1660 DVLOG(1) << "cancelKeyRequest: " << base::string16(key_system) << ": "
1661 << " [" << base::string16(session_id) << "]";
1663 std::string ascii_key_system =
1664 media::GetUnprefixedKeySystemName(ToASCIIOrEmpty(key_system));
1665 std::string ascii_session_id = ToASCIIOrEmpty(session_id);
1667 WebMediaPlayer::MediaKeyException e =
1668 CancelKeyRequestInternal(ascii_key_system, ascii_session_id);
1669 ReportMediaKeyExceptionToUMA("cancelKeyRequest", ascii_key_system, e);
1670 return e;
1673 WebMediaPlayer::MediaKeyException
1674 WebMediaPlayerAndroid::CancelKeyRequestInternal(const std::string& key_system,
1675 const std::string& session_id) {
1676 if (!IsKeySystemSupported(key_system))
1677 return WebMediaPlayer::MediaKeyExceptionKeySystemNotSupported;
1679 if (current_key_system_.empty() || key_system != current_key_system_)
1680 return WebMediaPlayer::MediaKeyExceptionInvalidPlayerState;
1682 proxy_decryptor_->CancelKeyRequest(session_id);
1683 return WebMediaPlayer::MediaKeyExceptionNoError;
1686 void WebMediaPlayerAndroid::setContentDecryptionModule(
1687 blink::WebContentDecryptionModule* cdm,
1688 blink::WebContentDecryptionModuleResult result) {
1689 DCHECK(main_thread_checker_.CalledOnValidThread());
1691 // Once the CDM is set it can't be cleared as there may be frames being
1692 // decrypted on other threads. So fail this request.
1693 // http://crbug.com/462365#c7.
1694 if (!cdm) {
1695 result.completeWithError(
1696 blink::WebContentDecryptionModuleExceptionInvalidStateError, 0,
1697 "The existing MediaKeys object cannot be removed at this time.");
1698 return;
1701 cdm_context_ = media::ToWebContentDecryptionModuleImpl(cdm)->GetCdmContext();
1703 if (is_player_initialized_) {
1704 SetCdmInternal(media::BindToCurrentLoop(
1705 base::Bind(&WebMediaPlayerAndroid::ContentDecryptionModuleAttached,
1706 weak_factory_.GetWeakPtr(), result)));
1707 } else {
1708 // No pipeline/decoder connected, so resolve the promise. When something
1709 // is connected, setting the CDM will happen in SetDecryptorReadyCB().
1710 ContentDecryptionModuleAttached(result, true);
1714 void WebMediaPlayerAndroid::ContentDecryptionModuleAttached(
1715 blink::WebContentDecryptionModuleResult result,
1716 bool success) {
1717 if (success) {
1718 result.complete();
1719 return;
1722 result.completeWithError(
1723 blink::WebContentDecryptionModuleExceptionNotSupportedError,
1725 "Unable to set MediaKeys object");
1728 void WebMediaPlayerAndroid::OnKeyAdded(const std::string& session_id) {
1729 EmeUMAHistogramCounts(current_key_system_, "KeyAdded", 1);
1731 encrypted_client_->keyAdded(
1732 WebString::fromUTF8(media::GetPrefixedKeySystemName(current_key_system_)),
1733 WebString::fromUTF8(session_id));
1736 void WebMediaPlayerAndroid::OnKeyError(const std::string& session_id,
1737 media::MediaKeys::KeyError error_code,
1738 uint32 system_code) {
1739 EmeUMAHistogramEnumeration(current_key_system_, "KeyError",
1740 error_code, media::MediaKeys::kMaxKeyError);
1742 unsigned short short_system_code = 0;
1743 if (system_code > std::numeric_limits<unsigned short>::max()) {
1744 LOG(WARNING) << "system_code exceeds unsigned short limit.";
1745 short_system_code = std::numeric_limits<unsigned short>::max();
1746 } else {
1747 short_system_code = static_cast<unsigned short>(system_code);
1750 encrypted_client_->keyError(
1751 WebString::fromUTF8(media::GetPrefixedKeySystemName(current_key_system_)),
1752 WebString::fromUTF8(session_id),
1753 static_cast<blink::WebMediaPlayerEncryptedMediaClient::MediaKeyErrorCode>(
1754 error_code),
1755 short_system_code);
1758 void WebMediaPlayerAndroid::OnKeyMessage(const std::string& session_id,
1759 const std::vector<uint8>& message,
1760 const GURL& destination_url) {
1761 DCHECK(destination_url.is_empty() || destination_url.is_valid());
1763 encrypted_client_->keyMessage(
1764 WebString::fromUTF8(media::GetPrefixedKeySystemName(current_key_system_)),
1765 WebString::fromUTF8(session_id), message.empty() ? NULL : &message[0],
1766 message.size(), destination_url);
1769 void WebMediaPlayerAndroid::OnMediaSourceOpened(
1770 blink::WebMediaSource* web_media_source) {
1771 client_->mediaSourceOpened(web_media_source);
1774 void WebMediaPlayerAndroid::OnEncryptedMediaInitData(
1775 media::EmeInitDataType init_data_type,
1776 const std::vector<uint8>& init_data) {
1777 DCHECK(main_thread_checker_.CalledOnValidThread());
1779 // Do not fire NeedKey event if encrypted media is not enabled.
1780 if (!blink::WebRuntimeFeatures::isPrefixedEncryptedMediaEnabled() &&
1781 !blink::WebRuntimeFeatures::isEncryptedMediaEnabled()) {
1782 return;
1785 UMA_HISTOGRAM_COUNTS(kMediaEme + std::string("NeedKey"), 1);
1787 DCHECK(init_data_type != media::EmeInitDataType::UNKNOWN);
1788 DLOG_IF(WARNING, init_data_type_ != media::EmeInitDataType::UNKNOWN &&
1789 init_data_type != init_data_type_)
1790 << "Mixed init data type not supported. The new type is ignored.";
1791 if (init_data_type_ == media::EmeInitDataType::UNKNOWN)
1792 init_data_type_ = init_data_type;
1794 encrypted_client_->encrypted(ConvertToWebInitDataType(init_data_type),
1795 vector_as_array(&init_data), init_data.size());
1798 void WebMediaPlayerAndroid::OnWaitingForDecryptionKey() {
1799 encrypted_client_->didBlockPlaybackWaitingForKey();
1801 // TODO(jrummell): didResumePlaybackBlockedForKey() should only be called
1802 // when a key has been successfully added (e.g. OnSessionKeysChange() with
1803 // |has_additional_usable_key| = true). http://crbug.com/461903
1804 encrypted_client_->didResumePlaybackBlockedForKey();
1807 void WebMediaPlayerAndroid::OnCdmContextReady(media::CdmContext* cdm_context) {
1808 DCHECK(!cdm_context_);
1810 if (!cdm_context) {
1811 LOG(ERROR) << "CdmContext not available (e.g. CDM creation failed).";
1812 return;
1815 cdm_context_ = cdm_context;
1817 if (is_player_initialized_)
1818 SetCdmInternal(base::Bind(&media::IgnoreCdmAttached));
1821 void WebMediaPlayerAndroid::SetCdmInternal(
1822 const media::CdmAttachedCB& cdm_attached_cb) {
1823 DCHECK(cdm_context_ && is_player_initialized_);
1824 DCHECK(cdm_context_->GetDecryptor() ||
1825 cdm_context_->GetCdmId() != media::CdmContext::kInvalidCdmId)
1826 << "CDM should support either a Decryptor or a CDM ID.";
1828 media::Decryptor* decryptor = cdm_context_->GetDecryptor();
1830 // Note:
1831 // - If |decryptor| is non-null, only handles |decryptor_ready_cb_| and
1832 // ignores the CDM ID.
1833 // - If |decryptor| is null (in which case the CDM ID should be valid),
1834 // returns any pending |decryptor_ready_cb_| with null, so that
1835 // MediaSourceDelegate will fall back to use a browser side (IPC-based) CDM,
1836 // then calls SetCdm() through the |player_manager_|.
1838 if (decryptor) {
1839 if (!decryptor_ready_cb_.is_null()) {
1840 base::ResetAndReturn(&decryptor_ready_cb_)
1841 .Run(decryptor, cdm_attached_cb);
1842 } else {
1843 cdm_attached_cb.Run(true);
1845 return;
1848 // |decryptor| is null.
1849 if (!decryptor_ready_cb_.is_null()) {
1850 base::ResetAndReturn(&decryptor_ready_cb_)
1851 .Run(nullptr, base::Bind(&media::IgnoreCdmAttached));
1854 DCHECK(cdm_context_->GetCdmId() != media::CdmContext::kInvalidCdmId);
1855 player_manager_->SetCdm(player_id_, cdm_context_->GetCdmId());
1856 cdm_attached_cb.Run(true);
1859 void WebMediaPlayerAndroid::SetDecryptorReadyCB(
1860 const media::DecryptorReadyCB& decryptor_ready_cb) {
1861 DCHECK(main_thread_checker_.CalledOnValidThread());
1862 DCHECK(is_player_initialized_);
1864 // Cancels the previous decryptor request.
1865 if (decryptor_ready_cb.is_null()) {
1866 if (!decryptor_ready_cb_.is_null()) {
1867 base::ResetAndReturn(&decryptor_ready_cb_)
1868 .Run(NULL, base::Bind(&media::IgnoreCdmAttached));
1870 return;
1873 // TODO(xhwang): Support multiple decryptor notification request (e.g. from
1874 // video and audio). The current implementation is okay for the current
1875 // media pipeline since we initialize audio and video decoders in sequence.
1876 // But WebMediaPlayerImpl should not depend on media pipeline's implementation
1877 // detail.
1878 DCHECK(decryptor_ready_cb_.is_null());
1880 if (cdm_context_) {
1881 decryptor_ready_cb.Run(cdm_context_->GetDecryptor(),
1882 base::Bind(&media::IgnoreCdmAttached));
1883 return;
1886 decryptor_ready_cb_ = decryptor_ready_cb;
1889 bool WebMediaPlayerAndroid::supportsOverlayFullscreenVideo() {
1890 return true;
1893 void WebMediaPlayerAndroid::enterFullscreen() {
1894 if (is_player_initialized_)
1895 player_manager_->EnterFullscreen(player_id_);
1896 SetNeedsEstablishPeer(false);
1897 is_fullscreen_ = true;
1900 bool WebMediaPlayerAndroid::IsHLSStream() const {
1901 std::string mime;
1902 GURL url = redirected_url_.is_empty() ? url_ : redirected_url_;
1903 if (!net::GetMimeTypeFromFile(base::FilePath(url.path()), &mime))
1904 return false;
1905 return !mime.compare("application/x-mpegurl");
1908 void WebMediaPlayerAndroid::ReportHLSMetrics() const {
1909 if (player_type_ != MEDIA_PLAYER_TYPE_URL)
1910 return;
1912 bool is_hls = IsHLSStream();
1913 UMA_HISTOGRAM_BOOLEAN("Media.Android.IsHttpLiveStreamingMedia", is_hls);
1914 if (is_hls) {
1915 media::RecordOriginOfHLSPlayback(
1916 GURL(frame_->document().securityOrigin().toString()));
1920 } // namespace content