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. 4 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ 6 #define MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ 7 8 #include <jni.h> 9 #include <map> 10 #include <string> 11 12 #include "base/android/scoped_java_ref.h" 13 #include "base/callback.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/weak_ptr.h" 16 #include "base/time/time.h" 17 #include "base/timer/timer.h" 18 #include "media/base/android/media_player_android.h" 19 #include "media/base/android/media_player_listener.h" 20 #include "url/gurl.h" 21 22 namespace media { 23 24 class MediaPlayerManager; 25 26 // This class serves as a bridge between the native code and Android MediaPlayer 27 // Java class. For more information on Android MediaPlayer, check 28 // http://developer.android.com/reference/android/media/MediaPlayer.html 29 // The actual Android MediaPlayer instance is created lazily when Start(), 30 // 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 // will cache those information in case the mediaplayer gets released. 33 // The class uses the corresponding MediaPlayerBridge Java class to talk to 34 // the Android MediaPlayer instance. 35 class MEDIA_EXPORT MediaPlayerBridge : public MediaPlayerAndroid { 36 public: 37 static bool RegisterMediaPlayerBridge(JNIEnv* env); 38 39 // Construct a MediaPlayerBridge object. This object needs to call |manager|'s 40 // RequestMediaResources() before decoding the media stream. This allows 41 // |manager| to track unused resources and free them when needed. On the other 42 // hand, it needs to call ReleaseMediaResources() when it is done with 43 // decoding. MediaPlayerBridge also forwards Android MediaPlayer callbacks to 44 // the |manager| when needed. 45 MediaPlayerBridge(int player_id, 46 const GURL& url, 47 const GURL& first_party_for_cookies, 48 bool hide_url_log, 49 MediaPlayerManager* manager); 50 virtual ~MediaPlayerBridge(); 51 52 // Initialize this object and extract the metadata from the media. 53 virtual void Initialize(); 54 55 // MediaPlayerAndroid implementation. 56 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface) OVERRIDE; 57 virtual void Start() OVERRIDE; 58 virtual void Pause(bool is_media_related_action ALLOW_UNUSED) OVERRIDE; 59 virtual void SeekTo(const base::TimeDelta& timestamp) OVERRIDE; 60 virtual void Release() OVERRIDE; 61 virtual void SetVolume(double volume) OVERRIDE; 62 virtual int GetVideoWidth() OVERRIDE; 63 virtual int GetVideoHeight() OVERRIDE; 64 virtual base::TimeDelta GetCurrentTime() OVERRIDE; 65 virtual base::TimeDelta GetDuration() OVERRIDE; 66 virtual bool IsPlaying() OVERRIDE; 67 virtual bool CanPause() OVERRIDE; 68 virtual bool CanSeekForward() OVERRIDE; 69 virtual bool CanSeekBackward() OVERRIDE; 70 virtual bool IsPlayerReady() OVERRIDE; 71 virtual GURL GetUrl() OVERRIDE; 72 virtual GURL GetFirstPartyForCookies() OVERRIDE; 73 74 // MediaPlayerListener callbacks. 75 void OnVideoSizeChanged(int width, int height); 76 void OnMediaError(int error_type); 77 void OnBufferingUpdate(int percent); 78 void OnPlaybackComplete(); 79 void OnMediaInterrupted(); 80 void OnSeekComplete(); 81 void OnDidSetDataUriDataSource(JNIEnv* env, jobject obj, jboolean success); 82 83 protected: 84 void SetJavaMediaPlayerBridge(jobject j_media_player_bridge); 85 base::android::ScopedJavaLocalRef<jobject> GetJavaMediaPlayerBridge(); 86 void SetMediaPlayerListener(); 87 void SetDuration(base::TimeDelta time); 88 89 virtual void PendingSeekInternal(const base::TimeDelta& time); 90 91 // Prepare the player for playback, asynchronously. When succeeds, 92 // OnMediaPrepared() will be called. Otherwise, OnMediaError() will 93 // be called with an error type. 94 virtual void Prepare(); 95 void OnMediaPrepared(); 96 97 // Create the corresponding Java class instance. 98 virtual void CreateJavaMediaPlayerBridge(); 99 100 // Get allowed operations from the player. 101 virtual base::android::ScopedJavaLocalRef<jobject> GetAllowedOperations(); 102 103 private: 104 // Set the data source for the media player. 105 void SetDataSource(const std::string& url); 106 107 // Functions that implements media player control. 108 void StartInternal(); 109 void PauseInternal(); 110 void SeekInternal(base::TimeDelta time); 111 112 // Called when |time_update_timer_| fires. 113 void OnTimeUpdateTimerFired(); 114 115 // Update allowed operations from the player. 116 void UpdateAllowedOperations(); 117 118 // Callback function passed to |resource_getter_|. Called when the cookies 119 // are retrieved. 120 void OnCookiesRetrieved(const std::string& cookies); 121 122 // Extract the media metadata from a url, asynchronously. 123 // OnMediaMetadataExtracted() will be called when this call finishes. 124 void ExtractMediaMetadata(const std::string& url); 125 void OnMediaMetadataExtracted(base::TimeDelta duration, int width, int height, 126 bool success); 127 128 // Whether the player is prepared for playback. 129 bool prepared_; 130 131 // Pending play event while player is preparing. 132 bool pending_play_; 133 134 // Pending seek time while player is preparing. 135 base::TimeDelta pending_seek_; 136 137 // Url for playback. 138 GURL url_; 139 140 // First party url for cookies. 141 GURL first_party_for_cookies_; 142 143 // Hide url log from media player. 144 bool hide_url_log_; 145 146 // Stats about the media. 147 base::TimeDelta duration_; 148 int width_; 149 int height_; 150 151 // Meta data about actions can be taken. 152 bool can_pause_; 153 bool can_seek_forward_; 154 bool can_seek_backward_; 155 156 // Cookies for |url_|. 157 std::string cookies_; 158 159 // Java MediaPlayerBridge instance. 160 base::android::ScopedJavaGlobalRef<jobject> j_media_player_bridge_; 161 162 base::RepeatingTimer<MediaPlayerBridge> time_update_timer_; 163 164 // Weak pointer passed to |listener_| for callbacks. 165 base::WeakPtrFactory<MediaPlayerBridge> weak_this_; 166 167 // Listener object that listens to all the media player events. 168 MediaPlayerListener listener_; 169 170 friend class MediaPlayerListener; 171 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge); 172 }; 173 174 } // namespace media 175 176 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_ 177