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_
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"
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 {
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,
46 const GURL& first_party_for_cookies,
47 const std::string& user_agent,
49 MediaPlayerManager* manager,
50 const RequestMediaResourcesCB& request_media_resources_cb,
51 const GURL& frame_url,
52 bool allow_credentials);
53 virtual ~MediaPlayerBridge();
55 // Initialize this object and extract the metadata from the media.
56 virtual void Initialize();
58 // MediaPlayerAndroid implementation.
59 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface) override;
60 virtual void Start() override;
61 virtual void Pause(bool is_media_related_action) override;
62 virtual void SeekTo(base::TimeDelta timestamp) override;
63 virtual void Release() override;
64 virtual void SetVolume(double volume) override;
65 virtual int GetVideoWidth() override;
66 virtual int GetVideoHeight() override;
67 virtual base::TimeDelta GetCurrentTime() override;
68 virtual base::TimeDelta GetDuration() override;
69 virtual bool IsPlaying() override;
70 virtual bool CanPause() override;
71 virtual bool CanSeekForward() override;
72 virtual bool CanSeekBackward() override;
73 virtual bool IsPlayerReady() override;
74 virtual GURL GetUrl() override;
75 virtual GURL GetFirstPartyForCookies() override;
77 void OnDidSetDataUriDataSource(JNIEnv* env, jobject obj, jboolean success);
80 void SetJavaMediaPlayerBridge(jobject j_media_player_bridge);
81 base::android::ScopedJavaLocalRef<jobject> GetJavaMediaPlayerBridge();
82 void SetDuration(base::TimeDelta time);
84 virtual void PendingSeekInternal(const base::TimeDelta& time);
86 // Prepare the player for playback, asynchronously. When succeeds,
87 // OnMediaPrepared() will be called. Otherwise, OnMediaError() will
88 // be called with an error type.
89 virtual void Prepare();
91 // MediaPlayerAndroid implementation.
92 virtual void OnVideoSizeChanged(int width, int height) override;
93 virtual void OnPlaybackComplete() override;
94 virtual void OnMediaInterrupted() override;
95 virtual void OnMediaPrepared() override;
97 // Create the corresponding Java class instance.
98 virtual void CreateJavaMediaPlayerBridge();
100 // Get allowed operations from the player.
101 virtual base::android::ScopedJavaLocalRef<jobject> GetAllowedOperations();
104 // Set the data source for the media player.
105 void SetDataSource(const std::string& url);
107 // Functions that implements media player control.
108 void StartInternal();
109 void PauseInternal();
110 void SeekInternal(base::TimeDelta time);
112 // Called when |time_update_timer_| fires.
113 void OnTimeUpdateTimerFired();
115 // Update allowed operations from the player.
116 void UpdateAllowedOperations();
118 // Callback function passed to |resource_getter_|. Called when the cookies
120 void OnCookiesRetrieved(const std::string& cookies);
122 // Callback function passed to |resource_getter_|. Called when the auth
123 // credentials are retrieved.
124 void OnAuthCredentialsRetrieved(
125 const base::string16& username, const base::string16& password);
127 // Extract the media metadata from a url, asynchronously.
128 // OnMediaMetadataExtracted() will be called when this call finishes.
129 void ExtractMediaMetadata(const std::string& url);
130 void OnMediaMetadataExtracted(base::TimeDelta duration, int width, int height,
133 // Returns true if a MediaUrlInterceptor registered by the embedder has
134 // intercepted the url.
135 bool InterceptMediaUrl(
136 const std::string& url, int* fd, int64* offset, int64* size);
138 // Whether the player is prepared for playback.
141 // Pending play event while player is preparing.
144 // Pending seek time while player is preparing.
145 base::TimeDelta pending_seek_;
147 // Whether a seek should be performed after preparing.
148 bool should_seek_on_prepare_;
153 // First party url for cookies.
154 GURL first_party_for_cookies_;
156 // User agent string to be used for media player.
157 const std::string user_agent_;
159 // Hide url log from media player.
162 // Stats about the media.
163 base::TimeDelta duration_;
167 // Meta data about actions can be taken.
169 bool can_seek_forward_;
170 bool can_seek_backward_;
172 // Cookies for |url_|.
173 std::string cookies_;
175 // Java MediaPlayerBridge instance.
176 base::android::ScopedJavaGlobalRef<jobject> j_media_player_bridge_;
178 base::RepeatingTimer<MediaPlayerBridge> time_update_timer_;
180 // Volume of playback.
183 // Whether user credentials are allowed to be passed.
184 bool allow_credentials_;
186 // NOTE: Weak pointers must be invalidated before all other member variables.
187 base::WeakPtrFactory<MediaPlayerBridge> weak_factory_;
189 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge);
194 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_