• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                     const std::string& user_agent,
49                     bool hide_url_log,
50                     MediaPlayerManager* manager,
51                     const RequestMediaResourcesCB& request_media_resources_cb,
52                     const ReleaseMediaResourcesCB& release_media_resources_cb,
53                     const GURL& frame_url,
54                     bool allow_credentials);
55   virtual ~MediaPlayerBridge();
56 
57   // Initialize this object and extract the metadata from the media.
58   virtual void Initialize();
59 
60   // MediaPlayerAndroid implementation.
61   virtual void SetVideoSurface(gfx::ScopedJavaSurface surface) OVERRIDE;
62   virtual void Start() OVERRIDE;
63   virtual void Pause(bool is_media_related_action ALLOW_UNUSED) OVERRIDE;
64   virtual void SeekTo(base::TimeDelta timestamp) OVERRIDE;
65   virtual void Release() OVERRIDE;
66   virtual void SetVolume(double volume) OVERRIDE;
67   virtual int GetVideoWidth() OVERRIDE;
68   virtual int GetVideoHeight() OVERRIDE;
69   virtual base::TimeDelta GetCurrentTime() OVERRIDE;
70   virtual base::TimeDelta GetDuration() OVERRIDE;
71   virtual bool IsPlaying() OVERRIDE;
72   virtual bool CanPause() OVERRIDE;
73   virtual bool CanSeekForward() OVERRIDE;
74   virtual bool CanSeekBackward() OVERRIDE;
75   virtual bool IsPlayerReady() OVERRIDE;
76   virtual GURL GetUrl() OVERRIDE;
77   virtual GURL GetFirstPartyForCookies() OVERRIDE;
78   virtual bool IsSurfaceInUse() const OVERRIDE;
79 
80   // MediaPlayerListener callbacks.
81   void OnVideoSizeChanged(int width, int height);
82   void OnMediaError(int error_type);
83   void OnBufferingUpdate(int percent);
84   void OnPlaybackComplete();
85   void OnMediaInterrupted();
86   void OnSeekComplete();
87   void OnDidSetDataUriDataSource(JNIEnv* env, jobject obj, jboolean success);
88 
89  protected:
90   void SetJavaMediaPlayerBridge(jobject j_media_player_bridge);
91   base::android::ScopedJavaLocalRef<jobject> GetJavaMediaPlayerBridge();
92   void SetMediaPlayerListener();
93   void SetDuration(base::TimeDelta time);
94 
95   virtual void PendingSeekInternal(const base::TimeDelta& time);
96 
97   // Prepare the player for playback, asynchronously. When succeeds,
98   // OnMediaPrepared() will be called. Otherwise, OnMediaError() will
99   // be called with an error type.
100   virtual void Prepare();
101   void OnMediaPrepared();
102 
103   // Create the corresponding Java class instance.
104   virtual void CreateJavaMediaPlayerBridge();
105 
106   // Get allowed operations from the player.
107   virtual base::android::ScopedJavaLocalRef<jobject> GetAllowedOperations();
108 
109  private:
110   friend class MediaPlayerListener;
111 
112   // Set the data source for the media player.
113   void SetDataSource(const std::string& url);
114 
115   // Functions that implements media player control.
116   void StartInternal();
117   void PauseInternal();
118   void SeekInternal(base::TimeDelta time);
119 
120   // Called when |time_update_timer_| fires.
121   void OnTimeUpdateTimerFired();
122 
123   // Update allowed operations from the player.
124   void UpdateAllowedOperations();
125 
126   // Callback function passed to |resource_getter_|. Called when the cookies
127   // are retrieved.
128   void OnCookiesRetrieved(const std::string& cookies);
129 
130   // Extract the media metadata from a url, asynchronously.
131   // OnMediaMetadataExtracted() will be called when this call finishes.
132   void ExtractMediaMetadata(const std::string& url);
133   void OnMediaMetadataExtracted(base::TimeDelta duration, int width, int height,
134                                 bool success);
135 
136   // Returns true if a MediaUrlInterceptor registered by the embedder has
137   // intercepted the url.
138   bool InterceptMediaUrl(
139       const std::string& url, int* fd, int64* offset, int64* size);
140 
141   // Whether the player is prepared for playback.
142   bool prepared_;
143 
144   // Pending play event while player is preparing.
145   bool pending_play_;
146 
147   // Pending seek time while player is preparing.
148   base::TimeDelta pending_seek_;
149 
150   // Url for playback.
151   GURL url_;
152 
153   // First party url for cookies.
154   GURL first_party_for_cookies_;
155 
156   // User agent string to be used for media player.
157   const std::string user_agent_;
158 
159   // Hide url log from media player.
160   bool hide_url_log_;
161 
162   // Stats about the media.
163   base::TimeDelta duration_;
164   int width_;
165   int height_;
166 
167   // Meta data about actions can be taken.
168   bool can_pause_;
169   bool can_seek_forward_;
170   bool can_seek_backward_;
171 
172   // Cookies for |url_|.
173   std::string cookies_;
174 
175   // Java MediaPlayerBridge instance.
176   base::android::ScopedJavaGlobalRef<jobject> j_media_player_bridge_;
177 
178   base::RepeatingTimer<MediaPlayerBridge> time_update_timer_;
179 
180   // Listener object that listens to all the media player events.
181   scoped_ptr<MediaPlayerListener> listener_;
182 
183   // Whether player is currently using a surface.
184   bool is_surface_in_use_;
185 
186   // Volume of playback.
187   double volume_;
188 
189   // Whether user credentials are allowed to be passed.
190   bool allow_credentials_;
191 
192   // Weak pointer passed to |listener_| for callbacks.
193   // NOTE: Weak pointers must be invalidated before all other member variables.
194   base::WeakPtrFactory<MediaPlayerBridge> weak_factory_;
195 
196   DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge);
197 };
198 
199 }  // namespace media
200 
201 #endif  // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_
202