content_video_view.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
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.
4
5#include "content/browser/android/content_video_view.h"
6
7#include "base/command_line.h"
8#include "base/logging.h"
9#include "content/browser/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"
13
14using base::android::AttachCurrentThread;
15using base::android::CheckException;
16using base::android::ScopedJavaGlobalRef;
17
18namespace content {
19
20namespace {
21// There can only be one content video view at a time, this holds onto that
22// singleton instance.
23ContentVideoView* g_content_video_view = NULL;
24
25}  // namespace
26
27static 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
34bool ContentVideoView::RegisterContentVideoView(JNIEnv* env) {
35  return RegisterNativesImpl(env);
36}
37
38bool ContentVideoView::HasContentVideoView() {
39  return g_content_video_view;
40}
41
42ContentVideoView::ContentVideoView(
43    const ScopedJavaLocalRef<jobject>& context,
44    const ScopedJavaLocalRef<jobject>& client,
45    BrowserMediaPlayerManager* 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;
53}
54
55ContentVideoView::~ContentVideoView() {
56  DCHECK(g_content_video_view);
57  DCHECK(!GetJavaObject(AttachCurrentThread()).obj());
58  g_content_video_view = NULL;
59}
60
61void 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());
66}
67
68void 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(),
73        error_type);
74  }
75}
76
77void 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(),
82        width, height);
83  }
84}
85
86void 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(),
91        percent);
92  }
93}
94
95void 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());
100}
101
102void ContentVideoView::OnExitFullscreen() {
103  JNIEnv *env = AttachCurrentThread();
104  ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
105  if (!content_video_view.is_null()) {
106    Java_ContentVideoView_destroyContentVideoView(env,
107        content_video_view.obj());
108    j_content_video_view_.reset();
109  }
110}
111
112void ContentVideoView::UpdateMediaMetadata() {
113  JNIEnv *env = AttachCurrentThread();
114  ScopedJavaLocalRef<jobject> content_video_view = GetJavaObject(env);
115  if (!content_video_view.is_null())
116    UpdateMediaMetadata(env, content_video_view.obj());
117}
118
119int ContentVideoView::GetVideoWidth(JNIEnv*, jobject obj) const {
120  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
121  return player ? player->GetVideoWidth() : 0;
122}
123
124int ContentVideoView::GetVideoHeight(JNIEnv*, jobject obj) const {
125  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
126  return player ? player->GetVideoHeight() : 0;
127}
128
129int ContentVideoView::GetDurationInMilliSeconds(JNIEnv*, jobject obj) const {
130  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
131  return player ? player->GetDuration().InMilliseconds() : -1;
132}
133
134int ContentVideoView::GetCurrentPosition(JNIEnv*, jobject obj) const {
135  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
136  return player ? player->GetCurrentTime().InMilliseconds() : 0;
137}
138
139bool ContentVideoView::IsPlaying(JNIEnv*, jobject obj) {
140  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
141  return player ? player->IsPlaying() : false;
142}
143
144void ContentVideoView::SeekTo(JNIEnv*, jobject obj, jint msec) {
145  manager_->FullscreenPlayerSeek(msec);
146}
147
148void ContentVideoView::Play(JNIEnv*, jobject obj) {
149  manager_->FullscreenPlayerPlay();
150}
151
152void ContentVideoView::Pause(JNIEnv*, jobject obj) {
153  manager_->FullscreenPlayerPause();
154}
155
156void ContentVideoView::ExitFullscreen(
157    JNIEnv*, jobject, jboolean release_media_player) {
158  j_content_video_view_.reset();
159  manager_->ExitFullscreen(release_media_player);
160}
161
162void ContentVideoView::SetSurface(JNIEnv* env, jobject obj,
163                                  jobject surface) {
164  manager_->SetVideoSurface(
165      gfx::ScopedJavaSurface::AcquireExternalSurface(surface));
166}
167
168void ContentVideoView::UpdateMediaMetadata(JNIEnv* env, jobject obj) {
169  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
170  if (player && player->IsPlayerReady())
171    Java_ContentVideoView_onUpdateMediaMetadata(
172        env, obj, player->GetVideoWidth(), player->GetVideoHeight(),
173        player->GetDuration().InMilliseconds(), player->CanPause(),
174        player->CanSeekForward(), player->CanSeekBackward());
175}
176
177ScopedJavaLocalRef<jobject> ContentVideoView::GetJavaObject(JNIEnv* env) {
178  return j_content_video_view_.get(env);
179}
180
181}  // namespace content
182