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 ~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
);
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();
102 friend class MediaPlayerBridgeTest
;
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();
111 // Returns true if the Java MediaPlayerBridge's seekTo method is called
112 bool SeekInternal(base::TimeDelta current_time
, base::TimeDelta time
);
114 // Called when |time_update_timer_| fires.
115 void OnTimeUpdateTimerFired();
117 // Update allowed operations from the player.
118 void UpdateAllowedOperations();
120 // Callback function passed to |resource_getter_|. Called when the cookies
122 void OnCookiesRetrieved(const std::string
& cookies
);
124 // Callback function passed to |resource_getter_|. Called when the auth
125 // credentials are retrieved.
126 void OnAuthCredentialsRetrieved(
127 const base::string16
& username
, const base::string16
& password
);
129 // Extract the media metadata from a url, asynchronously.
130 // OnMediaMetadataExtracted() will be called when this call finishes.
131 void ExtractMediaMetadata(const std::string
& url
);
132 void OnMediaMetadataExtracted(base::TimeDelta duration
, int width
, int height
,
135 // Returns true if a MediaUrlInterceptor registered by the embedder has
136 // intercepted the url.
137 bool InterceptMediaUrl(
138 const std::string
& url
, int* fd
, int64
* offset
, int64
* size
);
140 // Whether the player is prepared for playback.
143 // Pending play event while player is preparing.
146 // Pending seek time while player is preparing.
147 base::TimeDelta pending_seek_
;
149 // Whether a seek should be performed after preparing.
150 bool should_seek_on_prepare_
;
155 // First party url for cookies.
156 GURL first_party_for_cookies_
;
158 // User agent string to be used for media player.
159 const std::string user_agent_
;
161 // Hide url log from media player.
164 // Stats about the media.
165 base::TimeDelta duration_
;
169 // Meta data about actions can be taken.
171 bool can_seek_forward_
;
172 bool can_seek_backward_
;
174 // Cookies for |url_|.
175 std::string cookies_
;
177 // Java MediaPlayerBridge instance.
178 base::android::ScopedJavaGlobalRef
<jobject
> j_media_player_bridge_
;
180 base::RepeatingTimer
<MediaPlayerBridge
> time_update_timer_
;
182 // Volume of playback.
185 // Whether user credentials are allowed to be passed.
186 bool allow_credentials_
;
188 // NOTE: Weak pointers must be invalidated before all other member variables.
189 base::WeakPtrFactory
<MediaPlayerBridge
> weak_factory_
;
191 DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge
);
196 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_