Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / android / media_player_bridge.h
blob291aa97959dbfa7dd4136cd6db5d7422bbbdeea7
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/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 "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.
42 // MediaPlayerBridge also forwards Android MediaPlayer callbacks to
43 // the |manager| when needed.
44 MediaPlayerBridge(int player_id,
45 const GURL& url,
46 const GURL& first_party_for_cookies,
47 const std::string& user_agent,
48 bool hide_url_log,
49 MediaPlayerManager* manager,
50 const RequestMediaResourcesCB& request_media_resources_cb,
51 const GURL& frame_url,
52 bool allow_credentials);
53 ~MediaPlayerBridge() override;
55 // Initialize this object and extract the metadata from the media.
56 virtual void Initialize();
58 // MediaPlayerAndroid implementation.
59 void SetVideoSurface(gfx::ScopedJavaSurface surface) override;
60 void Start() override;
61 void Pause(bool is_media_related_action) override;
62 void SeekTo(base::TimeDelta timestamp) override;
63 void Release() override;
64 void SetVolume(double volume) override;
65 int GetVideoWidth() override;
66 int GetVideoHeight() override;
67 base::TimeDelta GetCurrentTime() override;
68 base::TimeDelta GetDuration() override;
69 bool IsPlaying() override;
70 bool CanPause() override;
71 bool CanSeekForward() override;
72 bool CanSeekBackward() override;
73 bool IsPlayerReady() override;
74 GURL GetUrl() override;
75 GURL GetFirstPartyForCookies() override;
77 void OnDidSetDataUriDataSource(JNIEnv* env, jobject obj, jboolean success);
79 protected:
80 void SetDuration(base::TimeDelta time);
82 virtual void PendingSeekInternal(const base::TimeDelta& time);
84 // Prepare the player for playback, asynchronously. When succeeds,
85 // OnMediaPrepared() will be called. Otherwise, OnMediaError() will
86 // be called with an error type.
87 virtual void Prepare();
89 // MediaPlayerAndroid implementation.
90 void OnVideoSizeChanged(int width, int height) override;
91 void OnPlaybackComplete() override;
92 void OnMediaInterrupted() override;
93 void OnMediaPrepared() override;
95 // Create the corresponding Java class instance.
96 virtual void CreateJavaMediaPlayerBridge();
98 // Get allowed operations from the player.
99 virtual base::android::ScopedJavaLocalRef<jobject> GetAllowedOperations();
101 private:
102 // Set the data source for the media player.
103 void SetDataSource(const std::string& url);
105 // Functions that implements media player control.
106 void StartInternal();
107 void PauseInternal();
108 void SeekInternal(base::TimeDelta time);
110 // Called when |time_update_timer_| fires.
111 void OnTimeUpdateTimerFired();
113 // Update allowed operations from the player.
114 void UpdateAllowedOperations();
116 // Callback function passed to |resource_getter_|. Called when the cookies
117 // are retrieved.
118 void OnCookiesRetrieved(const std::string& cookies);
120 // Callback function passed to |resource_getter_|. Called when the auth
121 // credentials are retrieved.
122 void OnAuthCredentialsRetrieved(
123 const base::string16& username, const base::string16& password);
125 // Extract the media metadata from a url, asynchronously.
126 // OnMediaMetadataExtracted() will be called when this call finishes.
127 void ExtractMediaMetadata(const std::string& url);
128 void OnMediaMetadataExtracted(base::TimeDelta duration, int width, int height,
129 bool success);
131 // Returns true if a MediaUrlInterceptor registered by the embedder has
132 // intercepted the url.
133 bool InterceptMediaUrl(
134 const std::string& url, int* fd, int64* offset, int64* size);
136 // Whether the player is prepared for playback.
137 bool prepared_;
139 // Pending play event while player is preparing.
140 bool pending_play_;
142 // Pending seek time while player is preparing.
143 base::TimeDelta pending_seek_;
145 // Whether a seek should be performed after preparing.
146 bool should_seek_on_prepare_;
148 // Url for playback.
149 GURL url_;
151 // First party url for cookies.
152 GURL first_party_for_cookies_;
154 // User agent string to be used for media player.
155 const std::string user_agent_;
157 // Hide url log from media player.
158 bool hide_url_log_;
160 // Stats about the media.
161 base::TimeDelta duration_;
162 int width_;
163 int height_;
165 // Meta data about actions can be taken.
166 bool can_pause_;
167 bool can_seek_forward_;
168 bool can_seek_backward_;
170 // Cookies for |url_|.
171 std::string cookies_;
173 // Java MediaPlayerBridge instance.
174 base::android::ScopedJavaGlobalRef<jobject> j_media_player_bridge_;
176 base::RepeatingTimer<MediaPlayerBridge> time_update_timer_;
178 // Volume of playback.
179 double volume_;
181 // Whether user credentials are allowed to be passed.
182 bool allow_credentials_;
184 // NOTE: Weak pointers must be invalidated before all other member variables.
185 base::WeakPtrFactory<MediaPlayerBridge> weak_factory_;
187 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge);
190 } // namespace media
192 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_