| 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 "content/browser/android/content_video_view.h" | 5 #include "content/browser/android/content_video_view.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 return g_content_video_view; | 46 return g_content_video_view; |
| 47 } | 47 } |
| 48 | 48 |
| 49 ContentVideoView::ContentVideoView( | 49 ContentVideoView::ContentVideoView( |
| 50 BrowserMediaPlayerManager* manager) | 50 BrowserMediaPlayerManager* manager) |
| 51 : manager_(manager), | 51 : manager_(manager), |
| 52 weak_factory_(this) { | 52 weak_factory_(this) { |
| 53 DCHECK(!g_content_video_view); | 53 DCHECK(!g_content_video_view); |
| 54 j_content_video_view_ = CreateJavaObject(); | 54 j_content_video_view_ = CreateJavaObject(); |
| 55 g_content_video_view = this; | 55 g_content_video_view = this; |
| 56 CreatePowerSaveBlocker(); | |
| 57 } | 56 } |
| 58 | 57 |
| 59 ContentVideoView::~ContentVideoView() { | 58 ContentVideoView::~ContentVideoView() { |
| 60 DCHECK(g_content_video_view); | 59 DCHECK(g_content_video_view); |
| 61 JNIEnv* env = AttachCurrentThread(); | 60 JNIEnv* env = AttachCurrentThread(); |
| 62 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); | 61 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); |
| 63 if (!content_video_view.is_null()) { | 62 if (!content_video_view.is_null()) { |
| 64 Java_ContentVideoView_destroyContentVideoView(env, | 63 Java_ContentVideoView_destroyContentVideoView(env, |
| 65 content_video_view.obj(), true); | 64 content_video_view.obj(), true); |
| 66 j_content_video_view_.reset(); | 65 j_content_video_view_.reset(); |
| 67 } | 66 } |
| 68 g_content_video_view = NULL; | 67 g_content_video_view = NULL; |
| 69 } | 68 } |
| 70 | 69 |
| 71 void ContentVideoView::OpenVideo() { | 70 void ContentVideoView::OpenVideo() { |
| 72 JNIEnv* env = AttachCurrentThread(); | 71 JNIEnv* env = AttachCurrentThread(); |
| 73 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); | 72 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); |
| 74 if (!content_video_view.is_null()) { | 73 if (!content_video_view.is_null()) { |
| 75 CreatePowerSaveBlocker(); | |
| 76 Java_ContentVideoView_openVideo(env, content_video_view.obj()); | 74 Java_ContentVideoView_openVideo(env, content_video_view.obj()); |
| 77 } | 75 } |
| 78 } | 76 } |
| 79 | 77 |
| 80 void ContentVideoView::OnMediaPlayerError(int error_type) { | 78 void ContentVideoView::OnMediaPlayerError(int error_type) { |
| 81 JNIEnv* env = AttachCurrentThread(); | 79 JNIEnv* env = AttachCurrentThread(); |
| 82 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); | 80 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); |
| 83 if (!content_video_view.is_null()) { | 81 if (!content_video_view.is_null()) { |
| 84 power_save_blocker_.reset(); | |
| 85 Java_ContentVideoView_onMediaPlayerError(env, content_video_view.obj(), | 82 Java_ContentVideoView_onMediaPlayerError(env, content_video_view.obj(), |
| 86 error_type); | 83 error_type); |
| 87 } | 84 } |
| 88 } | 85 } |
| 89 | 86 |
| 90 void ContentVideoView::OnVideoSizeChanged(int width, int height) { | 87 void ContentVideoView::OnVideoSizeChanged(int width, int height) { |
| 91 JNIEnv* env = AttachCurrentThread(); | 88 JNIEnv* env = AttachCurrentThread(); |
| 92 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); | 89 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); |
| 93 if (!content_video_view.is_null()) { | 90 if (!content_video_view.is_null()) { |
| 94 Java_ContentVideoView_onVideoSizeChanged(env, content_video_view.obj(), | 91 Java_ContentVideoView_onVideoSizeChanged(env, content_video_view.obj(), |
| 95 width, height); | 92 width, height); |
| 96 } | 93 } |
| 97 } | 94 } |
| 98 | 95 |
| 99 void ContentVideoView::OnBufferingUpdate(int percent) { | 96 void ContentVideoView::OnBufferingUpdate(int percent) { |
| 100 JNIEnv* env = AttachCurrentThread(); | 97 JNIEnv* env = AttachCurrentThread(); |
| 101 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); | 98 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); |
| 102 if (!content_video_view.is_null()) { | 99 if (!content_video_view.is_null()) { |
| 103 Java_ContentVideoView_onBufferingUpdate(env, content_video_view.obj(), | 100 Java_ContentVideoView_onBufferingUpdate(env, content_video_view.obj(), |
| 104 percent); | 101 percent); |
| 105 } | 102 } |
| 106 } | 103 } |
| 107 | 104 |
| 108 void ContentVideoView::OnPlaybackComplete() { | 105 void ContentVideoView::OnPlaybackComplete() { |
| 109 JNIEnv* env = AttachCurrentThread(); | 106 JNIEnv* env = AttachCurrentThread(); |
| 110 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); | 107 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); |
| 111 if (!content_video_view.is_null()) { | 108 if (!content_video_view.is_null()) { |
| 112 power_save_blocker_.reset(); | |
| 113 Java_ContentVideoView_onPlaybackComplete(env, content_video_view.obj()); | 109 Java_ContentVideoView_onPlaybackComplete(env, content_video_view.obj()); |
| 114 } | 110 } |
| 115 } | 111 } |
| 116 | 112 |
| 117 void ContentVideoView::OnExitFullscreen() { | 113 void ContentVideoView::OnExitFullscreen() { |
| 118 JNIEnv* env = AttachCurrentThread(); | 114 JNIEnv* env = AttachCurrentThread(); |
| 119 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); | 115 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); |
| 120 if (!content_video_view.is_null()) | 116 if (!content_video_view.is_null()) |
| 121 Java_ContentVideoView_onExitFullscreen(env, content_video_view.obj()); | 117 Java_ContentVideoView_onExitFullscreen(env, content_video_view.obj()); |
| 122 } | 118 } |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 bool ContentVideoView::IsPlaying(JNIEnv*, jobject obj) { | 190 bool ContentVideoView::IsPlaying(JNIEnv*, jobject obj) { |
| 195 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); | 191 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer(); |
| 196 return player ? player->IsPlaying() : false; | 192 return player ? player->IsPlaying() : false; |
| 197 } | 193 } |
| 198 | 194 |
| 199 void ContentVideoView::SeekTo(JNIEnv*, jobject obj, jint msec) { | 195 void ContentVideoView::SeekTo(JNIEnv*, jobject obj, jint msec) { |
| 200 manager_->FullscreenPlayerSeek(msec); | 196 manager_->FullscreenPlayerSeek(msec); |
| 201 } | 197 } |
| 202 | 198 |
| 203 void ContentVideoView::Play(JNIEnv*, jobject obj) { | 199 void ContentVideoView::Play(JNIEnv*, jobject obj) { |
| 204 CreatePowerSaveBlocker(); | |
| 205 manager_->FullscreenPlayerPlay(); | 200 manager_->FullscreenPlayerPlay(); |
| 206 } | 201 } |
| 207 | 202 |
| 208 void ContentVideoView::Pause(JNIEnv*, jobject obj) { | 203 void ContentVideoView::Pause(JNIEnv*, jobject obj) { |
| 209 power_save_blocker_.reset(); | |
| 210 manager_->FullscreenPlayerPause(); | 204 manager_->FullscreenPlayerPause(); |
| 211 } | 205 } |
| 212 | 206 |
| 213 void ContentVideoView::ExitFullscreen( | 207 void ContentVideoView::ExitFullscreen( |
| 214 JNIEnv*, jobject, jboolean release_media_player) { | 208 JNIEnv*, jobject, jboolean release_media_player) { |
| 215 power_save_blocker_.reset(); | |
| 216 j_content_video_view_.reset(); | 209 j_content_video_view_.reset(); |
| 217 manager_->ExitFullscreen(release_media_player); | 210 manager_->ExitFullscreen(release_media_player); |
| 218 } | 211 } |
| 219 | 212 |
| 220 void ContentVideoView::SetSurface(JNIEnv* env, jobject obj, | 213 void ContentVideoView::SetSurface(JNIEnv* env, jobject obj, |
| 221 jobject surface) { | 214 jobject surface) { |
| 222 manager_->SetVideoSurface( | 215 manager_->SetVideoSurface( |
| 223 gfx::ScopedJavaSurface::AcquireExternalSurface(surface)); | 216 gfx::ScopedJavaSurface::AcquireExternalSurface(surface)); |
| 224 } | 217 } |
| 225 | 218 |
| 226 void ContentVideoView::RequestMediaMetadata(JNIEnv* env, jobject obj) { | 219 void ContentVideoView::RequestMediaMetadata(JNIEnv* env, jobject obj) { |
| 227 base::MessageLoop::current()->PostTask( | 220 base::MessageLoop::current()->PostTask( |
| 228 FROM_HERE, | 221 FROM_HERE, |
| 229 base::Bind(&ContentVideoView::UpdateMediaMetadata, | 222 base::Bind(&ContentVideoView::UpdateMediaMetadata, |
| 230 weak_factory_.GetWeakPtr())); | 223 weak_factory_.GetWeakPtr())); |
| 231 } | 224 } |
| 232 | 225 |
| 233 ScopedJavaLocalRef<jobject> ContentVideoView::GetJavaObject(JNIEnv* env) { | 226 ScopedJavaLocalRef<jobject> ContentVideoView::GetJavaObject(JNIEnv* env) { |
| 234 return j_content_video_view_.get(env); | 227 return j_content_video_view_.get(env); |
| 235 } | 228 } |
| 236 | 229 |
| 237 gfx::NativeView ContentVideoView::GetNativeView() { | |
| 238 JNIEnv* env = AttachCurrentThread(); | |
| 239 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env); | |
| 240 if (content_video_view.is_null()) | |
| 241 return NULL; | |
| 242 | |
| 243 return reinterpret_cast<gfx::NativeView>( | |
| 244 Java_ContentVideoView_getNativeViewAndroid(env, | |
| 245 content_video_view.obj())); | |
| 246 | |
| 247 } | |
| 248 | |
| 249 JavaObjectWeakGlobalRef ContentVideoView::CreateJavaObject() { | 230 JavaObjectWeakGlobalRef ContentVideoView::CreateJavaObject() { |
| 250 ContentViewCoreImpl* content_view_core = manager_->GetContentViewCore(); | 231 ContentViewCoreImpl* content_view_core = manager_->GetContentViewCore(); |
| 251 JNIEnv* env = AttachCurrentThread(); | 232 JNIEnv* env = AttachCurrentThread(); |
| 252 return JavaObjectWeakGlobalRef( | 233 return JavaObjectWeakGlobalRef( |
| 253 env, | 234 env, |
| 254 Java_ContentVideoView_createContentVideoView( | 235 Java_ContentVideoView_createContentVideoView( |
| 255 env, | 236 env, |
| 256 content_view_core->GetJavaObject().obj(), | 237 content_view_core->GetJavaObject().obj(), |
| 257 reinterpret_cast<intptr_t>(this)).obj()); | 238 reinterpret_cast<intptr_t>(this)).obj()); |
| 258 } | 239 } |
| 259 | |
| 260 void ContentVideoView::CreatePowerSaveBlocker() { | |
| 261 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 262 switches::kEnableContentVideoViewPowerSaveBlocker)) { | |
| 263 // In fullscreen Clank reuses the power save blocker attached to the | |
| 264 // container view that was created for embedded video. The WebView cannot | |
| 265 // reuse that so we create a new blocker instead. | |
| 266 if (power_save_blocker_) return; | |
| 267 power_save_blocker_ = PowerSaveBlocker::Create( | |
| 268 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | |
| 269 "Playing video").Pass(); | |
| 270 static_cast<PowerSaveBlockerImpl*>(power_save_blocker_.get())-> | |
| 271 InitDisplaySleepBlocker(GetNativeView()); | |
| 272 } | |
| 273 } | |
| 274 | |
| 275 } // namespace content | 240 } // namespace content |
| OLD | NEW |