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" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/time.h" | 16 #include "base/time.h" |
17 #include "base/timer.h" | 17 #include "base/timer.h" |
| 18 #include "googleurl/src/gurl.h" |
18 #include "media/base/media_export.h" | 19 #include "media/base/media_export.h" |
19 #include "media/base/android/media_player_listener.h" | 20 #include "media/base/android/media_player_listener.h" |
20 | 21 |
21 namespace media { | 22 namespace media { |
22 | 23 |
23 class CookieGetter; | 24 class MediaResourceGetter; |
24 class MediaPlayerBridgeManager; | 25 class MediaPlayerBridgeManager; |
25 | 26 |
26 // This class serves as a bridge for native code to call java functions inside | 27 // This class serves as a bridge for native code to call java functions inside |
27 // android mediaplayer class. For more information on android mediaplayer, check | 28 // android mediaplayer class. For more information on android mediaplayer, check |
28 // http://developer.android.com/reference/android/media/MediaPlayer.html | 29 // http://developer.android.com/reference/android/media/MediaPlayer.html |
29 // The actual android mediaplayer instance is created lazily when Start(), | 30 // The actual android mediaplayer instance is created lazily when Start(), |
30 // Pause(), SeekTo() gets called. As a result, media information may not | 31 // Pause(), SeekTo() gets called. As a result, media information may not |
31 // be available until one of those operations is performed. After that, we | 32 // be available until one of those operations is performed. After that, we |
32 // will cache those information in case the mediaplayer gets released. | 33 // will cache those information in case the mediaplayer gets released. |
33 class MEDIA_EXPORT MediaPlayerBridge { | 34 class MEDIA_EXPORT MediaPlayerBridge { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 typedef base::Callback<void(int, base::TimeDelta)> TimeUpdateCB; | 68 typedef base::Callback<void(int, base::TimeDelta)> TimeUpdateCB; |
68 | 69 |
69 static bool RegisterMediaPlayerBridge(JNIEnv* env); | 70 static bool RegisterMediaPlayerBridge(JNIEnv* env); |
70 | 71 |
71 // Construct a MediaPlayerBridge object with all the needed media player | 72 // Construct a MediaPlayerBridge object with all the needed media player |
72 // callbacks. This object needs to call |manager|'s RequestMediaResources() | 73 // callbacks. This object needs to call |manager|'s RequestMediaResources() |
73 // before decoding the media stream. This allows |manager| to track | 74 // before decoding the media stream. This allows |manager| to track |
74 // unused resources and free them when needed. On the other hand, it needs | 75 // unused resources and free them when needed. On the other hand, it needs |
75 // to call ReleaseMediaResources() when it is done with decoding. | 76 // to call ReleaseMediaResources() when it is done with decoding. |
76 MediaPlayerBridge(int player_id, | 77 MediaPlayerBridge(int player_id, |
77 const std::string& url, | 78 const GURL& url, |
78 const std::string& first_party_for_cookies, | 79 const GURL& first_party_for_cookies, |
79 CookieGetter* cookie_getter, | 80 MediaResourceGetter* resource_getter, |
80 bool hide_url_log, | 81 bool hide_url_log, |
81 MediaPlayerBridgeManager* manager, | 82 MediaPlayerBridgeManager* manager, |
82 const MediaErrorCB& media_error_cb, | 83 const MediaErrorCB& media_error_cb, |
83 const VideoSizeChangedCB& video_size_changed_cb, | 84 const VideoSizeChangedCB& video_size_changed_cb, |
84 const BufferingUpdateCB& buffering_update_cb, | 85 const BufferingUpdateCB& buffering_update_cb, |
85 const MediaPreparedCB& media_prepared_cb, | 86 const MediaPreparedCB& media_prepared_cb, |
86 const PlaybackCompleteCB& playback_complete_cb, | 87 const PlaybackCompleteCB& playback_complete_cb, |
87 const SeekCompleteCB& seek_complete_cb, | 88 const SeekCompleteCB& seek_complete_cb, |
88 const TimeUpdateCB& time_update_cb, | 89 const TimeUpdateCB& time_update_cb, |
89 const MediaInterruptedCB& media_interrupted_cb); | 90 const MediaInterruptedCB& media_interrupted_cb); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 void OnPlaybackComplete(); | 132 void OnPlaybackComplete(); |
132 void OnSeekComplete(); | 133 void OnSeekComplete(); |
133 void OnMediaPrepared(); | 134 void OnMediaPrepared(); |
134 void OnMediaInterrupted(); | 135 void OnMediaInterrupted(); |
135 | 136 |
136 // Prepare the player for playback, asynchronously. When succeeds, | 137 // Prepare the player for playback, asynchronously. When succeeds, |
137 // OnMediaPrepared() will be called. Otherwise, OnMediaError() will | 138 // OnMediaPrepared() will be called. Otherwise, OnMediaError() will |
138 // be called with an error type. | 139 // be called with an error type. |
139 void Prepare(); | 140 void Prepare(); |
140 | 141 |
141 // Callback function passed to |cookies_retriever_|. | 142 // Callback function passed to |resource_getter_|. |
142 void GetCookiesCallback(const std::string& cookies); | 143 void GetCookiesCallback(const std::string& cookies); |
143 | 144 |
144 int player_id() { return player_id_; } | 145 int player_id() { return player_id_; } |
145 bool can_pause() { return can_pause_; } | 146 bool can_pause() { return can_pause_; } |
146 bool can_seek_forward() { return can_seek_forward_; } | 147 bool can_seek_forward() { return can_seek_forward_; } |
147 bool can_seek_backward() { return can_seek_backward_; } | 148 bool can_seek_backward() { return can_seek_backward_; } |
148 bool prepared() { return prepared_; } | 149 bool prepared() { return prepared_; } |
149 | 150 |
150 private: | 151 private: |
151 // Create the actual android media player. | 152 // Create the actual android media player. |
152 void InitializePlayer(); | 153 void InitializePlayer(); |
153 | 154 |
| 155 // Set the data source for the media player. |
| 156 void SetDataSource(const std::string& url); |
| 157 |
154 // Functions that implements media player control. | 158 // Functions that implements media player control. |
155 void StartInternal(); | 159 void StartInternal(); |
156 void PauseInternal(); | 160 void PauseInternal(); |
157 void SeekInternal(base::TimeDelta time); | 161 void SeekInternal(base::TimeDelta time); |
158 | 162 |
159 // Callbacks when events are received. | 163 // Callbacks when events are received. |
160 MediaErrorCB media_error_cb_; | 164 MediaErrorCB media_error_cb_; |
161 VideoSizeChangedCB video_size_changed_cb_; | 165 VideoSizeChangedCB video_size_changed_cb_; |
162 BufferingUpdateCB buffering_update_cb_; | 166 BufferingUpdateCB buffering_update_cb_; |
163 MediaPreparedCB media_prepared_cb_; | 167 MediaPreparedCB media_prepared_cb_; |
(...skipping 10 matching lines...) Expand all Loading... |
174 // Whether the player is prepared for playback. | 178 // Whether the player is prepared for playback. |
175 bool prepared_; | 179 bool prepared_; |
176 | 180 |
177 // Pending play event while player is preparing. | 181 // Pending play event while player is preparing. |
178 bool pending_play_; | 182 bool pending_play_; |
179 | 183 |
180 // Pending seek time while player is preparing. | 184 // Pending seek time while player is preparing. |
181 base::TimeDelta pending_seek_; | 185 base::TimeDelta pending_seek_; |
182 | 186 |
183 // Url for playback. | 187 // Url for playback. |
184 std::string url_; | 188 GURL url_; |
185 | 189 |
186 // First party url for cookies. | 190 // First party url for cookies. |
187 std::string first_party_for_cookies_; | 191 GURL first_party_for_cookies_; |
188 | 192 |
189 // Whether cookies are available. | 193 // Whether cookies are available. |
190 bool has_cookies_; | 194 bool has_cookies_; |
191 | 195 |
192 // Hide url log from media player. | 196 // Hide url log from media player. |
193 bool hide_url_log_; | 197 bool hide_url_log_; |
194 | 198 |
195 // Stats about the media. | 199 // Stats about the media. |
196 base::TimeDelta duration_; | 200 base::TimeDelta duration_; |
197 int width_; | 201 int width_; |
198 int height_; | 202 int height_; |
199 | 203 |
200 // Meta data about actions can be taken. | 204 // Meta data about actions can be taken. |
201 bool can_pause_; | 205 bool can_pause_; |
202 bool can_seek_forward_; | 206 bool can_seek_forward_; |
203 bool can_seek_backward_; | 207 bool can_seek_backward_; |
204 | 208 |
205 // Cookies for |url_| | 209 // Cookies for |url_|. |
206 std::string cookies_; | 210 std::string cookies_; |
207 | 211 |
208 // Resource manager for all the media players. | 212 // Resource manager for all the media players. |
209 MediaPlayerBridgeManager* manager_; | 213 MediaPlayerBridgeManager* manager_; |
210 | 214 |
211 // Object for retrieving cookies for this media player. | 215 // Object for retrieving resources for this media player. |
212 scoped_ptr<CookieGetter> cookie_getter_; | 216 scoped_ptr<MediaResourceGetter> resource_getter_; |
213 | 217 |
214 // Java MediaPlayer instance. | 218 // Java MediaPlayer instance. |
215 base::android::ScopedJavaGlobalRef<jobject> j_media_player_; | 219 base::android::ScopedJavaGlobalRef<jobject> j_media_player_; |
216 | 220 |
217 base::RepeatingTimer<MediaPlayerBridge> time_update_timer_; | 221 base::RepeatingTimer<MediaPlayerBridge> time_update_timer_; |
218 | 222 |
219 // Weak pointer passed to |listener_| for callbacks. | 223 // Weak pointer passed to |listener_| for callbacks. |
220 base::WeakPtrFactory<MediaPlayerBridge> weak_this_; | 224 base::WeakPtrFactory<MediaPlayerBridge> weak_this_; |
221 | 225 |
222 // Listener object that listens to all the media player events. | 226 // Listener object that listens to all the media player events. |
223 MediaPlayerListener listener_; | 227 MediaPlayerListener listener_; |
224 | 228 |
225 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge); | 229 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge); |
226 }; | 230 }; |
227 | 231 |
228 } // namespace media | 232 } // namespace media |
229 | 233 |
230 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ | 234 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ |
OLD | NEW |