Home | History | Annotate | Download | only in android
      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 
     14 using base::android::AttachCurrentThread;
     15 using base::android::CheckException;
     16 using base::android::ScopedJavaGlobalRef;
     17 
     18 namespace content {
     19 
     20 namespace {
     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;
     24 
     25 }  // namespace
     26 
     27 static 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 
     34 bool ContentVideoView::RegisterContentVideoView(JNIEnv* env) {
     35   return RegisterNativesImpl(env);
     36 }
     37 
     38 bool ContentVideoView::HasContentVideoView() {
     39   return g_content_video_view;
     40 }
     41 
     42 ContentVideoView::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 
     55 ContentVideoView::~ContentVideoView() {
     56   DCHECK(g_content_video_view);
     57   DestroyContentVideoView(true);
     58   g_content_video_view = NULL;
     59 }
     60 
     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());
     66 }
     67 
     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(),
     73         error_type);
     74   }
     75 }
     76 
     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(),
     82         width, height);
     83   }
     84 }
     85 
     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(),
     91         percent);
     92   }
     93 }
     94 
     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());
    100 }
    101 
    102 void ContentVideoView::OnExitFullscreen() {
    103   DestroyContentVideoView(false);
    104 }
    105 
    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());
    111 }
    112 
    113 int ContentVideoView::GetVideoWidth(JNIEnv*, jobject obj) const {
    114   media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
    115   return player ? player->GetVideoWidth() : 0;
    116 }
    117 
    118 int ContentVideoView::GetVideoHeight(JNIEnv*, jobject obj) const {
    119   media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
    120   return player ? player->GetVideoHeight() : 0;
    121 }
    122 
    123 int ContentVideoView::GetDurationInMilliSeconds(JNIEnv*, jobject obj) const {
    124   media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
    125   return player ? player->GetDuration().InMilliseconds() : -1;
    126 }
    127 
    128 int ContentVideoView::GetCurrentPosition(JNIEnv*, jobject obj) const {
    129   media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
    130   return player ? player->GetCurrentTime().InMilliseconds() : 0;
    131 }
    132 
    133 bool ContentVideoView::IsPlaying(JNIEnv*, jobject obj) {
    134   media::MediaPlayerAndroid* player = manager_->GetFullscreenPlayer();
    135   return player ? player->IsPlaying() : false;
    136 }
    137 
    138 void ContentVideoView::SeekTo(JNIEnv*, jobject obj, jint msec) {
    139   manager_->FullscreenPlayerSeek(msec);
    140 }
    141 
    142 void ContentVideoView::Play(JNIEnv*, jobject obj) {
    143   manager_->FullscreenPlayerPlay();
    144 }
    145 
    146 void ContentVideoView::Pause(JNIEnv*, jobject obj) {
    147   manager_->FullscreenPlayerPause();
    148 }
    149 
    150 void ContentVideoView::ExitFullscreen(
    151     JNIEnv*, jobject, jboolean release_media_player) {
    152   j_content_video_view_.reset();
    153   manager_->ExitFullscreen(release_media_player);
    154 }
    155 
    156 void ContentVideoView::SetSurface(JNIEnv* env, jobject obj,
    157                                   jobject surface) {
    158   manager_->SetVideoSurface(
    159       gfx::ScopedJavaSurface::AcquireExternalSurface(surface));
    160 }
    161 
    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());
    169 }
    170 
    171 ScopedJavaLocalRef<jobject> ContentVideoView::GetJavaObject(JNIEnv* env) {
    172   return j_content_video_view_.get(env);
    173 }
    174 
    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();
    182   }
    183 }
    184 }  // namespace content
    185