content_video_view.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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/media/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 DestroyContentVideoView(true); 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 68void ContentVideoView::OnMediaPlayerError(int error_type) { 69 JNIEnv *env = AttachCurrentThread(); 70 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 71 if (!content_video_view.is_null()) { 72 Java_ContentVideoView_onMediaPlayerError(env, content_video_view.obj(), 73 error_type); 74 } 75} 76 77void ContentVideoView::OnVideoSizeChanged(int width, int height) { 78 JNIEnv *env = AttachCurrentThread(); 79 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 80 if (!content_video_view.is_null()) { 81 Java_ContentVideoView_onVideoSizeChanged(env, content_video_view.obj(), 82 width, height); 83 } 84} 85 86void ContentVideoView::OnBufferingUpdate(int percent) { 87 JNIEnv *env = AttachCurrentThread(); 88 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 89 if (!content_video_view.is_null()) { 90 Java_ContentVideoView_onBufferingUpdate(env, content_video_view.obj(), 91 percent); 92 } 93} 94 95void ContentVideoView::OnPlaybackComplete() { 96 JNIEnv *env = AttachCurrentThread(); 97 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 98 if (!content_video_view.is_null()) 99 Java_ContentVideoView_onPlaybackComplete(env, content_video_view.obj()); 100} 101 102void ContentVideoView::OnExitFullscreen() { 103 DestroyContentVideoView(false); 104} 105 106void ContentVideoView::UpdateMediaMetadata() { 107 JNIEnv *env = AttachCurrentThread(); 108 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 109 if (!content_video_view.is_null()) 110 UpdateMediaMetadata(env, content_video_view.obj()); 111} 112 113int ContentVideoView::GetVideoWidth(JNIEnv*, jobject obj) const { 114 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 115 return player ? player->GetVideoWidth() : 0; 116} 117 118int ContentVideoView::GetVideoHeight(JNIEnv*, jobject obj) const { 119 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 120 return player ? player->GetVideoHeight() : 0; 121} 122 123int ContentVideoView::GetDurationInMilliSeconds(JNIEnv*, jobject obj) const { 124 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 125 return player ? player->GetDuration().InMilliseconds() : -1; 126} 127 128int ContentVideoView::GetCurrentPosition(JNIEnv*, jobject obj) const { 129 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 130 return player ? player->GetCurrentTime().InMilliseconds() : 0; 131} 132 133bool ContentVideoView::IsPlaying(JNIEnv*, jobject obj) { 134 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 135 return player ? player->IsPlaying() : false; 136} 137 138void ContentVideoView::SeekTo(JNIEnv*, jobject obj, jint msec) { 139 manager_->FullscreenPlayerSeek(msec); 140} 141 142void ContentVideoView::Play(JNIEnv*, jobject obj) { 143 manager_->FullscreenPlayerPlay(); 144} 145 146void ContentVideoView::Pause(JNIEnv*, jobject obj) { 147 manager_->FullscreenPlayerPause(); 148} 149 150void ContentVideoView::ExitFullscreen( 151 JNIEnv*, jobject, jboolean release_media_player) { 152 j_content_video_view_.reset(); 153 manager_->ExitFullscreen(release_media_player); 154} 155 156void ContentVideoView::SetSurface(JNIEnv* env, jobject obj, 157 jobject surface) { 158 manager_->SetVideoSurface( 159 gfx::ScopedJavaSurface::AcquireExternalSurface(surface)); 160} 161 162void ContentVideoView::UpdateMediaMetadata(JNIEnv* env, jobject obj) { 163 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); 164 if (player && player->IsPlayerReady()) 165 Java_ContentVideoView_onUpdateMediaMetadata( 166 env, obj, player->GetVideoWidth(), player->GetVideoHeight(), 167 player->GetDuration().InMilliseconds(), player->CanPause(), 168 player->CanSeekForward(), player->CanSeekBackward()); 169} 170 171ScopedJavaLocalRef<jobject> ContentVideoView::GetJavaObject(JNIEnv* env) { 172 return j_content_video_view_.get(env); 173} 174 175void ContentVideoView::DestroyContentVideoView(bool native_view_destroyed) { 176 JNIEnv *env = AttachCurrentThread(); 177 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); 178 if (!content_video_view.is_null()) { 179 Java_ContentVideoView_destroyContentVideoView(env, 180 content_video_view.obj(), native_view_destroyed); 181 j_content_video_view_.reset(); 182 } 183} 184} // namespace content 185