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 "url/gurl.h"
21
22namespace media {
23
24class MediaPlayerManager;
25
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.
35class MEDIA_EXPORT MediaPlayerBridge : public MediaPlayerAndroid {
36 public:
37  static bool RegisterMediaPlayerBridge(JNIEnv* env);
38
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  virtual ~MediaPlayerBridge();
54
55  // Initialize this object and extract the metadata from the media.
56  virtual void Initialize();
57
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 ALLOW_UNUSED) 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;
76
77  void OnDidSetDataUriDataSource(JNIEnv* env, jobject obj, jboolean success);
78
79 protected:
80  void SetJavaMediaPlayerBridge(jobject j_media_player_bridge);
81  base::android::ScopedJavaLocalRef<jobject> GetJavaMediaPlayerBridge();
82  void SetDuration(base::TimeDelta time);
83
84  virtual void PendingSeekInternal(const base::TimeDelta& time);
85
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();
90
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;
96
97  // Create the corresponding Java class instance.
98  virtual void CreateJavaMediaPlayerBridge();
99
100  // Get allowed operations from the player.
101  virtual base::android::ScopedJavaLocalRef<jobject> GetAllowedOperations();
102
103 private:
104  // Set the data source for the media player.
105  void SetDataSource(const std::string& url);
106
107  // Functions that implements media player control.
108  void StartInternal();
109  void PauseInternal();
110  void SeekInternal(base::TimeDelta time);
111
112  // Called when |time_update_timer_| fires.
113  void OnTimeUpdateTimerFired();
114
115  // Update allowed operations from the player.
116  void UpdateAllowedOperations();
117
118  // Callback function passed to |resource_getter_|. Called when the cookies
119  // are retrieved.
120  void OnCookiesRetrieved(const std::string& cookies);
121
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);
126
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,
131                                bool success);
132
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);
137
138  // Whether the player is prepared for playback.
139  bool prepared_;
140
141  // Pending play event while player is preparing.
142  bool pending_play_;
143
144  // Pending seek time while player is preparing.
145  base::TimeDelta pending_seek_;
146
147  // Whether a seek should be performed after preparing.
148  bool should_seek_on_prepare_;
149
150  // Url for playback.
151  GURL url_;
152
153  // First party url for cookies.
154  GURL first_party_for_cookies_;
155
156  // User agent string to be used for media player.
157  const std::string user_agent_;
158
159  // Hide url log from media player.
160  bool hide_url_log_;
161
162  // Stats about the media.
163  base::TimeDelta duration_;
164  int width_;
165  int height_;
166
167  // Meta data about actions can be taken.
168  bool can_pause_;
169  bool can_seek_forward_;
170  bool can_seek_backward_;
171
172  // Cookies for |url_|.
173  std::string cookies_;
174
175  // Java MediaPlayerBridge instance.
176  base::android::ScopedJavaGlobalRef<jobject> j_media_player_bridge_;
177
178  base::RepeatingTimer<MediaPlayerBridge> time_update_timer_;
179
180  // Volume of playback.
181  double volume_;
182
183  // Whether user credentials are allowed to be passed.
184  bool allow_credentials_;
185
186  // NOTE: Weak pointers must be invalidated before all other member variables.
187  base::WeakPtrFactory<MediaPlayerBridge> weak_factory_;
188
189  DISALLOW_COPY_AND_ASSIGN(MediaPlayerBridge);
190};
191
192}  // namespace media
193
194#endif  // MEDIA_BASE_ANDROID_MEDIA_PLAYER_BRIDGE_H_
195