roll skia to 4057
[chromium-blink-merge.git] / media / base / android / media_player_bridge.h
blob66c3fa6c98e8bf852fd8a5c4093b8171c8d98cd5
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.
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_
8 #include <jni.h>
9 #include <map>
10 #include <string>
12 #include "base/android/scoped_java_ref.h"
13 #include "base/callback.h"
14 #include "base/time.h"
16 namespace media {
18 // This class serves as a bridge for native code to call java functions inside
19 // android mediaplayer class. For more information on android mediaplayer, check
20 // http://developer.android.com/reference/android/media/MediaPlayer.html
21 // To use this class, follow the state diagram listed in the above url.
22 // Here is the normal work flow for this class:
23 // 1. Call SetDataSource() to set the media url.
24 // 2. Call Prepare() to prepare the player for playback. This is a non
25 // blocking call.
26 // 3. When Prepare() succeeds, OnMediaPrepared() will get called.
27 // 4. Call Start(), Pause(), SeekTo() to play/pause/seek the media.
28 class MediaPlayerBridge {
29 public:
30 // Error types for MediaErrorCB.
31 enum MediaErrorType {
32 MEDIA_ERROR_UNKNOWN,
33 MEDIA_ERROR_SERVER_DIED,
34 MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK,
35 MEDIA_ERROR_INVALID_CODE,
38 // Info types for MediaInfoCB.
39 enum MediaInfoType {
40 MEDIA_INFO_UNKNOWN,
41 MEDIA_INFO_VIDEO_TRACK_LAGGING,
42 MEDIA_INFO_BUFFERING_START,
43 MEDIA_INFO_BUFFERING_END,
44 MEDIA_INFO_BAD_INTERLEAVING,
45 MEDIA_INFO_NOT_SEEKABLE,
46 MEDIA_INFO_METADATA_UPDATE,
49 // Callback when video info is received. Args: info type.
50 typedef base::Callback<void(int)> MediaInfoCB;
52 // Callback when error happens. Args: error type.
53 typedef base::Callback<void(int)> MediaErrorCB;
55 // Callback when video size has changed. Args: width, height.
56 typedef base::Callback<void(int,int)> VideoSizeChangedCB;
58 // Callback when buffering has changed. Args: percentage of the media.
59 typedef base::Callback<void(int)> BufferingUpdateCB;
61 MediaPlayerBridge();
62 ~MediaPlayerBridge();
64 typedef std::map<std::string, std::string> HeadersMap;
65 void SetDataSource(const std::string& url,
66 const std::string& cookies,
67 bool hide_url_log);
69 void SetVideoSurface(jobject surface);
71 // Prepare the player for playback, asynchronously. When succeeds,
72 // OnMediaPrepared() will be called. Otherwise, OnMediaError() will
73 // be called with an error type.
74 void Prepare(const MediaInfoCB& media_info_cb,
75 const MediaErrorCB& media_error_cb,
76 const VideoSizeChangedCB& video_size_changed_cb,
77 const BufferingUpdateCB& buffering_update_cb,
78 const base::Closure& media_prepared_cb);
80 // Start playing the media.
81 void Start(const base::Closure& playback_complete_cb);
83 // Pause the media.
84 void Pause();
86 // Stop the media playback. Needs to call Prepare() again to play the media.
87 void Stop();
89 // Seek to a particular position. When succeeds, OnSeekComplete() will be
90 // called. Otherwise, nothing will happen.
91 void SeekTo(base::TimeDelta time, const base::Closure& seek_complete_cb);
93 // Reset the player. Needs to call SetDataSource() again after this call.
94 void Reset();
96 // Set the player volume.
97 void SetVolume(float leftVolume, float rightVolume);
99 // Get the media information from the player.
100 int GetVideoWidth();
101 int GetVideoHeight();
102 base::TimeDelta GetCurrentTime();
103 base::TimeDelta GetDuration();
104 bool IsPlaying();
106 // Get metadata from the media.
107 void GetMetadata(bool* can_pause,
108 bool* can_seek_forward,
109 bool* can_seek_backward);
111 // Set the device to stay awake when player is playing.
112 void SetStayAwakeWhilePlaying();
114 // Called by the Java MediaPlayerListener and mirrored to corresponding
115 // callbacks.
116 void OnMediaError(JNIEnv* /* env */, jobject /* obj */, jint error_type);
117 void OnMediaInfo(JNIEnv* /* env */, jobject /* obj */, jint info_type);
118 void OnVideoSizeChanged(JNIEnv* /* env */, jobject /* obj */,
119 jint width, jint height);
120 void OnBufferingUpdate(JNIEnv* /* env */, jobject /* obj */, jint percent);
121 void OnPlaybackComplete(JNIEnv* /* env */, jobject /* obj */);
122 void OnSeekComplete(JNIEnv* /* env */, jobject /* obj */);
123 void OnMediaPrepared(JNIEnv* /* env */, jobject /* obj */);
125 // Register MediaPlayerListener in the system library loader.
126 static bool RegisterMediaPlayerListener(JNIEnv* env);
128 private:
129 void CallVoidMethod(std::string method_name);
130 int CallIntMethod(std::string method_name);
132 // Callbacks when events are received.
133 MediaInfoCB media_info_cb_;
134 MediaErrorCB media_error_cb_;
135 VideoSizeChangedCB video_size_changed_cb_;
136 BufferingUpdateCB buffering_update_cb_;
137 base::Closure playback_complete_cb_;
138 base::Closure seek_complete_cb_;
139 base::Closure media_prepared_cb_;
141 // Java MediaPlayer class and instance.
142 base::android::ScopedJavaGlobalRef<jclass> j_media_player_class_;
143 base::android::ScopedJavaGlobalRef<jobject> j_media_player_;
145 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge);
148 } // namespace media
150 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_