Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: media/base/android/media_player_bridge.h

Issue 10919075: Move android mediaplayer from render process to browser process. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: adding cookie policy check and fix the threading issue for CookieGetterImpl Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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.
26 // 3. When Prepare() succeeds, OnMediaPrepared() will get called.
27 // 4. Call Start(), Pause(), SeekTo() to play/pause/seek the media.
28 class MediaPlayerBridge { 35 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 const std::string& first_party_for_cookies,
76 media::CookieGetter* cookies_retriever,
77 bool hide_url_log,
78 media::MediaPlayerBridgeManager* manager,
79 const MediaErrorCB& media_error_cb,
80 const VideoSizeChangedCB& video_size_changed_cb,
81 const BufferingUpdateCB& buffering_update_cb,
82 const MediaPreparedCB& media_prepared_cb,
83 const PlaybackCompleteCB& playback_complete_cb,
84 const SeekCompleteCB& seek_complete_cb,
85 const TimeUpdateCB& time_update_cb);
62 ~MediaPlayerBridge(); 86 ~MediaPlayerBridge();
63 87
64 typedef std::map<std::string, std::string> HeadersMap; 88 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 89
69 void SetVideoSurface(jobject surface); 90 void SetVideoSurface(jobject surface);
70 91
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. 92 // Start playing the media.
81 void Start(const base::Closure& playback_complete_cb); 93 void Start();
82 94
83 // Pause the media. 95 // Pause the media.
84 void Pause(); 96 void Pause();
85 97
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 98 // Seek to a particular position. When succeeds, OnSeekComplete() will be
90 // called. Otherwise, nothing will happen. 99 // called. Otherwise, nothing will happen.
91 void SeekTo(base::TimeDelta time, const base::Closure& seek_complete_cb); 100 void SeekTo(base::TimeDelta time);
92 101
93 // Reset the player. Needs to call SetDataSource() again after this call. 102 // Release the player resources.
94 void Reset(); 103 void Release();
95 104
96 // Set the player volume. 105 // Set the player volume.
97 void SetVolume(float leftVolume, float rightVolume); 106 void SetVolume(float leftVolume, float rightVolume);
98 107
99 // Get the media information from the player. 108 // Get the media information from the player.
100 int GetVideoWidth(); 109 int GetVideoWidth();
101 int GetVideoHeight(); 110 int GetVideoHeight();
102 base::TimeDelta GetCurrentTime(); 111 base::TimeDelta GetCurrentTime();
103 base::TimeDelta GetDuration(); 112 base::TimeDelta GetDuration();
104 bool IsPlaying(); 113 bool IsPlaying();
105 114
106 // Get metadata from the media. 115 // Get metadata from the media.
107 void GetMetadata(bool* can_pause, 116 void GetMetadata();
108 bool* can_seek_forward,
109 bool* can_seek_backward);
110 117
111 // Set the device to stay awake when player is playing. 118 // Called by the timer to check for current time routinely and generates
112 void SetStayAwakeWhilePlaying(); 119 // time update events.
120 void DoTimeUpdate();
113 121
114 // Called by the Java MediaPlayerListener and mirrored to corresponding 122 // Called by the MediaPlayerListener and mirrored to corresponding
115 // callbacks. 123 // callbacks.
116 void OnMediaError(JNIEnv* /* env */, jobject /* obj */, jint error_type); 124 void OnMediaError(int error_type);
117 void OnMediaInfo(JNIEnv* /* env */, jobject /* obj */, jint info_type); 125 void OnVideoSizeChanged(int width, int height);
118 void OnVideoSizeChanged(JNIEnv* /* env */, jobject /* obj */, 126 void OnBufferingUpdate(int percent);
119 jint width, jint height); 127 void OnPlaybackComplete();
120 void OnBufferingUpdate(JNIEnv* /* env */, jobject /* obj */, jint percent); 128 void OnSeekComplete();
121 void OnPlaybackComplete(JNIEnv* /* env */, jobject /* obj */); 129 void OnMediaPrepared();
122 void OnSeekComplete(JNIEnv* /* env */, jobject /* obj */);
123 void OnMediaPrepared(JNIEnv* /* env */, jobject /* obj */);
124 130
125 // Register MediaPlayerListener in the system library loader. 131 // Prepare the player for playback, asynchronously. When succeeds,
126 static bool RegisterMediaPlayerListener(JNIEnv* env); 132 // OnMediaPrepared() will be called. Otherwise, OnMediaError() will
133 // be called with an error type.
134 void Prepare();
135
136 // Callback function passed to |cookies_retriever_|.
137 void GetCookiesCallback(std::string cookies);
138
139 int player_id() { return player_id_; }
140 bool can_pause() { return can_pause_; }
141 bool can_seek_forward() { return can_seek_forward_; }
142 bool can_seek_backward() { return can_seek_backward_; }
143 bool prepared() { return prepared_; }
127 144
128 private: 145 private:
129 void CallVoidMethod(std::string method_name); 146 void CallVoidMethod(std::string method_name);
130 int CallIntMethod(std::string method_name); 147 int CallIntMethod(std::string method_name);
131 148
149 void SetDataSource(const std::string& url,
150 const std::string& cookies,
151 bool hide_url_log);
152
153 // Create the actual android media player.
154 void InitializePlayer();
155
156 // Functions that implements media player control.
157 void StartInternal();
158 void PauseInternal();
159 void SeekInternal(base::TimeDelta time);
160
132 // Callbacks when events are received. 161 // Callbacks when events are received.
133 MediaInfoCB media_info_cb_;
134 MediaErrorCB media_error_cb_; 162 MediaErrorCB media_error_cb_;
135 VideoSizeChangedCB video_size_changed_cb_; 163 VideoSizeChangedCB video_size_changed_cb_;
136 BufferingUpdateCB buffering_update_cb_; 164 BufferingUpdateCB buffering_update_cb_;
137 base::Closure playback_complete_cb_; 165 MediaPreparedCB media_prepared_cb_;
138 base::Closure seek_complete_cb_; 166 PlaybackCompleteCB playback_complete_cb_;
139 base::Closure media_prepared_cb_; 167 SeekCompleteCB seek_complete_cb_;
168
169 // Callbacks when timer events are received.
170 TimeUpdateCB time_update_cb_;
171
172 // Player ID assigned to this player.
173 int player_id_;
174
175 // Whether the player is prepared for playback.
176 bool prepared_;
177
178 // Pending play event while player is preparing.
179 bool pending_play_;
180
181 // Pending seek time while player is preparing.
182 base::TimeDelta pending_seek_;
183
184 // Url for playback.
185 std::string url_;
186
187 // First party url for cookies.
188 std::string first_party_for_cookies_;
189
190 // Whether cookies are available.
191 bool has_cookies_;
192
193 // Hide url log from media player.
194 bool hide_url_log_;
195
196 // Stats about the media.
197 base::TimeDelta duration_;
198 int width_;
199 int height_;
200
201 // Meta data about actions can be taken.
202 bool can_pause_;
203 bool can_seek_forward_;
204 bool can_seek_backward_;
205
206 // Cookies for |url_|
207 std::string cookies_;
208
209 // Resource manager for all the media players.
210 media::MediaPlayerBridgeManager* manager_;
211
212 // Object for retrieving cookies for this media player.
213 scoped_ptr<media::CookieGetter> cookie_getter_;
140 214
141 // Java MediaPlayer class and instance. 215 // Java MediaPlayer class and instance.
142 base::android::ScopedJavaGlobalRef<jclass> j_media_player_class_; 216 base::android::ScopedJavaGlobalRef<jclass> j_media_player_class_;
143 base::android::ScopedJavaGlobalRef<jobject> j_media_player_; 217 base::android::ScopedJavaGlobalRef<jobject> j_media_player_;
144 218
219 base::RepeatingTimer<MediaPlayerBridge> time_update_timer_;
220
221 // Weak pointer passed to |listener_| for callbacks.
222 base::WeakPtrFactory<MediaPlayerBridge> weak_this_;
223
224 // Listener object that listens to all the media player events.
225 scoped_ptr<MediaPlayerListener> listener_;
scherkus (not reviewing) 2012/09/13 10:40:14 I was referring to replacing this line of code wit
qinmin 2012/09/13 18:51:16 Sorry, done. On 2012/09/13 10:40:14, scherkus wro
226
145 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge); 227 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge);
146 }; 228 };
147 229
148 } // namespace media 230 } // namespace media
149 231
150 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ 232 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698