Commit 701cb640 authored by boliu's avatar boliu Committed by Commit bot

Clean up WebMediaPlayerAndroid needs_establish_peer_

needs_establish_peer_ has been used to skip actual estalishing a surface
texture peer based on many conditions. Not all places that modify
actually checks all these conditions so there are bound to be some bugs.

This CL aims to tease out all these different conditions and put them in
a single function EstablishSurfaceTexturePeerIfNeeded. This function
should be called any time these conditions are changed. Makes for much
saner reading.

BUG=412578

Review URL: https://codereview.chromium.org/557593002

Cr-Commit-Position: refs/heads/master@{#294068}
parent 1633055f
......@@ -135,6 +135,7 @@ WebMediaPlayerAndroid::WebMediaPlayerAndroid(
stream_id_(0),
is_playing_(false),
needs_establish_peer_(true),
in_fullscreen_(false),
stream_texture_proxy_initialized_(false),
has_size_info_(false),
stream_texture_factory_(factory),
......@@ -162,7 +163,7 @@ WebMediaPlayerAndroid::WebMediaPlayerAndroid(
if (force_use_overlay_embedded_video_ ||
player_manager_->ShouldUseVideoOverlayForEmbeddedEncryptedVideo()) {
// Defer stream texture creation until we are sure it's necessary.
needs_establish_peer_ = false;
needs_external_surface_ = true;
current_frame_ = VideoFrame::CreateBlackFrame(gfx::Size(1, 1));
}
#endif // defined(VIDEO_HOLE)
......@@ -303,18 +304,11 @@ void WebMediaPlayerAndroid::play() {
#if defined(VIDEO_HOLE)
if ((hasVideo() || IsHLSStream()) && needs_external_surface_ &&
!player_manager_->IsInFullscreen(frame_)) {
DCHECK(!needs_establish_peer_);
player_manager_->RequestExternalSurface(player_id_, last_computed_rect_);
}
#endif // defined(VIDEO_HOLE)
TryCreateStreamTextureProxyIfNeeded();
// There is no need to establish the surface texture peer for fullscreen
// video.
if ((hasVideo() || IsHLSStream()) && needs_establish_peer_ &&
!player_manager_->IsInFullscreen(frame_)) {
EstablishSurfaceTexturePeer();
}
if (paused())
player_manager_->Start(player_id_);
......@@ -754,7 +748,6 @@ void WebMediaPlayerAndroid::OnPlaybackComplete() {
// process are sequential, the OnSeekComplete() will only occur
// once OnPlaybackComplete() is done. As the playback can only be executed
// upon completion of OnSeekComplete(), the request needs to be saved.
is_playing_ = false;
if (seeking_ && seek_time_ == base::TimeDelta())
pending_playback_ = true;
}
......@@ -820,25 +813,18 @@ void WebMediaPlayerAndroid::OnVideoSizeChanged(int width, int height) {
if (force_use_overlay_embedded_video_ ||
(media_source_delegate_ && media_source_delegate_->IsVideoEncrypted() &&
player_manager_->ShouldUseVideoOverlayForEmbeddedEncryptedVideo())) {
needs_external_surface_ = true;
if (!paused() && !player_manager_->IsInFullscreen(frame_))
player_manager_->RequestExternalSurface(player_id_, last_computed_rect_);
} else if (stream_texture_proxy_ && !stream_id_) {
// Do deferred stream texture creation finally.
DoCreateStreamTexture();
SetNeedsEstablishPeer(true);
} else {
needs_external_surface_ = false;
}
#endif // defined(VIDEO_HOLE)
natural_size_.width = width;
natural_size_.height = height;
// When play() gets called, |natural_size_| may still be empty and
// EstablishSurfaceTexturePeer() will not get called. As a result, the video
// may play without a surface texture. When we finally get the valid video
// size here, we should call EstablishSurfaceTexturePeer() if it has not been
// previously called.
if (!paused() && needs_establish_peer_)
EstablishSurfaceTexturePeer();
// hasVideo() might have changed since play was called, so need to possibly
// estlibash peer here.
EstablishSurfaceTexturePeerIfNeeded();
ReallocateVideoFrame();
......@@ -876,16 +862,15 @@ void WebMediaPlayerAndroid::OnConnectedToRemoteDevice(
DCHECK(!media_source_delegate_);
DrawRemotePlaybackText(remote_playback_message);
is_remote_ = true;
SetNeedsEstablishPeer(false);
needs_establish_peer_ = true;
EstablishSurfaceTexturePeerIfNeeded();
}
void WebMediaPlayerAndroid::OnDisconnectedFromRemoteDevice() {
DCHECK(main_thread_checker_.CalledOnValidThread());
DCHECK(!media_source_delegate_);
SetNeedsEstablishPeer(true);
if (!paused())
EstablishSurfaceTexturePeer();
is_remote_ = false;
EstablishSurfaceTexturePeerIfNeeded();
ReallocateVideoFrame();
}
......@@ -895,13 +880,8 @@ void WebMediaPlayerAndroid::OnDidEnterFullscreen() {
}
void WebMediaPlayerAndroid::OnDidExitFullscreen() {
// |needs_external_surface_| is always false on non-TV devices.
if (!needs_external_surface_)
SetNeedsEstablishPeer(true);
// We had the fullscreen surface connected to Android MediaPlayer,
// so reconnect our surface texture for embedded playback.
if (!paused() && needs_establish_peer_)
EstablishSurfaceTexturePeer();
in_fullscreen_ = false;
EstablishSurfaceTexturePeerIfNeeded();
#if defined(VIDEO_HOLE)
if (!paused() && needs_external_surface_)
......@@ -963,9 +943,7 @@ void WebMediaPlayerAndroid::UpdateReadyState(
}
void WebMediaPlayerAndroid::OnPlayerReleased() {
// |needs_external_surface_| is always false on non-TV devices.
if (!needs_external_surface_)
needs_establish_peer_ = true;
needs_establish_peer_ = true; // Established when this plays.
if (is_playing_)
OnMediaPlayerPause();
......@@ -995,8 +973,7 @@ void WebMediaPlayerAndroid::ReleaseMediaResources() {
break;
}
player_manager_->ReleaseResources(player_id_);
if (!needs_external_surface_)
SetNeedsEstablishPeer(true);
needs_establish_peer_ = true; // Established when this plays.
}
void WebMediaPlayerAndroid::OnDestruct() {
......@@ -1239,32 +1216,17 @@ void WebMediaPlayerAndroid::TryCreateStreamTextureProxyIfNeeded() {
if (!stream_texture_factory_)
return;
if (needs_external_surface_)
return;
stream_texture_proxy_.reset(stream_texture_factory_->CreateProxy());
if (needs_establish_peer_ && stream_texture_proxy_) {
if (stream_texture_proxy_) {
DoCreateStreamTexture();
ReallocateVideoFrame();
}
if (stream_texture_proxy_ && video_frame_provider_client_)
stream_texture_proxy_->SetClient(video_frame_provider_client_);
}
void WebMediaPlayerAndroid::EstablishSurfaceTexturePeer() {
DCHECK(main_thread_checker_.CalledOnValidThread());
if (!stream_texture_proxy_)
return;
if (stream_texture_factory_.get() && stream_id_)
stream_texture_factory_->EstablishPeer(stream_id_, player_id_);
// Set the deferred size because the size was changed in remote mode.
if (!is_remote_ && cached_stream_texture_size_ != natural_size_) {
stream_texture_factory_->SetStreamTextureSize(
stream_id_, gfx::Size(natural_size_.width, natural_size_.height));
cached_stream_texture_size_ = natural_size_;
if (video_frame_provider_client_)
stream_texture_proxy_->SetClient(video_frame_provider_client_);
}
needs_establish_peer_ = false;
}
void WebMediaPlayerAndroid::DoCreateStreamTexture() {
......@@ -1275,8 +1237,21 @@ void WebMediaPlayerAndroid::DoCreateStreamTexture() {
kGLTextureExternalOES, &texture_id_, &texture_mailbox_);
}
void WebMediaPlayerAndroid::SetNeedsEstablishPeer(bool needs_establish_peer) {
needs_establish_peer_ = needs_establish_peer;
void WebMediaPlayerAndroid::EstablishSurfaceTexturePeerIfNeeded() {
DCHECK(main_thread_checker_.CalledOnValidThread());
if (!needs_establish_peer_ || in_fullscreen_ || needs_external_surface_ ||
is_remote_ || !is_playing_ || !stream_texture_proxy_ ||
(!hasVideo() && !IsHLSStream())) {
return;
}
stream_texture_factory_->EstablishPeer(stream_id_, player_id_);
if (cached_stream_texture_size_ != natural_size_) {
stream_texture_factory_->SetStreamTextureSize(
stream_id_, gfx::Size(natural_size_.width, natural_size_.height));
cached_stream_texture_size_ = natural_size_;
}
needs_establish_peer_ = false;
}
void WebMediaPlayerAndroid::setPoster(const blink::WebURL& poster) {
......@@ -1285,6 +1260,7 @@ void WebMediaPlayerAndroid::setPoster(const blink::WebURL& poster) {
void WebMediaPlayerAndroid::UpdatePlayingState(bool is_playing) {
is_playing_ = is_playing;
EstablishSurfaceTexturePeerIfNeeded();
if (!delegate_)
return;
if (is_playing)
......@@ -1754,8 +1730,10 @@ void WebMediaPlayerAndroid::SetDecryptorReadyCB(
void WebMediaPlayerAndroid::enterFullscreen() {
if (player_manager_->CanEnterFullscreen(frame_)) {
player_manager_->EnterFullscreen(player_id_, frame_);
SetNeedsEstablishPeer(false);
}
in_fullscreen_ = true;
needs_establish_peer_ = true;
EstablishSurfaceTexturePeerIfNeeded();
}
bool WebMediaPlayerAndroid::canEnterFullscreen() const {
......
......@@ -266,12 +266,9 @@ class WebMediaPlayerAndroid : public blink::WebMediaPlayer,
void TryCreateStreamTextureProxyIfNeeded();
void DoCreateStreamTexture();
// Helper method to reestablish the surface texture peer for android
// media player.
void EstablishSurfaceTexturePeer();
// Requesting whether the surface texture peer needs to be reestablished.
void SetNeedsEstablishPeer(bool needs_establish_peer);
// This method is meant to be idempotent. Should be called whenever any of
// conditions changes and it is ok to establish peer.
void EstablishSurfaceTexturePeerIfNeeded();
private:
void InitializePlayer(const GURL& url,
......@@ -408,8 +405,15 @@ class WebMediaPlayerAndroid : public blink::WebMediaPlayer,
bool is_playing_;
// Whether media player needs to re-establish the surface texture peer.
// This should be unset in EstablishSurfaceTexturePeerIfNeeded() only, and set
// we believe peer is disconnected that we need to establish it again.
// Setting this should not be conditioned on additional state, eg playing
// or full screen.
bool needs_establish_peer_;
// This is a helper state used in EstablishSurfaceTexturePeerIfNeeded().
bool in_fullscreen_;
// Whether |stream_texture_proxy_| is initialized.
bool stream_texture_proxy_initialized_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment