[Android] Always use pixels for gestures handled in ContentViewCore
[chromium-blink-merge.git] / media / base / android / media_player_bridge.h
blobfe03ad4a171cad93bda5dc34d439d928221ac486
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.
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_
8 #include <jni.h>
9 #include <map>
10 #include <string>
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"
22 namespace media {
24 class MediaPlayerManager;
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);
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 virtual ~MediaPlayerBridge();
53 // Initialize this object and extract the metadata from the media.
54 virtual void Initialize();
56 // MediaPlayerAndroid implementation.
57 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface) OVERRIDE;
58 virtual void Start() OVERRIDE;
59 virtual void Pause(bool is_media_related_action ALLOW_UNUSED) OVERRIDE;
60 virtual void SeekTo(const base::TimeDelta& timestamp) OVERRIDE;
61 virtual void Release() OVERRIDE;
62 virtual void SetVolume(double volume) OVERRIDE;
63 virtual int GetVideoWidth() OVERRIDE;
64 virtual int GetVideoHeight() OVERRIDE;
65 virtual base::TimeDelta GetCurrentTime() OVERRIDE;
66 virtual base::TimeDelta GetDuration() OVERRIDE;
67 virtual bool IsPlaying() OVERRIDE;
68 virtual bool CanPause() OVERRIDE;
69 virtual bool CanSeekForward() OVERRIDE;
70 virtual bool CanSeekBackward() OVERRIDE;
71 virtual bool IsPlayerReady() OVERRIDE;
72 virtual GURL GetUrl() OVERRIDE;
73 virtual GURL GetFirstPartyForCookies() OVERRIDE;
75 // MediaPlayerListener callbacks.
76 void OnVideoSizeChanged(int width, int height);
77 void OnMediaError(int error_type);
78 void OnBufferingUpdate(int percent);
79 void OnPlaybackComplete();
80 void OnMediaInterrupted();
81 void OnSeekComplete();
82 void OnDidSetDataUriDataSource(JNIEnv* env, jobject obj, jboolean success);
84 protected:
85 void SetJavaMediaPlayerBridge(jobject j_media_player_bridge);
86 base::android::ScopedJavaLocalRef<jobject> GetJavaMediaPlayerBridge();
87 void SetMediaPlayerListener();
88 void SetDuration(base::TimeDelta time);
90 virtual void PendingSeekInternal(const base::TimeDelta& time);
92 // Prepare the player for playback, asynchronously. When succeeds,
93 // OnMediaPrepared() will be called. Otherwise, OnMediaError() will
94 // be called with an error type.
95 virtual void Prepare();
96 void OnMediaPrepared();
98 // Create the corresponding Java class instance.
99 virtual void CreateJavaMediaPlayerBridge();
101 // Get allowed operations from the player.
102 virtual base::android::ScopedJavaLocalRef<jobject> GetAllowedOperations();
104 private:
105 // Set the data source for the media player.
106 void SetDataSource(const std::string& url);
108 // Functions that implements media player control.
109 void StartInternal();
110 void PauseInternal();
111 void SeekInternal(base::TimeDelta time);
113 // Called when |time_update_timer_| fires.
114 void OnTimeUpdateTimerFired();
116 // Update allowed operations from the player.
117 void UpdateAllowedOperations();
119 // Callback function passed to |resource_getter_|. Called when the cookies
120 // are retrieved.
121 void OnCookiesRetrieved(const std::string& cookies);
123 // Extract the media metadata from a url, asynchronously.
124 // OnMediaMetadataExtracted() will be called when this call finishes.
125 void ExtractMediaMetadata(const std::string& url);
126 void OnMediaMetadataExtracted(base::TimeDelta duration, int width, int height,
127 bool success);
129 // Whether the player is prepared for playback.
130 bool prepared_;
132 // Pending play event while player is preparing.
133 bool pending_play_;
135 // Pending seek time while player is preparing.
136 base::TimeDelta pending_seek_;
138 // Url for playback.
139 GURL url_;
141 // First party url for cookies.
142 GURL first_party_for_cookies_;
144 // User agent string to be used for media player.
145 const std::string user_agent_;
147 // Hide url log from media player.
148 bool hide_url_log_;
150 // Stats about the media.
151 base::TimeDelta duration_;
152 int width_;
153 int height_;
155 // Meta data about actions can be taken.
156 bool can_pause_;
157 bool can_seek_forward_;
158 bool can_seek_backward_;
160 // Cookies for |url_|.
161 std::string cookies_;
163 // Java MediaPlayerBridge instance.
164 base::android::ScopedJavaGlobalRef<jobject> j_media_player_bridge_;
166 base::RepeatingTimer<MediaPlayerBridge> time_update_timer_;
168 // Weak pointer passed to |listener_| for callbacks.
169 base::WeakPtrFactory<MediaPlayerBridge> weak_this_;
171 // Listener object that listens to all the media player events.
172 MediaPlayerListener listener_;
174 friend class MediaPlayerListener;
175 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge);
178 } // namespace media
180 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_