content_video_view.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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 3// found in the LICENSE file. 4 5#include "content/browser/android/content_video_view.h" 6 7#include "base/command_line.h" 8#include "base/logging.h" 9#include "content/browser/android/browser_media_player_manager.h" 10#include "content/common/android/surface_texture_peer.h" 11#include "content/public/common/content_switches.h" 12#include "jni/ContentVideoView_jni.h" 13 14using base::android::AttachCurrentThread; 15using base::android::CheckException; 16using base::android::ScopedJavaGlobalRef; 17 18namespace content { 19 20namespace { 21// There can only be one content video view at a time, this holds onto that 22// singleton instance. 23ContentVideoView* g_content_video_view = NULL; 24 25} // namespace 26 27static jobject GetSingletonJavaContentVideoView(JNIEnv*env, jclass) { 28 if (g_content_video_view) 29 return g_content_video_view->GetJavaObject(env).Release(); 30 else 31 return NULL; 32} 33 34bool ContentVideoView::RegisterContentVideoView(JNIEnv* env) { 35 return RegisterNativesImpl(env); 36} 37 38bool ContentVideoView::HasContentVideoView() { 39 return g_content_video_view; 40} 41 42ContentVideoView::ContentVideoView( 43 const ScopedJavaLocalRef<jobject>& context, 44 const ScopedJavaLocalRef<jobject>& client, 45 BrowserMediaPlayerManager* manager) 46 : manager_(manager) { 47 DCHECK(!g_content_video_view); 48 JNIEnv *env = AttachCurrentThread(); 49 j_content_video_view_ = JavaObjectWeakGlobalRef(env, 50 Java_ContentVideoView_createContentVideoView(env, context.obj(), 51 reinterpret_cast<int>(this), client.obj()).obj()); 52 g_content_video_view = this; 53} 54 55ContentVideoView::~ContentVideoView() { 56 DCHECK(g_content_video_view); 57 DCHECK(!GetJavaObject(AttachCurrentThread()).obj()); 58 g_content_video_view = NULL; 59} 60 61void ContentVideoView::OpenVideo() { 62 JNIEnv *env = AttachCurrentThread(); 63 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 64 if (!content_video_view.is_null()) 65 Java_ContentVideoView_openVideo(env, content_video_view.obj()); 66} 67 68// static 69void ContentVideoView::KeepScreenOn(bool screen_on) { 70 Java_ContentVideoView_keepScreenOnContentVideoView(AttachCurrentThread(), 71 screen_on); 72} 73 74void ContentVideoView::OnMediaPlayerError(int error_type) { 75 JNIEnv *env = AttachCurrentThread(); 76 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 77 if (!content_video_view.is_null()) { 78 Java_ContentVideoView_onMediaPlayerError(env, content_video_view.obj(), 79 error_type); 80 } 81} 82 83void ContentVideoView::OnVideoSizeChanged(int width, int height) { 84 JNIEnv *env = AttachCurrentThread(); 85 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 86 if (!content_video_view.is_null()) { 87 Java_ContentVideoView_onVideoSizeChanged(env, content_video_view.obj(), 88 width, height); 89 } 90} 91 92void ContentVideoView::OnBufferingUpdate(int percent) { 93 JNIEnv *env = AttachCurrentThread(); 94 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 95 if (!content_video_view.is_null()) { 96 Java_ContentVideoView_onBufferingUpdate(env, content_video_view.obj(), 97 percent); 98 } 99} 100 101void ContentVideoView::OnPlaybackComplete() { 102 JNIEnv *env = AttachCurrentThread(); 103 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 104 if (!content_video_view.is_null()) 105 Java_ContentVideoView_onPlaybackComplete(env, content_video_view.obj()); 106} 107 108void ContentVideoView::OnExitFullscreen() { 109 JNIEnv *env = AttachCurrentThread(); 110 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 111 if (!content_video_view.is_null()) { 112 Java_ContentVideoView_destroyContentVideoView(env, 113 content_video_view.obj()); 114 j_content_video_view_.reset(); 115 } 116} 117 118void ContentVideoView::UpdateMediaMetadata() { 119 JNIEnv *env = AttachCurrentThread(); 120 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 121 if (!content_video_view.is_null()) 122 UpdateMediaMetadata(env, content_video_view.obj()); 123} 124 125int ContentVideoView::GetVideoWidth(JNIEnv*, jobject obj) const { 126 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 127 return player ? player->GetVideoWidth() : 0; 128} 129 130int ContentVideoView::GetVideoHeight(JNIEnv*, jobject obj) const { 131 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 132 return player ? player->GetVideoHeight() : 0; 133} 134 135int ContentVideoView::GetDurationInMilliSeconds(JNIEnv*, jobject obj) const { 136 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 137 return player ? player->GetDuration().InMilliseconds() : -1; 138} 139 140int ContentVideoView::GetCurrentPosition(JNIEnv*, jobject obj) const { 141 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 142 return player ? player->GetCurrentTime().InMilliseconds() : 0; 143} 144 145bool ContentVideoView::IsPlaying(JNIEnv*, jobject obj) { 146 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 147 return player ? player->IsPlaying() : false; 148} 149 150void ContentVideoView::SeekTo(JNIEnv*, jobject obj, jint msec) { 151 manager_->FullscreenPlayerSeek(msec); 152} 153 154void ContentVideoView::Play(JNIEnv*, jobject obj) { 155 manager_->FullscreenPlayerPlay(); 156} 157 158void ContentVideoView::Pause(JNIEnv*, jobject obj) { 159 manager_->FullscreenPlayerPause(); 160} 161 162void ContentVideoView::ExitFullscreen( 163 JNIEnv*, jobject, jboolean release_media_player) { 164 j_content_video_view_.reset(); 165 manager_->ExitFullscreen(release_media_player); 166} 167 168void ContentVideoView::SetSurface(JNIEnv* env, jobject obj, 169 jobject surface) { 170 manager_->SetVideoSurface( 171 gfx::ScopedJavaSurface::AcquireExternalSurface(surface)); 172} 173 174void ContentVideoView::UpdateMediaMetadata(JNIEnv* env, jobject obj) { 175 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 176 if (player && player->IsPlayerReady()) 177 Java_ContentVideoView_onUpdateMediaMetadata( 178 env, obj, player->GetVideoWidth(), player->GetVideoHeight(), 179 player->GetDuration().InMilliseconds(), player->CanPause(), 180 player->CanSeekForward(), player->CanSeekBackward()); 181} 182 183ScopedJavaLocalRef<jobject> ContentVideoView::GetJavaObject(JNIEnv* env) { 184 return j_content_video_view_.get(env); 185} 186 187} // namespace content 188