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