content_video_view.cc revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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/android/jni_android.h"
8#include "base/command_line.h"
9#include "base/logging.h"
10#include "content/browser/android/media_player_manager_impl.h"
11#include "content/common/android/surface_texture_peer.h"
12#include "content/public/common/content_switches.h"
13#include "jni/ContentVideoView_jni.h"
14
15using base::android::AttachCurrentThread;
16using base::android::CheckException;
17using base::android::ScopedJavaGlobalRef;
18
19namespace content {
20
21bool ContentVideoView::RegisterContentVideoView(JNIEnv* env) {
22  return RegisterNativesImpl(env);
23}
24
25ContentVideoView::ContentVideoView(MediaPlayerManagerImpl* manager)
26    : manager_(manager) {
27}
28
29ContentVideoView::~ContentVideoView() {
30  DestroyContentVideoView();
31}
32
33void ContentVideoView::CreateContentVideoView() {
34  if (j_content_video_view_.is_null()) {
35    JNIEnv* env = AttachCurrentThread();
36    j_content_video_view_.Reset(Java_ContentVideoView_createContentVideoView(
37        env, reinterpret_cast<jint>(this)));
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
45void ContentVideoView::DestroyContentVideoView() {
46  if (!j_content_video_view_.is_null()) {
47    Java_ContentVideoView_destroyContentVideoView(AttachCurrentThread());
48    j_content_video_view_.Reset();
49  }
50}
51
52void ContentVideoView::OnMediaPlayerError(int error_type) {
53  if (!j_content_video_view_.is_null()) {
54    Java_ContentVideoView_onMediaPlayerError(AttachCurrentThread(),
55                                             j_content_video_view_.obj(),
56                                             error_type);
57  }
58}
59
60void ContentVideoView::OnVideoSizeChanged(int width, int height) {
61  if (!j_content_video_view_.is_null()) {
62    Java_ContentVideoView_onVideoSizeChanged(AttachCurrentThread(),
63                                             j_content_video_view_.obj(),
64                                             width,
65                                             height);
66  }
67}
68
69void ContentVideoView::OnBufferingUpdate(int percent) {
70  if (!j_content_video_view_.is_null()) {
71    Java_ContentVideoView_onBufferingUpdate(AttachCurrentThread(),
72                                            j_content_video_view_.obj(),
73                                            percent);
74  }
75}
76
77void ContentVideoView::OnPlaybackComplete() {
78  if (!j_content_video_view_.is_null()) {
79    Java_ContentVideoView_onPlaybackComplete(AttachCurrentThread(),
80                                             j_content_video_view_.obj());
81  }
82}
83
84void ContentVideoView::UpdateMediaMetadata() {
85  if (!j_content_video_view_.is_null())
86    UpdateMediaMetadata(AttachCurrentThread(), j_content_video_view_.obj());
87}
88
89int ContentVideoView::GetVideoWidth(JNIEnv*, jobject obj) const {
90  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
91  return player ? player->GetVideoWidth() : 0;
92}
93
94int ContentVideoView::GetVideoHeight(JNIEnv*, jobject obj) const {
95  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
96  return player ? player->GetVideoHeight() : 0;
97}
98
99int ContentVideoView::GetDurationInMilliSeconds(JNIEnv*, jobject obj) const {
100  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
101  return player ? player->GetDuration().InMilliseconds() : -1;
102}
103
104int ContentVideoView::GetCurrentPosition(JNIEnv*, jobject obj) const {
105  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
106  return player ? player->GetCurrentTime().InMilliseconds() : 0;
107}
108
109bool ContentVideoView::IsPlaying(JNIEnv*, jobject obj) {
110  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
111  return player ? player->IsPlaying() : false;
112}
113
114void ContentVideoView::SeekTo(JNIEnv*, jobject obj, jint msec) {
115  manager_->FullscreenPlayerSeek(msec);
116}
117
118void ContentVideoView::Play(JNIEnv*, jobject obj) {
119  manager_->FullscreenPlayerPlay();
120}
121
122void ContentVideoView::Pause(JNIEnv*, jobject obj) {
123  manager_->FullscreenPlayerPause();
124}
125
126void ContentVideoView::ExitFullscreen(
127    JNIEnv*, jobject, jboolean release_media_player) {
128  manager_->ExitFullscreen(release_media_player);
129  j_content_video_view_.Reset();
130}
131
132void ContentVideoView::SetSurface(JNIEnv* env, jobject obj,
133                                  jobject surface) {
134  manager_->SetVideoSurface(surface);
135}
136
137void ContentVideoView::UpdateMediaMetadata(JNIEnv* env, jobject obj) {
138  media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
139  if (player && player->IsPlayerReady())
140    Java_ContentVideoView_updateMediaMetadata(
141        env, obj, player->GetVideoWidth(), player->GetVideoHeight(),
142        player->GetDuration().InMilliseconds(), player->CanPause(),
143        player->CanSeekForward(), player->CanSeekBackward());
144}
145
146}  // namespace content
147