Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / content / browser / android / content_video_view.cc
blobf7e9fc891a4934388c8c5f1411b90f1929a7836f
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.
5 #include "content/browser/android/content_video_view.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/browser/android/content_view_core_impl.h"
11 #include "content/browser/media/android/browser_media_player_manager.h"
12 #include "content/browser/power_save_blocker_impl.h"
13 #include "content/common/android/surface_texture_peer.h"
14 #include "content/public/common/content_switches.h"
15 #include "jni/ContentVideoView_jni.h"
17 using base::android::AttachCurrentThread;
18 using base::android::CheckException;
19 using base::android::ScopedJavaGlobalRef;
21 namespace content {
23 namespace {
24 // There can only be one content video view at a time, this holds onto that
25 // singleton instance.
26 ContentVideoView* g_content_video_view = NULL;
28 } // namespace
30 static jobject GetSingletonJavaContentVideoView(JNIEnv*env, jclass) {
31 if (g_content_video_view)
32 return g_content_video_view->GetJavaObject(env).Release();
33 else
34 return NULL;
37 bool ContentVideoView::RegisterContentVideoView(JNIEnv* env) {
38 return RegisterNativesImpl(env);
41 ContentVideoView* ContentVideoView::GetInstance() {
42 return g_content_video_view;
45 ContentVideoView::ContentVideoView(
46 BrowserMediaPlayerManager* manager)
47 : manager_(manager),
48 weak_factory_(this) {
49 DCHECK(!g_content_video_view);
50 j_content_video_view_ = CreateJavaObject();
51 g_content_video_view = this;
52 CreatePowerSaveBlocker();
55 ContentVideoView::~ContentVideoView() {
56 DCHECK(g_content_video_view);
57 JNIEnv* env = AttachCurrentThread();
58 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
59 if (!content_video_view.is_null()) {
60 Java_ContentVideoView_destroyContentVideoView(env,
61 content_video_view.obj(), true);
62 j_content_video_view_.reset();
64 g_content_video_view = NULL;
67 void ContentVideoView::OpenVideo() {
68 JNIEnv* env = AttachCurrentThread();
69 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
70 if (!content_video_view.is_null()) {
71 CreatePowerSaveBlocker();
72 Java_ContentVideoView_openVideo(env, content_video_view.obj());
76 void ContentVideoView::OnMediaPlayerError(int error_type) {
77 JNIEnv* env = AttachCurrentThread();
78 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
79 if (!content_video_view.is_null()) {
80 power_save_blocker_.reset();
81 Java_ContentVideoView_onMediaPlayerError(env, content_video_view.obj(),
82 error_type);
86 void ContentVideoView::OnVideoSizeChanged(int width, int height) {
87 JNIEnv* env = AttachCurrentThread();
88 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
89 if (!content_video_view.is_null()) {
90 Java_ContentVideoView_onVideoSizeChanged(env, content_video_view.obj(),
91 width, height);
95 void ContentVideoView::OnBufferingUpdate(int percent) {
96 JNIEnv* env = AttachCurrentThread();
97 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
98 if (!content_video_view.is_null()) {
99 Java_ContentVideoView_onBufferingUpdate(env, content_video_view.obj(),
100 percent);
104 void ContentVideoView::OnPlaybackComplete() {
105 JNIEnv* env = AttachCurrentThread();
106 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
107 if (!content_video_view.is_null()) {
108 power_save_blocker_.reset();
109 Java_ContentVideoView_onPlaybackComplete(env, content_video_view.obj());
113 void ContentVideoView::OnExitFullscreen() {
114 JNIEnv* env = AttachCurrentThread();
115 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
116 if (!content_video_view.is_null())
117 Java_ContentVideoView_onExitFullscreen(env, content_video_view.obj());
120 void ContentVideoView::UpdateMediaMetadata() {
121 JNIEnv* env = AttachCurrentThread();
122 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
123 if (content_video_view.is_null())
124 return;
126 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
127 if (player && player->IsPlayerReady()) {
128 Java_ContentVideoView_onUpdateMediaMetadata(
129 env, content_video_view.obj(), player->GetVideoWidth(),
130 player->GetVideoHeight(), player->GetDuration().InMilliseconds(),
131 player->CanPause(),player->CanSeekForward(), player->CanSeekBackward());
135 int ContentVideoView::GetVideoWidth(JNIEnv*, jobject obj) const {
136 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
137 return player ? player->GetVideoWidth() : 0;
140 int ContentVideoView::GetVideoHeight(JNIEnv*, jobject obj) const {
141 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
142 return player ? player->GetVideoHeight() : 0;
145 int ContentVideoView::GetDurationInMilliSeconds(JNIEnv*, jobject obj) const {
146 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
147 return player ? player->GetDuration().InMilliseconds() : -1;
150 int ContentVideoView::GetCurrentPosition(JNIEnv*, jobject obj) const {
151 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
152 return player ? player->GetCurrentTime().InMilliseconds() : 0;
155 bool ContentVideoView::IsPlaying(JNIEnv*, jobject obj) {
156 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
157 return player ? player->IsPlaying() : false;
160 void ContentVideoView::SeekTo(JNIEnv*, jobject obj, jint msec) {
161 manager_->FullscreenPlayerSeek(msec);
164 void ContentVideoView::Play(JNIEnv*, jobject obj) {
165 CreatePowerSaveBlocker();
166 manager_->FullscreenPlayerPlay();
169 void ContentVideoView::Pause(JNIEnv*, jobject obj) {
170 power_save_blocker_.reset();
171 manager_->FullscreenPlayerPause();
174 void ContentVideoView::ExitFullscreen(
175 JNIEnv*, jobject, jboolean release_media_player) {
176 power_save_blocker_.reset();
177 j_content_video_view_.reset();
178 manager_->ExitFullscreen(release_media_player);
181 void ContentVideoView::SetSurface(JNIEnv* env, jobject obj,
182 jobject surface) {
183 manager_->SetVideoSurface(
184 gfx::ScopedJavaSurface::AcquireExternalSurface(surface));
187 void ContentVideoView::RequestMediaMetadata(JNIEnv* env, jobject obj) {
188 base::MessageLoop::current()->PostTask(
189 FROM_HERE,
190 base::Bind(&ContentVideoView::UpdateMediaMetadata,
191 weak_factory_.GetWeakPtr()));
194 ScopedJavaLocalRef<jobject> ContentVideoView::GetJavaObject(JNIEnv* env) {
195 return j_content_video_view_.get(env);
198 gfx::NativeView ContentVideoView::GetNativeView() {
199 JNIEnv* env = AttachCurrentThread();
200 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
201 if (content_video_view.is_null())
202 return NULL;
204 return reinterpret_cast<gfx::NativeView>(
205 Java_ContentVideoView_getNativeViewAndroid(env,
206 content_video_view.obj()));
210 JavaObjectWeakGlobalRef ContentVideoView::CreateJavaObject() {
211 ContentViewCoreImpl* content_view_core = manager_->GetContentViewCore();
212 JNIEnv* env = AttachCurrentThread();
213 bool legacyMode = CommandLine::ForCurrentProcess()->HasSwitch(
214 switches::kDisableOverlayFullscreenVideoSubtitle);
215 return JavaObjectWeakGlobalRef(
216 env,
217 Java_ContentVideoView_createContentVideoView(
218 env,
219 content_view_core->GetContext().obj(),
220 reinterpret_cast<intptr_t>(this),
221 content_view_core->GetContentVideoViewClient().obj(),
222 legacyMode).obj());
225 void ContentVideoView::CreatePowerSaveBlocker() {
226 if (!CommandLine::ForCurrentProcess()->HasSwitch(
227 switches::kDisableOverlayFullscreenVideoSubtitle)) {
228 return;
231 if (power_save_blocker_) return;
233 power_save_blocker_ = PowerSaveBlocker::Create(
234 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep,
235 "Playing video").Pass();
236 static_cast<PowerSaveBlockerImpl*>(power_save_blocker_.get())->
237 InitDisplaySleepBlocker(GetNativeView());
239 } // namespace content