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 "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"
14 using base::android::AttachCurrentThread
;
15 using base::android::CheckException
;
16 using base::android::ScopedJavaGlobalRef
;
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
;
27 static jobject
GetSingletonJavaContentVideoView(JNIEnv
*env
, jclass
) {
28 if (g_content_video_view
)
29 return g_content_video_view
->GetJavaObject(env
).Release();
34 bool ContentVideoView::RegisterContentVideoView(JNIEnv
* env
) {
35 return RegisterNativesImpl(env
);
38 bool ContentVideoView::HasContentVideoView() {
39 return g_content_video_view
;
42 ContentVideoView::ContentVideoView(
43 const ScopedJavaLocalRef
<jobject
>& context
,
44 const ScopedJavaLocalRef
<jobject
>& client
,
45 BrowserMediaPlayerManager
* 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;
55 ContentVideoView::~ContentVideoView() {
56 DCHECK(g_content_video_view
);
57 DestroyContentVideoView(true);
58 g_content_video_view
= NULL
;
61 void 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());
68 void 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(),
77 void 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(),
86 void 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(),
95 void 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());
102 void ContentVideoView::OnExitFullscreen() {
103 DestroyContentVideoView(false);
106 void 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());
113 int ContentVideoView::GetVideoWidth(JNIEnv
*, jobject obj
) const {
114 media::MediaPlayerAndroid
* player
= manager_
->GetFullscreenPlayer();
115 return player
? player
->GetVideoWidth() : 0;
118 int ContentVideoView::GetVideoHeight(JNIEnv
*, jobject obj
) const {
119 media::MediaPlayerAndroid
* player
= manager_
->GetFullscreenPlayer();
120 return player
? player
->GetVideoHeight() : 0;
123 int ContentVideoView::GetDurationInMilliSeconds(JNIEnv
*, jobject obj
) const {
124 media::MediaPlayerAndroid
* player
= manager_
->GetFullscreenPlayer();
125 return player
? player
->GetDuration().InMilliseconds() : -1;
128 int ContentVideoView::GetCurrentPosition(JNIEnv
*, jobject obj
) const {
129 media::MediaPlayerAndroid
* player
= manager_
->GetFullscreenPlayer();
130 return player
? player
->GetCurrentTime().InMilliseconds() : 0;
133 bool ContentVideoView::IsPlaying(JNIEnv
*, jobject obj
) {
134 media::MediaPlayerAndroid
* player
= manager_
->GetFullscreenPlayer();
135 return player
? player
->IsPlaying() : false;
138 void ContentVideoView::SeekTo(JNIEnv
*, jobject obj
, jint msec
) {
139 manager_
->FullscreenPlayerSeek(msec
);
142 void ContentVideoView::Play(JNIEnv
*, jobject obj
) {
143 manager_
->FullscreenPlayerPlay();
146 void ContentVideoView::Pause(JNIEnv
*, jobject obj
) {
147 manager_
->FullscreenPlayerPause();
150 void ContentVideoView::ExitFullscreen(
151 JNIEnv
*, jobject
, jboolean release_media_player
) {
152 j_content_video_view_
.reset();
153 manager_
->ExitFullscreen(release_media_player
);
156 void ContentVideoView::SetSurface(JNIEnv
* env
, jobject obj
,
158 manager_
->SetVideoSurface(
159 gfx::ScopedJavaSurface::AcquireExternalSurface(surface
));
162 void 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());
171 ScopedJavaLocalRef
<jobject
> ContentVideoView::GetJavaObject(JNIEnv
* env
) {
172 return j_content_video_view_
.get(env
);
175 void 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();
184 } // namespace content