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 #ifndef MEDIA_BASE_ANDROID_MEDIA_PLAYER_LISTENER_H_ 6 #define MEDIA_BASE_ANDROID_MEDIA_PLAYER_LISTENER_H_ 7 8 #include <jni.h> 9 10 #include "base/android/scoped_java_ref.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/memory/weak_ptr.h" 13 14 namespace base { 15 class MessageLoopProxy; 16 } 17 18 namespace media { 19 20 class MediaPlayerBridge; 21 22 // Acts as a thread proxy between java MediaPlayerListener object and 23 // MediaPlayerBridge so that callbacks are posted onto the UI thread. 24 class MediaPlayerListener { 25 public: 26 // Construct a native MediaPlayerListener object. Callbacks from the java 27 // side object will be forwarded to |media_player| by posting a task on the 28 // |message_loop|. 29 MediaPlayerListener( 30 const scoped_refptr<base::MessageLoopProxy>& message_loop, 31 base::WeakPtr<MediaPlayerBridge> media_player); 32 virtual ~MediaPlayerListener(); 33 34 // Called by the Java MediaPlayerListener and mirrored to corresponding 35 // callbacks. 36 void OnMediaError(JNIEnv* /* env */, jobject /* obj */, jint error_type); 37 void OnVideoSizeChanged(JNIEnv* /* env */, jobject /* obj */, 38 jint width, jint height); 39 void OnBufferingUpdate(JNIEnv* /* env */, jobject /* obj */, jint percent); 40 void OnPlaybackComplete(JNIEnv* /* env */, jobject /* obj */); 41 void OnSeekComplete(JNIEnv* /* env */, jobject /* obj */); 42 void OnMediaPrepared(JNIEnv* /* env */, jobject /* obj */); 43 void OnMediaInterrupted(JNIEnv* /* env */, jobject /* obj */); 44 45 // Create a Java MediaPlayerListener object. 46 void CreateMediaPlayerListener(jobject context, jobject media_player_bridge); 47 void ReleaseMediaPlayerListenerResources(); 48 49 // Register MediaPlayerListener in the system library loader. 50 static bool RegisterMediaPlayerListener(JNIEnv* env); 51 52 private: 53 // The message loop where |media_player_| lives. 54 scoped_refptr<base::MessageLoopProxy> message_loop_; 55 56 // The MediaPlayerBridge object all the callbacks should be send to. 57 base::WeakPtr<MediaPlayerBridge> media_player_; 58 59 base::android::ScopedJavaGlobalRef<jobject> j_media_player_listener_; 60 61 DISALLOW_COPY_AND_ASSIGN(MediaPlayerListener); 62 }; 63 64 } // namespace media 65 66 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_LISTENER_H_ 67