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 "media/base/android/media_player_listener.h"
      6 
      7 #include "base/android/jni_android.h"
      8 #include "base/bind.h"
      9 #include "base/location.h"
     10 #include "base/logging.h"
     11 #include "base/single_thread_task_runner.h"
     12 #include "jni/MediaPlayerListener_jni.h"
     13 #include "media/base/android/media_player_android.h"
     14 
     15 using base::android::AttachCurrentThread;
     16 using base::android::CheckException;
     17 using base::android::ScopedJavaLocalRef;
     18 
     19 namespace media {
     20 
     21 MediaPlayerListener::MediaPlayerListener(
     22     const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
     23     base::WeakPtr<MediaPlayerAndroid> media_player)
     24     : task_runner_(task_runner),
     25       media_player_(media_player) {
     26   DCHECK(task_runner_.get());
     27   DCHECK(media_player_);
     28 }
     29 
     30 MediaPlayerListener::~MediaPlayerListener() {}
     31 
     32 void MediaPlayerListener::CreateMediaPlayerListener(
     33     jobject context, jobject media_player) {
     34   JNIEnv* env = AttachCurrentThread();
     35   CHECK(env);
     36   if (j_media_player_listener_.is_null()) {
     37     j_media_player_listener_.Reset(Java_MediaPlayerListener_create(
     38         env, reinterpret_cast<intptr_t>(this), context, media_player));
     39   }
     40 }
     41 
     42 
     43 void MediaPlayerListener::ReleaseMediaPlayerListenerResources() {
     44   JNIEnv* env = AttachCurrentThread();
     45   CHECK(env);
     46   if (!j_media_player_listener_.is_null()) {
     47     Java_MediaPlayerListener_releaseResources(
     48         env, j_media_player_listener_.obj());
     49   }
     50   j_media_player_listener_.Reset();
     51 }
     52 
     53 void MediaPlayerListener::OnMediaError(
     54     JNIEnv* /* env */, jobject /* obj */, jint error_type) {
     55   task_runner_->PostTask(FROM_HERE, base::Bind(
     56       &MediaPlayerAndroid::OnMediaError, media_player_, error_type));
     57 }
     58 
     59 void MediaPlayerListener::OnVideoSizeChanged(
     60     JNIEnv* /* env */, jobject /* obj */, jint width, jint height) {
     61   task_runner_->PostTask(FROM_HERE, base::Bind(
     62       &MediaPlayerAndroid::OnVideoSizeChanged, media_player_,
     63       width, height));
     64 }
     65 
     66 void MediaPlayerListener::OnBufferingUpdate(
     67     JNIEnv* /* env */, jobject /* obj */, jint percent) {
     68   task_runner_->PostTask(FROM_HERE, base::Bind(
     69       &MediaPlayerAndroid::OnBufferingUpdate, media_player_, percent));
     70 }
     71 
     72 void MediaPlayerListener::OnPlaybackComplete(
     73     JNIEnv* /* env */, jobject /* obj */) {
     74   task_runner_->PostTask(FROM_HERE, base::Bind(
     75       &MediaPlayerAndroid::OnPlaybackComplete, media_player_));
     76 }
     77 
     78 void MediaPlayerListener::OnSeekComplete(
     79     JNIEnv* /* env */, jobject /* obj */) {
     80   task_runner_->PostTask(FROM_HERE, base::Bind(
     81       &MediaPlayerAndroid::OnSeekComplete, media_player_));
     82 }
     83 
     84 void MediaPlayerListener::OnMediaPrepared(
     85     JNIEnv* /* env */, jobject /* obj */) {
     86   task_runner_->PostTask(FROM_HERE, base::Bind(
     87       &MediaPlayerAndroid::OnMediaPrepared, media_player_));
     88 }
     89 
     90 void MediaPlayerListener::OnMediaInterrupted(
     91     JNIEnv* /* env */, jobject /* obj */) {
     92   task_runner_->PostTask(FROM_HERE, base::Bind(
     93       &MediaPlayerAndroid::OnMediaInterrupted, media_player_));
     94 }
     95 
     96 bool MediaPlayerListener::RegisterMediaPlayerListener(JNIEnv* env) {
     97   bool ret = RegisterNativesImpl(env);
     98   DCHECK(g_MediaPlayerListener_clazz);
     99   return ret;
    100 }
    101 
    102 }  // namespace media
    103