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