This sets up API to release OutputSurface from LTHClient.
[chromium-blink-merge.git] / content / browser / android / content_video_view.cc
blob8a9eb4298b10848f5ef4681a1de12d339c415a08
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 "base/metrics/histogram.h"
11 #include "content/browser/android/content_view_core_impl.h"
12 #include "content/browser/media/android/browser_media_player_manager.h"
13 #include "content/browser/power_save_blocker_impl.h"
14 #include "content/common/android/surface_texture_peer.h"
15 #include "content/public/browser/user_metrics.h"
16 #include "content/public/common/content_switches.h"
17 #include "jni/ContentVideoView_jni.h"
19 using base::android::AttachCurrentThread;
20 using base::android::CheckException;
21 using base::android::ScopedJavaGlobalRef;
22 using base::UserMetricsAction;
23 using content::RecordAction;
25 namespace content {
27 namespace {
28 // There can only be one content video view at a time, this holds onto that
29 // singleton instance.
30 ContentVideoView* g_content_video_view = NULL;
32 } // namespace
34 static ScopedJavaLocalRef<jobject> GetSingletonJavaContentVideoView(
35 JNIEnv* env,
36 const JavaParamRef<jclass>&) {
37 if (g_content_video_view)
38 return g_content_video_view->GetJavaObject(env);
39 else
40 return ScopedJavaLocalRef<jobject>();
43 bool ContentVideoView::RegisterContentVideoView(JNIEnv* env) {
44 return RegisterNativesImpl(env);
47 ContentVideoView* ContentVideoView::GetInstance() {
48 return g_content_video_view;
51 ContentVideoView::ContentVideoView(
52 BrowserMediaPlayerManager* manager)
53 : manager_(manager),
54 weak_factory_(this) {
55 DCHECK(!g_content_video_view);
56 j_content_video_view_ = CreateJavaObject();
57 g_content_video_view = this;
60 ContentVideoView::~ContentVideoView() {
61 DCHECK(g_content_video_view);
62 JNIEnv* env = AttachCurrentThread();
63 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
64 if (!content_video_view.is_null()) {
65 Java_ContentVideoView_destroyContentVideoView(env,
66 content_video_view.obj(), true);
67 j_content_video_view_.reset();
69 g_content_video_view = NULL;
72 void ContentVideoView::OpenVideo() {
73 JNIEnv* env = AttachCurrentThread();
74 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
75 if (!content_video_view.is_null()) {
76 Java_ContentVideoView_openVideo(env, content_video_view.obj());
80 void ContentVideoView::OnMediaPlayerError(int error_type) {
81 JNIEnv* env = AttachCurrentThread();
82 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
83 if (!content_video_view.is_null()) {
84 Java_ContentVideoView_onMediaPlayerError(env, content_video_view.obj(),
85 error_type);
89 void ContentVideoView::OnVideoSizeChanged(int width, int height) {
90 JNIEnv* env = AttachCurrentThread();
91 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
92 if (!content_video_view.is_null()) {
93 Java_ContentVideoView_onVideoSizeChanged(env, content_video_view.obj(),
94 width, height);
98 void ContentVideoView::OnBufferingUpdate(int percent) {
99 JNIEnv* env = AttachCurrentThread();
100 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
101 if (!content_video_view.is_null()) {
102 Java_ContentVideoView_onBufferingUpdate(env, content_video_view.obj(),
103 percent);
107 void ContentVideoView::OnPlaybackComplete() {
108 JNIEnv* env = AttachCurrentThread();
109 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
110 if (!content_video_view.is_null()) {
111 Java_ContentVideoView_onPlaybackComplete(env, content_video_view.obj());
115 void ContentVideoView::OnExitFullscreen() {
116 JNIEnv* env = AttachCurrentThread();
117 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
118 if (!content_video_view.is_null())
119 Java_ContentVideoView_onExitFullscreen(env, content_video_view.obj());
122 void ContentVideoView::RecordFullscreenPlayback(
123 JNIEnv*, jobject, bool is_portrait_video, bool is_orientation_portrait) {
124 UMA_HISTOGRAM_BOOLEAN("MobileFullscreenVideo.OrientationPortrait",
125 is_orientation_portrait);
126 UMA_HISTOGRAM_BOOLEAN("MobileFullscreenVideo.VideoPortrait",
127 is_portrait_video);
130 void ContentVideoView::RecordExitFullscreenPlayback(
131 JNIEnv*, jobject, bool is_portrait_video,
132 long playback_duration_in_milliseconds_before_orientation_change,
133 long playback_duration_in_milliseconds_after_orientation_change) {
134 bool orientation_changed = (
135 playback_duration_in_milliseconds_after_orientation_change != 0);
136 if (is_portrait_video) {
137 UMA_HISTOGRAM_COUNTS(
138 "MobileFullscreenVideo.PortraitDuration",
139 playback_duration_in_milliseconds_before_orientation_change);
140 UMA_HISTOGRAM_COUNTS(
141 "MobileFullscreenVideo.PortraitRotation", orientation_changed);
142 if (orientation_changed) {
143 UMA_HISTOGRAM_COUNTS(
144 "MobileFullscreenVideo.DurationAfterPotraitRotation",
145 playback_duration_in_milliseconds_after_orientation_change);
147 } else {
148 UMA_HISTOGRAM_COUNTS(
149 "MobileFullscreenVideo.LandscapeDuration",
150 playback_duration_in_milliseconds_before_orientation_change);
151 UMA_HISTOGRAM_COUNTS(
152 "MobileFullscreenVideo.LandscapeRotation", orientation_changed);
156 void ContentVideoView::UpdateMediaMetadata() {
157 JNIEnv* env = AttachCurrentThread();
158 ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
159 if (content_video_view.is_null())
160 return;
162 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
163 if (player && player->IsPlayerReady()) {
164 Java_ContentVideoView_onUpdateMediaMetadata(
165 env, content_video_view.obj(), player->GetVideoWidth(),
166 player->GetVideoHeight(),
167 static_cast<int>(player->GetDuration().InMilliseconds()),
168 player->CanPause(),player->CanSeekForward(), player->CanSeekBackward());
172 bool ContentVideoView::IsPlaying(JNIEnv*, jobject obj) {
173 media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
174 return player ? player->IsPlaying() : false;
177 void ContentVideoView::ExitFullscreen(
178 JNIEnv*, jobject, jboolean release_media_player) {
179 j_content_video_view_.reset();
180 manager_->ExitFullscreen(release_media_player);
183 void ContentVideoView::SetSurface(JNIEnv* env, jobject obj,
184 jobject surface) {
185 manager_->SetVideoSurface(
186 gfx::ScopedJavaSurface::AcquireExternalSurface(surface));
189 void ContentVideoView::RequestMediaMetadata(JNIEnv* env, jobject obj) {
190 base::MessageLoop::current()->PostTask(
191 FROM_HERE,
192 base::Bind(&ContentVideoView::UpdateMediaMetadata,
193 weak_factory_.GetWeakPtr()));
196 ScopedJavaLocalRef<jobject> ContentVideoView::GetJavaObject(JNIEnv* env) {
197 return j_content_video_view_.get(env);
200 JavaObjectWeakGlobalRef ContentVideoView::CreateJavaObject() {
201 ContentViewCore* content_view_core = manager_->GetContentViewCore();
202 JNIEnv* env = AttachCurrentThread();
204 base::android::ScopedJavaLocalRef<jobject> j_content_view_core =
205 content_view_core->GetJavaObject();
206 if (j_content_view_core.is_null())
207 return JavaObjectWeakGlobalRef(env, nullptr);
209 return JavaObjectWeakGlobalRef(
210 env,
211 Java_ContentVideoView_createContentVideoView(
212 env,
213 j_content_view_core.obj(),
214 reinterpret_cast<intptr_t>(this)).obj());
216 } // namespace content