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