Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ | 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ |
| 6 #define MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ | 6 #define MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ |
| 7 | 7 |
| 8 #include <jni.h> | 8 #include <jni.h> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/time.h" | 16 #include "base/time.h" |
| 17 #include "base/timer.h" | |
| 15 | 18 |
| 16 namespace media { | 19 namespace media { |
| 20 class CookieGetter; | |
| 21 } | |
| 22 | |
| 23 namespace media { | |
| 24 | |
| 25 class MediaPlayerBridgeManager; | |
| 26 class MediaPlayerListener; | |
| 17 | 27 |
| 18 // This class serves as a bridge for native code to call java functions inside | 28 // 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 | 29 // android mediaplayer class. For more information on android mediaplayer, check |
| 20 // http://developer.android.com/reference/android/media/MediaPlayer.html | 30 // http://developer.android.com/reference/android/media/MediaPlayer.html |
| 21 // To use this class, follow the state diagram listed in the above url. | 31 // The actual android mediaplayer instance is created lazily when Start(), |
| 22 // Here is the normal work flow for this class: | 32 // Pause(), SeekTo() gets called. As a result, media information may not |
| 23 // 1. Call SetDataSource() to set the media url. | 33 // be available until one of those operations is performed. After that, we |
| 24 // 2. Call Prepare() to prepare the player for playback. This is a non | 34 // will cache those information in case the mediaplayer gets released. |
| 25 // blocking call. | 35 class MediaPlayerBridge : public base::SupportsWeakPtr<MediaPlayerBridge> { |
|
scherkus (not reviewing)
2012/09/11 11:56:25
can you use WeakPtrFactory weak_this_ instead of i
qinmin
2012/09/12 23:12:52
Done.
| |
| 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: | 36 public: |
| 30 // Error types for MediaErrorCB. | 37 // Error types for MediaErrorCB. |
| 31 enum MediaErrorType { | 38 enum MediaErrorType { |
| 32 MEDIA_ERROR_UNKNOWN, | 39 MEDIA_ERROR_UNKNOWN, |
| 33 MEDIA_ERROR_SERVER_DIED, | 40 MEDIA_ERROR_SERVER_DIED, |
| 34 MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK, | 41 MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK, |
| 35 MEDIA_ERROR_INVALID_CODE, | 42 MEDIA_ERROR_INVALID_CODE, |
| 36 }; | 43 }; |
| 37 | 44 |
| 38 // Info types for MediaInfoCB. | 45 // Callback when error happens. Args: player ID, error type. |
| 39 enum MediaInfoType { | 46 typedef base::Callback<void(int, int)> MediaErrorCB; |
| 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, | |
| 47 }; | |
| 48 | 47 |
| 49 // Callback when video info is received. Args: info type. | 48 // Callback when video size has changed. Args: player ID, width, height. |
| 50 typedef base::Callback<void(int)> MediaInfoCB; | 49 typedef base::Callback<void(int, int, int)> VideoSizeChangedCB; |
| 51 | 50 |
| 52 // Callback when error happens. Args: error type. | 51 // Callback when buffering has changed. Args: player ID, percentage |
| 53 typedef base::Callback<void(int)> MediaErrorCB; | 52 // of the media. |
| 53 typedef base::Callback<void(int, int)> BufferingUpdateCB; | |
| 54 | 54 |
| 55 // Callback when video size has changed. Args: width, height. | 55 // Callback when player got prepared. Args: player ID, duration of the media. |
| 56 typedef base::Callback<void(int,int)> VideoSizeChangedCB; | 56 typedef base::Callback<void(int, base::TimeDelta)> MediaPreparedCB; |
| 57 | 57 |
| 58 // Callback when buffering has changed. Args: percentage of the media. | 58 // Callbacks when seek completed. Args: player ID, current time. |
| 59 typedef base::Callback<void(int)> BufferingUpdateCB; | 59 typedef base::Callback<void(int, base::TimeDelta)> SeekCompleteCB; |
| 60 | 60 |
| 61 MediaPlayerBridge(); | 61 // Callbacks when playback completed. Args: player ID. |
| 62 typedef base::Callback<void(int)> PlaybackCompleteCB; | |
| 63 | |
| 64 // Callback when time update messages need to be sent. Args: player ID, | |
| 65 // current time. | |
| 66 typedef base::Callback<void(int, base::TimeDelta)> TimeUpdateCB; | |
| 67 | |
| 68 // Construct a MediaPlayerBridge object with all the needed media player | |
| 69 // callbacks. This object needs to call |manager|'s RequestMediaResources() | |
| 70 // before decoding the media stream. This allows |manager| to track | |
| 71 // unused resources and free them when needed. On the other hand, it needs | |
| 72 // to call ReleaseMediaResources() when it is done with decoding. | |
| 73 MediaPlayerBridge(int player_id, | |
| 74 const std::string& url, | |
| 75 media::CookieGetter* cookies_retriever, | |
| 76 bool hide_url_log, | |
| 77 media::MediaPlayerBridgeManager* manager, | |
| 78 const MediaErrorCB& media_error_cb, | |
| 79 const VideoSizeChangedCB& video_size_changed_cb, | |
| 80 const BufferingUpdateCB& buffering_update_cb, | |
| 81 const MediaPreparedCB& media_prepared_cb, | |
| 82 const PlaybackCompleteCB& playback_complete_cb, | |
| 83 const SeekCompleteCB& seek_complete_cb, | |
| 84 const TimeUpdateCB& time_update_cb); | |
| 62 ~MediaPlayerBridge(); | 85 ~MediaPlayerBridge(); |
| 63 | 86 |
| 64 typedef std::map<std::string, std::string> HeadersMap; | 87 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); | |
| 68 | 88 |
| 69 void SetVideoSurface(jobject surface); | 89 void SetVideoSurface(jobject surface); |
| 70 | 90 |
| 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); | |
| 79 | |
| 80 // Start playing the media. | 91 // Start playing the media. |
| 81 void Start(const base::Closure& playback_complete_cb); | 92 void Start(); |
| 82 | 93 |
| 83 // Pause the media. | 94 // Pause the media. |
| 84 void Pause(); | 95 void Pause(); |
| 85 | 96 |
| 86 // Stop the media playback. Needs to call Prepare() again to play the media. | |
| 87 void Stop(); | |
| 88 | |
| 89 // Seek to a particular position. When succeeds, OnSeekComplete() will be | 97 // Seek to a particular position. When succeeds, OnSeekComplete() will be |
| 90 // called. Otherwise, nothing will happen. | 98 // called. Otherwise, nothing will happen. |
| 91 void SeekTo(base::TimeDelta time, const base::Closure& seek_complete_cb); | 99 void SeekTo(base::TimeDelta time); |
| 92 | 100 |
| 93 // Reset the player. Needs to call SetDataSource() again after this call. | 101 // Release the player resources. |
| 94 void Reset(); | 102 void Release(); |
| 95 | 103 |
| 96 // Set the player volume. | 104 // Set the player volume. |
| 97 void SetVolume(float leftVolume, float rightVolume); | 105 void SetVolume(float leftVolume, float rightVolume); |
| 98 | 106 |
| 99 // Get the media information from the player. | 107 // Get the media information from the player. |
| 100 int GetVideoWidth(); | 108 int GetVideoWidth(); |
| 101 int GetVideoHeight(); | 109 int GetVideoHeight(); |
| 102 base::TimeDelta GetCurrentTime(); | 110 base::TimeDelta GetCurrentTime(); |
| 103 base::TimeDelta GetDuration(); | 111 base::TimeDelta GetDuration(); |
| 104 bool IsPlaying(); | 112 bool IsPlaying(); |
| 105 | 113 |
| 106 // Get metadata from the media. | 114 // Get metadata from the media. |
| 107 void GetMetadata(bool* can_pause, | 115 void GetMetadata(); |
| 108 bool* can_seek_forward, | |
| 109 bool* can_seek_backward); | |
| 110 | 116 |
| 111 // Set the device to stay awake when player is playing. | 117 // Called by the timer to check for current time routinely and generates |
| 112 void SetStayAwakeWhilePlaying(); | 118 // time update events. |
| 119 void DoTimeUpdate(); | |
| 113 | 120 |
| 114 // Called by the Java MediaPlayerListener and mirrored to corresponding | 121 // Called by the MediaPlayerListener and mirrored to corresponding |
| 115 // callbacks. | 122 // callbacks. |
| 116 void OnMediaError(JNIEnv* /* env */, jobject /* obj */, jint error_type); | 123 void OnMediaError(int error_type); |
| 117 void OnMediaInfo(JNIEnv* /* env */, jobject /* obj */, jint info_type); | 124 void OnVideoSizeChanged(int width, int height); |
| 118 void OnVideoSizeChanged(JNIEnv* /* env */, jobject /* obj */, | 125 void OnBufferingUpdate(int percent); |
| 119 jint width, jint height); | 126 void OnPlaybackComplete(); |
| 120 void OnBufferingUpdate(JNIEnv* /* env */, jobject /* obj */, jint percent); | 127 void OnSeekComplete(); |
| 121 void OnPlaybackComplete(JNIEnv* /* env */, jobject /* obj */); | 128 void OnMediaPrepared(); |
| 122 void OnSeekComplete(JNIEnv* /* env */, jobject /* obj */); | |
| 123 void OnMediaPrepared(JNIEnv* /* env */, jobject /* obj */); | |
| 124 | 129 |
| 125 // Register MediaPlayerListener in the system library loader. | 130 // Prepare the player for playback, asynchronously. When succeeds, |
| 126 static bool RegisterMediaPlayerListener(JNIEnv* env); | 131 // OnMediaPrepared() will be called. Otherwise, OnMediaError() will |
| 132 // be called with an error type. | |
| 133 void Prepare(); | |
| 134 | |
| 135 // Callback function passed to |cookies_retriever_|. | |
| 136 void GetCookiesCallback(std::string cookies); | |
| 137 | |
| 138 int player_id() { return player_id_; } | |
| 139 bool can_pause() { return can_pause_; } | |
| 140 bool can_seek_forward() { return can_seek_forward_; } | |
| 141 bool can_seek_backward() { return can_seek_backward_; } | |
| 142 bool prepared() { return prepared_; } | |
| 127 | 143 |
| 128 private: | 144 private: |
| 129 void CallVoidMethod(std::string method_name); | 145 void CallVoidMethod(std::string method_name); |
| 130 int CallIntMethod(std::string method_name); | 146 int CallIntMethod(std::string method_name); |
| 131 | 147 |
| 148 void SetDataSource(const std::string& url, | |
| 149 const std::string& cookies, | |
| 150 bool hide_url_log); | |
| 151 | |
| 152 // Create the actual android mediaplayer. | |
|
scherkus (not reviewing)
2012/09/11 11:56:25
s/mediaplayer/media player/
qinmin
2012/09/12 23:12:52
Done.
| |
| 153 void InitializePlayer(); | |
| 154 | |
| 155 // Functions that implements media player control. | |
| 156 void StartInternal(); | |
| 157 void PauseInternal(); | |
| 158 void SeekInternal(base::TimeDelta time); | |
| 159 | |
| 132 // Callbacks when events are received. | 160 // Callbacks when events are received. |
| 133 MediaInfoCB media_info_cb_; | |
| 134 MediaErrorCB media_error_cb_; | 161 MediaErrorCB media_error_cb_; |
| 135 VideoSizeChangedCB video_size_changed_cb_; | 162 VideoSizeChangedCB video_size_changed_cb_; |
| 136 BufferingUpdateCB buffering_update_cb_; | 163 BufferingUpdateCB buffering_update_cb_; |
| 137 base::Closure playback_complete_cb_; | 164 MediaPreparedCB media_prepared_cb_; |
| 138 base::Closure seek_complete_cb_; | 165 PlaybackCompleteCB playback_complete_cb_; |
| 139 base::Closure media_prepared_cb_; | 166 SeekCompleteCB seek_complete_cb_; |
| 167 | |
| 168 // Callbacks when timer events are received. | |
| 169 TimeUpdateCB time_update_cb_; | |
| 170 | |
| 171 // Player ID assigned to this player. | |
| 172 int player_id_; | |
| 173 | |
| 174 // Whether the player is prepared for playback. | |
| 175 bool prepared_; | |
| 176 | |
| 177 // Pending play event while player is preparing. | |
| 178 bool pending_play_; | |
| 179 | |
| 180 // Pending seek time while player is preparing. | |
| 181 base::TimeDelta pending_seek_; | |
| 182 | |
| 183 // Url for playback. | |
| 184 std::string url_; | |
| 185 | |
| 186 // Whether cookies are available. | |
| 187 bool has_cookies_; | |
| 188 | |
| 189 // Hide url log from media player. | |
| 190 bool hide_url_log_; | |
| 191 | |
| 192 // Stats about the media. | |
| 193 base::TimeDelta duration_; | |
| 194 int width_; | |
| 195 int height_; | |
| 196 | |
| 197 // Meta data about actions can be taken. | |
| 198 bool can_pause_; | |
| 199 bool can_seek_forward_; | |
| 200 bool can_seek_backward_; | |
| 201 | |
| 202 // Cookies for |url_| | |
| 203 std::string cookies_; | |
| 204 | |
| 205 // Resource manager for all the mediaplayers. | |
|
scherkus (not reviewing)
2012/09/11 11:56:25
s/mediaplayers/media players/
qinmin
2012/09/12 23:12:52
Done.
| |
| 206 media::MediaPlayerBridgeManager* manager_; | |
| 207 | |
| 208 // Object for retrieving cookies for this media player. | |
| 209 scoped_ptr<media::CookieGetter> cookie_getter_; | |
| 140 | 210 |
| 141 // Java MediaPlayer class and instance. | 211 // Java MediaPlayer class and instance. |
| 142 base::android::ScopedJavaGlobalRef<jclass> j_media_player_class_; | 212 base::android::ScopedJavaGlobalRef<jclass> j_media_player_class_; |
| 143 base::android::ScopedJavaGlobalRef<jobject> j_media_player_; | 213 base::android::ScopedJavaGlobalRef<jobject> j_media_player_; |
| 144 | 214 |
| 215 base::RepeatingTimer<MediaPlayerBridge> time_update_timer_; | |
| 216 | |
| 217 // Listener object that listens to all the media player events. | |
| 218 scoped_ptr<MediaPlayerListener> listener_; | |
|
scherkus (not reviewing)
2012/09/11 11:56:25
nit: this doesn't need to be a scoped_ptr<> anymor
qinmin
2012/09/12 23:12:52
The native MediaPlayerListener object's should sti
| |
| 219 | |
| 145 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge); | 220 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge); |
| 146 }; | 221 }; |
| 147 | 222 |
| 148 } // namespace media | 223 } // namespace media |
| 149 | 224 |
| 150 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ | 225 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ |
| OLD | NEW |