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

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: addressing comments 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 CookiesRetriever;
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> {
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 MediaPlayerBridge(int player_id,
69 const std::string& url,
70 media::CookiesRetriever* cookies_retriever,
71 bool hide_url_log,
72 media::MediaPlayerBridgeManager* manager,
73 const MediaErrorCB& media_error_cb,
74 const VideoSizeChangedCB& video_size_changed_cb,
75 const BufferingUpdateCB& buffering_update_cb,
76 const MediaPreparedCB& media_prepared_cb,
77 const PlaybackCompleteCB& playback_complete_cb,
78 const SeekCompleteCB& seek_complete_cb,
79 const TimeUpdateCB& time_update_cb);
62 ~MediaPlayerBridge(); 80 ~MediaPlayerBridge();
63 81
64 typedef std::map<std::string, std::string> HeadersMap; 82 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 83
69 void SetVideoSurface(jobject surface); 84 void SetVideoSurface(jobject surface);
70 85
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. 86 // Start playing the media.
81 void Start(const base::Closure& playback_complete_cb); 87 void Start();
82 88
83 // Pause the media. 89 // Pause the media.
84 void Pause(); 90 void Pause();
85 91
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 92 // Seek to a particular position. When succeeds, OnSeekComplete() will be
90 // called. Otherwise, nothing will happen. 93 // called. Otherwise, nothing will happen.
91 void SeekTo(base::TimeDelta time, const base::Closure& seek_complete_cb); 94 void SeekTo(base::TimeDelta time);
92 95
93 // Reset the player. Needs to call SetDataSource() again after this call. 96 // Release the player resources.
94 void Reset(); 97 void Release();
95 98
96 // Set the player volume. 99 // Set the player volume.
97 void SetVolume(float leftVolume, float rightVolume); 100 void SetVolume(float leftVolume, float rightVolume);
98 101
99 // Get the media information from the player. 102 // Get the media information from the player.
100 int GetVideoWidth(); 103 int GetVideoWidth();
101 int GetVideoHeight(); 104 int GetVideoHeight();
102 base::TimeDelta GetCurrentTime(); 105 base::TimeDelta GetCurrentTime();
103 base::TimeDelta GetDuration(); 106 base::TimeDelta GetDuration();
104 bool IsPlaying(); 107 bool IsPlaying();
105 108
106 // Get metadata from the media. 109 // Get metadata from the media.
107 void GetMetadata(bool* can_pause, 110 void GetMetadata();
108 bool* can_seek_forward,
109 bool* can_seek_backward);
110 111
111 // Set the device to stay awake when player is playing. 112 // Called by the timer to check for current time routinely and generates
112 void SetStayAwakeWhilePlaying(); 113 // time update events.
114 void DoTimeUpdate();
113 115
114 // Called by the Java MediaPlayerListener and mirrored to corresponding 116 // Called by the MediaPlayerListener and mirrored to corresponding
115 // callbacks. 117 // callbacks.
116 void OnMediaError(JNIEnv* /* env */, jobject /* obj */, jint error_type); 118 void OnMediaError(int error_type);
117 void OnMediaInfo(JNIEnv* /* env */, jobject /* obj */, jint info_type); 119 void OnVideoSizeChanged(int width, int height);
118 void OnVideoSizeChanged(JNIEnv* /* env */, jobject /* obj */, 120 void OnBufferingUpdate(int percent);
119 jint width, jint height); 121 void OnPlaybackComplete();
120 void OnBufferingUpdate(JNIEnv* /* env */, jobject /* obj */, jint percent); 122 void OnSeekComplete();
121 void OnPlaybackComplete(JNIEnv* /* env */, jobject /* obj */); 123 void OnMediaPrepared();
122 void OnSeekComplete(JNIEnv* /* env */, jobject /* obj */);
123 void OnMediaPrepared(JNIEnv* /* env */, jobject /* obj */);
124 124
125 // Register MediaPlayerListener in the system library loader. 125 // Prepare the player for playback, asynchronously. When succeeds,
126 static bool RegisterMediaPlayerListener(JNIEnv* env); 126 // OnMediaPrepared() will be called. Otherwise, OnMediaError() will
127 // be called with an error type.
128 void Prepare();
129
130 // Callback function passed to |cookies_retriever_|.
131 void GetCookiesCallback(std::string cookies);
132
133 int player_id() { return player_id_; }
134 bool can_pause() { return can_pause_; }
135 bool can_seek_forward() { return can_seek_forward_; }
136 bool can_seek_backward() { return can_seek_backward_; }
137 bool prepared() { return prepared_; }
127 138
128 private: 139 private:
129 void CallVoidMethod(std::string method_name); 140 void CallVoidMethod(std::string method_name);
130 int CallIntMethod(std::string method_name); 141 int CallIntMethod(std::string method_name);
131 142
143 void SetDataSource(const std::string& url,
144 const std::string& cookies,
145 bool hide_url_log);
146
147 // Create the actual android mediaplayer.
148 void InitializePlayer();
149
150 // Functions that implements media player control.
151 void StartInternal();
152 void PauseInternal();
153 void SeekInternal(base::TimeDelta time);
154
132 // Callbacks when events are received. 155 // Callbacks when events are received.
133 MediaInfoCB media_info_cb_;
134 MediaErrorCB media_error_cb_; 156 MediaErrorCB media_error_cb_;
135 VideoSizeChangedCB video_size_changed_cb_; 157 VideoSizeChangedCB video_size_changed_cb_;
136 BufferingUpdateCB buffering_update_cb_; 158 BufferingUpdateCB buffering_update_cb_;
137 base::Closure playback_complete_cb_; 159 MediaPreparedCB media_prepared_cb_;
138 base::Closure seek_complete_cb_; 160 PlaybackCompleteCB playback_complete_cb_;
139 base::Closure media_prepared_cb_; 161 SeekCompleteCB seek_complete_cb_;
162
163 // Callbacks when timer events are received.
164 TimeUpdateCB time_update_cb_;
165
166 // Player ID assigned to this player.
167 int player_id_;
168
169 // Whether the player is prepared for playback.
170 bool prepared_;
171
172 // Pending play event while player is preparing.
173 bool pending_play_;
174
175 // Pending seek time while player is preparing.
176 base::TimeDelta pending_seek_;
177
178 // Url for playback.
179 std::string url_;
180
181 // Whether cookies are available.
182 bool has_cookies_;
183
184 // Hide url log from media player.
185 bool hide_url_log_;
186
187 // Stats about the media.
188 base::TimeDelta duration_;
189 int width_;
190 int height_;
191
192 // Meta data about actions can be taken.
193 bool can_pause_;
194 bool can_seek_forward_;
195 bool can_seek_backward_;
196
197 // Cookies for |url_|
198 std::string cookies_;
199
200 // Resource manager for all the mediaplayers.
201 media::MediaPlayerBridgeManager* manager_;
202
203 // Object for retrieving cookies for this media player.
204 scoped_refptr<media::CookiesRetriever> cookies_retriever_;
140 205
141 // Java MediaPlayer class and instance. 206 // Java MediaPlayer class and instance.
142 base::android::ScopedJavaGlobalRef<jclass> j_media_player_class_; 207 base::android::ScopedJavaGlobalRef<jclass> j_media_player_class_;
143 base::android::ScopedJavaGlobalRef<jobject> j_media_player_; 208 base::android::ScopedJavaGlobalRef<jobject> j_media_player_;
144 209
210 base::RepeatingTimer<MediaPlayerBridge> time_update_timer_;
211
212 // Listener object that listens to all the media player events.
213 scoped_refptr<MediaPlayerListener> listener_;
214
145 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge); 215 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge);
146 }; 216 };
147 217
148 } // namespace media 218 } // namespace media
149 219
150 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ 220 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698