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
14namespace base {
15class SingleThreadTaskRunner;
16}
17
18namespace media {
19
20class MediaPlayerAndroid;
21
22// Acts as a thread proxy between java MediaPlayerListener object and
23// MediaPlayerAndroid so that callbacks are posted onto the UI thread.
24class 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  // |task_runner|.
29  MediaPlayerListener(
30      const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
31      base::WeakPtr<MediaPlayerAndroid> 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 and listens to all the media
46  // related events from system and |media_player|. If |media_player| is NULL,
47  // this class only listens to system events.
48  void CreateMediaPlayerListener(jobject context, jobject media_player);
49  void ReleaseMediaPlayerListenerResources();
50
51  // Register MediaPlayerListener in the system library loader.
52  static bool RegisterMediaPlayerListener(JNIEnv* env);
53
54 private:
55  // The message loop where |media_player_| lives.
56  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
57
58  // The MediaPlayerAndroid object all the callbacks should be sent to.
59  base::WeakPtr<MediaPlayerAndroid> media_player_;
60
61  base::android::ScopedJavaGlobalRef<jobject> j_media_player_listener_;
62
63  DISALLOW_COPY_AND_ASSIGN(MediaPlayerListener);
64};
65
66}  // namespace media
67
68#endif  // MEDIA_BASE_ANDROID_MEDIA_PLAYER_LISTENER_H_
69