Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / media / base / android / media_player_bridge.h
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/strings/string16.h"
17 #include "base/time/time.h"
18 #include "base/timer/timer.h"
19 #include "media/base/android/media_player_android.h"
20 #include "media/base/android/media_player_listener.h"
21 #include "url/gurl.h"
22
23 namespace media {
24
25 class MediaPlayerManager;
26
27 // This class serves as a bridge between the native code and Android MediaPlayer
28 // Java class. For more information on Android MediaPlayer, check
29 // http://developer.android.com/reference/android/media/MediaPlayer.html
30 // The actual Android MediaPlayer instance is created lazily when Start(),
31 // Pause(), SeekTo() gets called. As a result, media information may not
32 // be available until one of those operations is performed. After that, we
33 // will cache those information in case the mediaplayer gets released.
34 // The class uses the corresponding MediaPlayerBridge Java class to talk to
35 // the Android MediaPlayer instance.
36 class MEDIA_EXPORT MediaPlayerBridge : public MediaPlayerAndroid {
37  public:
38   static bool RegisterMediaPlayerBridge(JNIEnv* env);
39
40   // Construct a MediaPlayerBridge object. This object needs to call |manager|'s
41   // RequestMediaResources() before decoding the media stream. This allows
42   // |manager| to track unused resources and free them when needed.
43   // 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 GURL& frame_url,
53                     bool allow_credentials);
54   virtual ~MediaPlayerBridge();
55
56   // Initialize this object and extract the metadata from the media.
57   virtual void Initialize();
58
59   // MediaPlayerAndroid implementation.
60   virtual void SetVideoSurface(gfx::ScopedJavaSurface surface) OVERRIDE;
61   virtual void Start() OVERRIDE;
62   virtual void Pause(bool is_media_related_action ALLOW_UNUSED) OVERRIDE;
63   virtual void SeekTo(base::TimeDelta timestamp) OVERRIDE;
64   virtual void Release() OVERRIDE;
65   virtual void SetVolume(double volume) OVERRIDE;
66   virtual int GetVideoWidth() OVERRIDE;
67   virtual int GetVideoHeight() OVERRIDE;
68   virtual base::TimeDelta GetCurrentTime() OVERRIDE;
69   virtual base::TimeDelta GetDuration() OVERRIDE;
70   virtual bool IsPlaying() OVERRIDE;
71   virtual bool CanPause() OVERRIDE;
72   virtual bool CanSeekForward() OVERRIDE;
73   virtual bool CanSeekBackward() OVERRIDE;
74   virtual bool IsPlayerReady() OVERRIDE;
75   virtual GURL GetUrl() OVERRIDE;
76   virtual GURL GetFirstPartyForCookies() OVERRIDE;
77   virtual bool IsSurfaceInUse() const OVERRIDE;
78
79   // MediaPlayerListener callbacks.
80   void OnVideoSizeChanged(int width, int height);
81   void OnMediaError(int error_type);
82   void OnBufferingUpdate(int percent);
83   void OnPlaybackComplete();
84   void OnMediaInterrupted();
85   void OnSeekComplete();
86   void OnDidSetDataUriDataSource(JNIEnv* env, jobject obj, jboolean success);
87
88  protected:
89   void SetJavaMediaPlayerBridge(jobject j_media_player_bridge);
90   base::android::ScopedJavaLocalRef<jobject> GetJavaMediaPlayerBridge();
91   void SetMediaPlayerListener();
92   void SetDuration(base::TimeDelta time);
93
94   virtual void PendingSeekInternal(const base::TimeDelta& time);
95
96   // Prepare the player for playback, asynchronously. When succeeds,
97   // OnMediaPrepared() will be called. Otherwise, OnMediaError() will
98   // be called with an error type.
99   virtual void Prepare();
100   void OnMediaPrepared();
101
102   // Create the corresponding Java class instance.
103   virtual void CreateJavaMediaPlayerBridge();
104
105   // Get allowed operations from the player.
106   virtual base::android::ScopedJavaLocalRef<jobject> GetAllowedOperations();
107
108  private:
109   friend class MediaPlayerListener;
110
111   // Set the data source for the media player.
112   void SetDataSource(const std::string& url);
113
114   // Functions that implements media player control.
115   void StartInternal();
116   void PauseInternal();
117   void SeekInternal(base::TimeDelta time);
118
119   // Called when |time_update_timer_| fires.
120   void OnTimeUpdateTimerFired();
121
122   // Update allowed operations from the player.
123   void UpdateAllowedOperations();
124
125   // Callback function passed to |resource_getter_|. Called when the cookies
126   // are retrieved.
127   void OnCookiesRetrieved(const std::string& cookies);
128
129   // Callback function passed to |resource_getter_|. Called when the auth
130   // credentials are retrieved.
131   void OnAuthCredentialsRetrieved(
132       const base::string16& username, const base::string16& password);
133
134   // Extract the media metadata from a url, asynchronously.
135   // OnMediaMetadataExtracted() will be called when this call finishes.
136   void ExtractMediaMetadata(const std::string& url);
137   void OnMediaMetadataExtracted(base::TimeDelta duration, int width, int height,
138                                 bool success);
139
140   // Returns true if a MediaUrlInterceptor registered by the embedder has
141   // intercepted the url.
142   bool InterceptMediaUrl(
143       const std::string& url, int* fd, int64* offset, int64* size);
144
145   // Whether the player is prepared for playback.
146   bool prepared_;
147
148   // Pending play event while player is preparing.
149   bool pending_play_;
150
151   // Pending seek time while player is preparing.
152   base::TimeDelta pending_seek_;
153
154   // Url for playback.
155   GURL url_;
156
157   // First party url for cookies.
158   GURL first_party_for_cookies_;
159
160   // User agent string to be used for media player.
161   const std::string user_agent_;
162
163   // Hide url log from media player.
164   bool hide_url_log_;
165
166   // Stats about the media.
167   base::TimeDelta duration_;
168   int width_;
169   int height_;
170
171   // Meta data about actions can be taken.
172   bool can_pause_;
173   bool can_seek_forward_;
174   bool can_seek_backward_;
175
176   // Cookies for |url_|.
177   std::string cookies_;
178
179   // Java MediaPlayerBridge instance.
180   base::android::ScopedJavaGlobalRef<jobject> j_media_player_bridge_;
181
182   base::RepeatingTimer<MediaPlayerBridge> time_update_timer_;
183
184   // Listener object that listens to all the media player events.
185   scoped_ptr<MediaPlayerListener> listener_;
186
187   // Whether player is currently using a surface.
188   bool is_surface_in_use_;
189
190   // Volume of playback.
191   double volume_;
192
193   // Whether user credentials are allowed to be passed.
194   bool allow_credentials_;
195
196   // Weak pointer passed to |listener_| for callbacks.
197   // NOTE: Weak pointers must be invalidated before all other member variables.
198   base::WeakPtrFactory<MediaPlayerBridge> weak_factory_;
199
200   DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge);
201 };
202
203 }  // namespace media
204
205 #endif  // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_