| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/media/android/webmediaplayer_android.h" | 5 #include "webkit/media/android/webmediaplayer_android.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "cc/layers/video_layer.h" | 11 #include "cc/layers/video_layer.h" |
| 12 #include "gpu/GLES2/gl2extchromium.h" | 12 #include "gpu/GLES2/gl2extchromium.h" |
| 13 #include "media/base/android/media_player_bridge.h" | 13 #include "media/base/android/media_player_bridge.h" |
| 14 #include "media/base/video_frame.h" | 14 #include "media/base/video_frame.h" |
| 15 #include "net/base/mime_util.h" | 15 #include "net/base/mime_util.h" |
| 16 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" | 16 #include "third_party/WebKit/Source/Platform/chromium/public/WebString.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayerClient.
h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayerClient.
h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaSource.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaSource.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 22 #include "webkit/compositor_bindings/web_layer_impl.h" | 22 #include "webkit/compositor_bindings/web_layer_impl.h" |
| 23 #include "webkit/media/android/webmediaplayer_manager_android.h" | 23 #include "webkit/media/android/webmediaplayer_manager_android.h" |
| 24 #include "webkit/media/android/webmediaplayer_proxy_android.h" | 24 #include "webkit/media/android/webmediaplayer_proxy_android.h" |
| 25 #include "webkit/media/media_switches.h" | 25 #include "webkit/media/media_switches.h" |
| 26 #include "webkit/media/webmediaplayer_util.h" | 26 #include "webkit/media/webmediaplayer_util.h" |
| 27 | 27 |
| 28 #if defined(GOOGLE_TV) | 28 #if defined(GOOGLE_TV) |
| 29 #include "webkit/media/android/media_source_delegate.h" | 29 #include "webkit/media/android/media_source_delegate.h" |
| 30 #include "webkit/media/media_stream_audio_renderer.h" |
| 31 #include "webkit/media/media_stream_client.h" |
| 30 #endif | 32 #endif |
| 31 | 33 |
| 32 static const uint32 kGLTextureExternalOES = 0x8D65; | 34 static const uint32 kGLTextureExternalOES = 0x8D65; |
| 33 | 35 |
| 34 using WebKit::WebMediaPlayer; | 36 using WebKit::WebMediaPlayer; |
| 35 using WebKit::WebMediaSource; | 37 using WebKit::WebMediaSource; |
| 36 using WebKit::WebSize; | 38 using WebKit::WebSize; |
| 37 using WebKit::WebString; | 39 using WebKit::WebString; |
| 38 using WebKit::WebTimeRanges; | 40 using WebKit::WebTimeRanges; |
| 39 using WebKit::WebURL; | 41 using WebKit::WebURL; |
| 40 using media::MediaPlayerBridge; | 42 using media::MediaPlayerBridge; |
| 41 using media::VideoFrame; | 43 using media::VideoFrame; |
| 42 | 44 |
| 43 namespace webkit_media { | 45 namespace webkit_media { |
| 44 | 46 |
| 45 WebMediaPlayerAndroid::WebMediaPlayerAndroid( | 47 WebMediaPlayerAndroid::WebMediaPlayerAndroid( |
| 46 WebKit::WebFrame* frame, | 48 WebKit::WebFrame* frame, |
| 47 WebKit::WebMediaPlayerClient* client, | 49 WebKit::WebMediaPlayerClient* client, |
| 48 WebMediaPlayerManagerAndroid* manager, | 50 WebMediaPlayerManagerAndroid* manager, |
| 49 WebMediaPlayerProxyAndroid* proxy, | 51 WebMediaPlayerProxyAndroid* proxy, |
| 50 StreamTextureFactory* factory, | 52 StreamTextureFactory* factory, |
| 51 media::MediaLog* media_log) | 53 media::MediaLog* media_log, |
| 54 MediaStreamClient* media_stream_client, |
| 55 media::Demuxer* demuxer) |
| 52 : frame_(frame), | 56 : frame_(frame), |
| 53 client_(client), | 57 client_(client), |
| 54 buffered_(1u), | 58 buffered_(1u), |
| 55 main_loop_(base::MessageLoop::current()), | 59 main_loop_(base::MessageLoop::current()), |
| 56 pending_seek_(0), | 60 pending_seek_(0), |
| 57 seeking_(false), | 61 seeking_(false), |
| 58 did_loading_progress_(false), | 62 did_loading_progress_(false), |
| 59 manager_(manager), | 63 manager_(manager), |
| 60 network_state_(WebMediaPlayer::NetworkStateEmpty), | 64 network_state_(WebMediaPlayer::NetworkStateEmpty), |
| 61 ready_state_(WebMediaPlayer::ReadyStateHaveNothing), | 65 ready_state_(WebMediaPlayer::ReadyStateHaveNothing), |
| 62 is_playing_(false), | 66 is_playing_(false), |
| 63 needs_establish_peer_(true), | 67 needs_establish_peer_(true), |
| 64 has_size_info_(false), | 68 has_size_info_(false), |
| 65 stream_texture_factory_(factory), | 69 stream_texture_factory_(factory), |
| 66 needs_external_surface_(false), | 70 needs_external_surface_(false), |
| 67 video_frame_provider_client_(NULL), | 71 video_frame_provider_client_(NULL), |
| 68 proxy_(proxy), | 72 proxy_(proxy), |
| 69 current_time_(0), | 73 current_time_(0), |
| 70 media_log_(media_log) { | 74 media_log_(media_log), |
| 75 media_stream_client_(media_stream_client), |
| 76 demuxer_(demuxer) { |
| 71 main_loop_->AddDestructionObserver(this); | 77 main_loop_->AddDestructionObserver(this); |
| 72 if (manager_) | 78 if (manager_) |
| 73 player_id_ = manager_->RegisterMediaPlayer(this); | 79 player_id_ = manager_->RegisterMediaPlayer(this); |
| 74 | 80 |
| 75 if (stream_texture_factory_.get()) { | 81 if (stream_texture_factory_.get()) { |
| 76 stream_texture_proxy_.reset(stream_texture_factory_->CreateProxy()); | 82 stream_texture_proxy_.reset(stream_texture_factory_->CreateProxy()); |
| 77 stream_id_ = stream_texture_factory_->CreateStreamTexture(&texture_id_); | 83 stream_id_ = stream_texture_factory_->CreateStreamTexture(&texture_id_); |
| 78 ReallocateVideoFrame(); | 84 ReallocateVideoFrame(); |
| 79 } | 85 } |
| 80 } | 86 } |
| 81 | 87 |
| 82 WebMediaPlayerAndroid::~WebMediaPlayerAndroid() { | 88 WebMediaPlayerAndroid::~WebMediaPlayerAndroid() { |
| 83 SetVideoFrameProviderClient(NULL); | 89 SetVideoFrameProviderClient(NULL); |
| 84 client_->setWebLayer(NULL); | 90 client_->setWebLayer(NULL); |
| 85 | 91 |
| 86 if (proxy_) | 92 if (proxy_) |
| 87 proxy_->DestroyPlayer(player_id_); | 93 proxy_->DestroyPlayer(player_id_); |
| 88 | 94 |
| 89 if (stream_id_) | 95 if (stream_id_) |
| 90 stream_texture_factory_->DestroyStreamTexture(texture_id_); | 96 stream_texture_factory_->DestroyStreamTexture(texture_id_); |
| 91 | 97 |
| 92 if (manager_) | 98 if (manager_) |
| 93 manager_->UnregisterMediaPlayer(player_id_); | 99 manager_->UnregisterMediaPlayer(player_id_); |
| 94 | 100 |
| 95 if (main_loop_) | 101 if (main_loop_) |
| 96 main_loop_->RemoveDestructionObserver(this); | 102 main_loop_->RemoveDestructionObserver(this); |
| 103 #if defined(GOOGLE_TV) |
| 104 if (audio_renderer_) { |
| 105 if (audio_renderer_->IsLocalRenderer()) { |
| 106 audio_renderer_->Stop(); |
| 107 } else if (!paused()) { |
| 108 // The |audio_renderer_| can be shared by multiple remote streams, and |
| 109 // it will be stopped when WebRtcAudioDeviceImpl goes away. So we simply |
| 110 // pause the |audio_renderer_| here to avoid re-creating the |
| 111 // |audio_renderer_|. |
| 112 audio_renderer_->Pause(); |
| 113 } |
| 114 } |
| 115 #endif |
| 97 } | 116 } |
| 98 | 117 |
| 99 void WebMediaPlayerAndroid::load(const WebURL& url, CORSMode cors_mode) { | 118 void WebMediaPlayerAndroid::load(const WebURL& url, CORSMode cors_mode) { |
| 100 load(url, NULL, cors_mode); | 119 load(url, NULL, cors_mode); |
| 101 } | 120 } |
| 102 | 121 |
| 103 void WebMediaPlayerAndroid::load(const WebURL& url, | 122 void WebMediaPlayerAndroid::load(const WebURL& url, |
| 104 WebMediaSource* media_source, | 123 WebMediaSource* media_source, |
| 105 CORSMode cors_mode) { | 124 CORSMode cors_mode) { |
| 106 if (cors_mode != CORSModeUnspecified) | 125 if (cors_mode != CORSModeUnspecified) |
| 107 NOTIMPLEMENTED() << "No CORS support"; | 126 NOTIMPLEMENTED() << "No CORS support"; |
| 108 | 127 |
| 109 scoped_ptr<WebKit::WebMediaSource> scoped_media_source(media_source); | 128 MediaPlayerBridge::MediaSource media_source_type = |
| 129 MediaPlayerBridge::MEDIA_SOURCE_URL; |
| 110 #if defined(GOOGLE_TV) | 130 #if defined(GOOGLE_TV) |
| 111 if (media_source) { | 131 if (media_source) { |
| 112 media_source_delegate_.reset( | 132 media_source_type = MediaPlayerBridge::MEDIA_SOURCE_MSE; |
| 113 new MediaSourceDelegate( | 133 |
| 114 frame_, client_, proxy_, player_id_, media_log_)); | |
| 115 // |media_source_delegate_| is owned, so Unretained() is safe here. | 134 // |media_source_delegate_| is owned, so Unretained() is safe here. |
| 116 media_source_delegate_->Initialize( | 135 media_source_delegate_.reset(new MediaSourceDelegate( |
| 117 scoped_media_source.Pass(), | 136 proxy_, player_id_, |
| 118 base::Bind(&WebMediaPlayerAndroid::UpdateNetworkState, | 137 base::Bind(&WebMediaPlayerAndroid::UpdateNetworkState, |
| 119 base::Unretained(this))); | 138 base::Unretained(this)))); |
| 139 media_source_delegate_->InitializeMediaSource( |
| 140 frame_, client_, media_source, media_log_); |
| 141 } else if (media_stream_client_) { |
| 142 media_source_type = MediaPlayerBridge::MEDIA_SOURCE_STREAM; |
| 143 |
| 144 media_source_delegate_.reset(new MediaSourceDelegate( |
| 145 proxy_, player_id_, |
| 146 base::Bind(&WebMediaPlayerAndroid::UpdateNetworkState, |
| 147 base::Unretained(this)))); |
| 148 media_source_delegate_->InitializeMediaStream( |
| 149 demuxer_, destroy_demuxer_cb_); |
| 150 |
| 151 audio_renderer_ = media_stream_client_->GetAudioRenderer(url); |
| 152 if (audio_renderer_) |
| 153 audio_renderer_->Start(); |
| 120 } | 154 } |
| 155 #else |
| 156 if (media_source) |
| 157 media_source_type = MediaPlayerBridge::MEDIA_SOURCE_MSE; |
| 121 #endif | 158 #endif |
| 122 | 159 |
| 160 InitializeMediaPlayer(url, media_source_type); |
| 161 } |
| 162 |
| 163 void WebMediaPlayerAndroid::InitializeMediaPlayer( |
| 164 const WebURL& url, |
| 165 MediaPlayerBridge::MediaSource media_source) { |
| 123 url_ = url; | 166 url_ = url; |
| 124 GURL first_party_url = frame_->document().firstPartyForCookies(); | 167 GURL first_party_url = frame_->document().firstPartyForCookies(); |
| 125 if (proxy_) { | 168 if (proxy_) { |
| 126 proxy_->Initialize(player_id_, url_, media_source != NULL, first_party_url); | 169 proxy_->Initialize(player_id_, url, media_source, first_party_url); |
| 127 if (manager_->IsInFullscreen(frame_)) | 170 if (manager_->IsInFullscreen(frame_)) |
| 128 proxy_->EnterFullscreen(player_id_); | 171 proxy_->EnterFullscreen(player_id_); |
| 129 } | 172 } |
| 130 | 173 |
| 131 UpdateNetworkState(WebMediaPlayer::NetworkStateLoading); | 174 UpdateNetworkState(WebMediaPlayer::NetworkStateLoading); |
| 132 UpdateReadyState(WebMediaPlayer::ReadyStateHaveNothing); | 175 UpdateReadyState(WebMediaPlayer::ReadyStateHaveNothing); |
| 133 } | 176 } |
| 134 | 177 |
| 135 void WebMediaPlayerAndroid::cancelLoad() { | 178 void WebMediaPlayerAndroid::cancelLoad() { |
| 136 NOTIMPLEMENTED(); | 179 NOTIMPLEMENTED(); |
| 137 } | 180 } |
| 138 | 181 |
| 139 void WebMediaPlayerAndroid::play() { | 182 void WebMediaPlayerAndroid::play() { |
| 140 #if defined(GOOGLE_TV) | 183 #if defined(GOOGLE_TV) |
| 141 if (hasVideo() && needs_external_surface_) { | 184 if (hasVideo() && needs_external_surface_) { |
| 142 DCHECK(!needs_establish_peer_); | |
| 143 if (proxy_) | 185 if (proxy_) |
| 144 proxy_->RequestExternalSurface(player_id_); | 186 proxy_->RequestExternalSurface(player_id_); |
| 145 } | 187 } |
| 188 if (audio_renderer_ && paused()) |
| 189 audio_renderer_->Play(); |
| 146 #endif | 190 #endif |
| 147 if (hasVideo() && needs_establish_peer_) | 191 if (hasVideo() && needs_establish_peer_) |
| 148 EstablishSurfaceTexturePeer(); | 192 EstablishSurfaceTexturePeer(); |
| 149 | 193 |
| 150 if (paused() && proxy_) | 194 if (paused() && proxy_) |
| 151 proxy_->Start(player_id_); | 195 proxy_->Start(player_id_); |
| 152 is_playing_ = true; | 196 is_playing_ = true; |
| 153 } | 197 } |
| 154 | 198 |
| 155 void WebMediaPlayerAndroid::pause() { | 199 void WebMediaPlayerAndroid::pause() { |
| 200 #if defined(GOOGLE_TV) |
| 201 if (audio_renderer_ && !paused()) |
| 202 audio_renderer_->Pause(); |
| 203 #endif |
| 156 if (proxy_) | 204 if (proxy_) |
| 157 proxy_->Pause(player_id_); | 205 proxy_->Pause(player_id_); |
| 158 is_playing_ = false; | 206 is_playing_ = false; |
| 159 } | 207 } |
| 160 | 208 |
| 161 void WebMediaPlayerAndroid::seek(double seconds) { | 209 void WebMediaPlayerAndroid::seek(double seconds) { |
| 162 pending_seek_ = seconds; | 210 pending_seek_ = seconds; |
| 163 seeking_ = true; | 211 seeking_ = true; |
| 164 | 212 |
| 165 base::TimeDelta seek_time = ConvertSecondsToTimestamp(seconds); | 213 base::TimeDelta seek_time = ConvertSecondsToTimestamp(seconds); |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 #if defined(GOOGLE_TV) | 505 #if defined(GOOGLE_TV) |
| 458 static bool has_switch = CommandLine::ForCurrentProcess()->HasSwitch( | 506 static bool has_switch = CommandLine::ForCurrentProcess()->HasSwitch( |
| 459 switches::kUseExternalVideoSurfaceThresholdInPixels); | 507 switches::kUseExternalVideoSurfaceThresholdInPixels); |
| 460 static int threshold = 0; | 508 static int threshold = 0; |
| 461 static bool parsed_arg = | 509 static bool parsed_arg = |
| 462 has_switch && | 510 has_switch && |
| 463 base::StringToInt( | 511 base::StringToInt( |
| 464 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 512 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 465 switches::kUseExternalVideoSurfaceThresholdInPixels), | 513 switches::kUseExternalVideoSurfaceThresholdInPixels), |
| 466 &threshold); | 514 &threshold); |
| 467 | |
| 468 if ((parsed_arg && threshold <= width * height) || | 515 if ((parsed_arg && threshold <= width * height) || |
| 469 // Use H/W surface for MSE as the content is protected. | 516 // Use H/W surface for MSE as the content is protected. |
| 470 media_source_delegate_) { | 517 media_source_delegate_) { |
| 471 needs_external_surface_ = true; | 518 needs_external_surface_ = true; |
| 472 SetNeedsEstablishPeer(false); | 519 SetNeedsEstablishPeer(false); |
| 473 if (!paused() && proxy_) | 520 if (!paused() && proxy_) |
| 474 proxy_->RequestExternalSurface(player_id_); | 521 proxy_->RequestExternalSurface(player_id_); |
| 475 } | 522 } |
| 476 #endif | 523 #endif |
| 477 | 524 |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 | 694 |
| 648 // Return false when the geometry hasn't been changed from the last time. | 695 // Return false when the geometry hasn't been changed from the last time. |
| 649 if (last_computed_rect_ == *rect) | 696 if (last_computed_rect_ == *rect) |
| 650 return false; | 697 return false; |
| 651 | 698 |
| 652 // Store the changed geometry information when it is actually changed. | 699 // Store the changed geometry information when it is actually changed. |
| 653 last_computed_rect_ = *rect; | 700 last_computed_rect_ = *rect; |
| 654 return true; | 701 return true; |
| 655 } | 702 } |
| 656 | 703 |
| 657 WebMediaPlayer::MediaKeyException | 704 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::generateKeyRequest( |
| 658 WebMediaPlayerAndroid::generateKeyRequest(const WebString& key_system, | 705 const WebString& key_system, |
| 659 const unsigned char* init_data, | 706 const unsigned char* init_data, |
| 660 unsigned init_data_length) { | 707 unsigned init_data_length) { |
| 661 if (media_source_delegate_) { | 708 if (media_source_delegate_) { |
| 662 return media_source_delegate_->GenerateKeyRequest( | 709 return media_source_delegate_->GenerateKeyRequest( |
| 663 key_system, init_data, init_data_length); | 710 key_system, init_data, init_data_length); |
| 664 } | 711 } |
| 665 return MediaKeyExceptionKeySystemNotSupported; | 712 return MediaKeyExceptionKeySystemNotSupported; |
| 666 } | 713 } |
| 667 | 714 |
| 668 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::addKey( | 715 WebMediaPlayer::MediaKeyException WebMediaPlayerAndroid::addKey( |
| 669 const WebString& key_system, | 716 const WebString& key_system, |
| 670 const unsigned char* key, | 717 const unsigned char* key, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 687 return MediaKeyExceptionKeySystemNotSupported; | 734 return MediaKeyExceptionKeySystemNotSupported; |
| 688 } | 735 } |
| 689 | 736 |
| 690 void WebMediaPlayerAndroid::OnReadFromDemuxer( | 737 void WebMediaPlayerAndroid::OnReadFromDemuxer( |
| 691 media::DemuxerStream::Type type, bool seek_done) { | 738 media::DemuxerStream::Type type, bool seek_done) { |
| 692 if (media_source_delegate_) | 739 if (media_source_delegate_) |
| 693 media_source_delegate_->OnReadFromDemuxer(type, seek_done); | 740 media_source_delegate_->OnReadFromDemuxer(type, seek_done); |
| 694 else | 741 else |
| 695 NOTIMPLEMENTED(); | 742 NOTIMPLEMENTED(); |
| 696 } | 743 } |
| 744 |
| 745 void WebMediaPlayerAndroid::SetDestroyDemuxerCB( |
| 746 const MediaSourceDelegate::DestroyDemuxerCB& destroy_demuxer_cb) { |
| 747 destroy_demuxer_cb_ = destroy_demuxer_cb; |
| 748 } |
| 697 #endif | 749 #endif |
| 698 | 750 |
| 699 void WebMediaPlayerAndroid::enterFullscreen() { | 751 void WebMediaPlayerAndroid::enterFullscreen() { |
| 700 if (proxy_ && manager_->CanEnterFullscreen(frame_)) { | 752 if (proxy_ && manager_->CanEnterFullscreen(frame_)) { |
| 701 proxy_->EnterFullscreen(player_id_); | 753 proxy_->EnterFullscreen(player_id_); |
| 702 SetNeedsEstablishPeer(false); | 754 SetNeedsEstablishPeer(false); |
| 703 } | 755 } |
| 704 } | 756 } |
| 705 | 757 |
| 706 void WebMediaPlayerAndroid::exitFullscreen() { | 758 void WebMediaPlayerAndroid::exitFullscreen() { |
| 707 if (proxy_) | 759 if (proxy_) |
| 708 proxy_->ExitFullscreen(player_id_); | 760 proxy_->ExitFullscreen(player_id_); |
| 709 } | 761 } |
| 710 | 762 |
| 711 bool WebMediaPlayerAndroid::canEnterFullscreen() const { | 763 bool WebMediaPlayerAndroid::canEnterFullscreen() const { |
| 712 return manager_->CanEnterFullscreen(frame_); | 764 return manager_->CanEnterFullscreen(frame_); |
| 713 } | 765 } |
| 714 | 766 |
| 715 } // namespace webkit_media | 767 } // namespace webkit_media |
| OLD | NEW |