Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / base / android / media_player_android.h
blob303f8a6fa2f749cac2b577a0ba3c91c14c17245a
1 // Copyright (c) 2013 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_ANDROID_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_PLAYER_ANDROID_H_
8 #include <jni.h>
9 #include <string>
11 #include "base/callback.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "media/base/android/media_player_listener.h"
15 #include "media/base/media_export.h"
16 #include "ui/gfx/geometry/size.h"
17 #include "ui/gl/android/scoped_java_surface.h"
18 #include "url/gurl.h"
20 namespace media {
22 class BrowserCdm;
23 class MediaPlayerManager;
25 // This class serves as the base class for different media player
26 // implementations on Android. Subclasses need to provide their own
27 // MediaPlayerAndroid::Create() implementation.
28 class MEDIA_EXPORT MediaPlayerAndroid {
29 public:
30 virtual ~MediaPlayerAndroid();
32 // Error types for MediaErrorCB.
33 enum MediaErrorType {
34 MEDIA_ERROR_FORMAT,
35 MEDIA_ERROR_DECODE,
36 MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK,
37 MEDIA_ERROR_INVALID_CODE,
40 // Callback when the player needs decoding resources.
41 typedef base::Callback<void(int player_id)> RequestMediaResourcesCB;
43 // Virtual destructor.
44 // For most subclasses we can delete on the caller thread.
45 virtual void DeleteOnCorrectThread();
47 // Passing an external java surface object to the player.
48 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface) = 0;
50 // Start playing the media.
51 virtual void Start() = 0;
53 // Pause the media.
54 virtual void Pause(bool is_media_related_action) = 0;
56 // Seek to a particular position, based on renderer signaling actual seek
57 // with MediaPlayerHostMsg_Seek. If eventual success, OnSeekComplete() will be
58 // called.
59 virtual void SeekTo(base::TimeDelta timestamp) = 0;
61 // Release the player resources.
62 virtual void Release() = 0;
64 // Set the player volume.
65 virtual void SetVolume(double volume) = 0;
67 // Get the media information from the player.
68 virtual int GetVideoWidth() = 0;
69 virtual int GetVideoHeight() = 0;
70 virtual base::TimeDelta GetDuration() = 0;
71 virtual base::TimeDelta GetCurrentTime() = 0;
72 virtual bool IsPlaying() = 0;
73 virtual bool IsPlayerReady() = 0;
74 virtual bool CanPause() = 0;
75 virtual bool CanSeekForward() = 0;
76 virtual bool CanSeekBackward() = 0;
77 virtual GURL GetUrl();
78 virtual GURL GetFirstPartyForCookies();
80 // Associates the |cdm| with this player.
81 virtual void SetCdm(BrowserCdm* cdm);
83 // Requests playback permission from MediaPlayerManager.
84 // Overridden in MediaCodecPlayer to pass data between threads.
85 virtual void RequestPermissionAndPostResult(base::TimeDelta duration) {}
87 // Overridden in MediaCodecPlayer to pass data between threads.
88 virtual void OnMediaMetadataChanged(base::TimeDelta duration,
89 const gfx::Size& video_size) {}
91 // Overridden in MediaCodecPlayer to pass data between threads.
92 virtual void OnTimeUpdate(base::TimeDelta current_timestamp,
93 base::TimeTicks current_time_ticks) {}
95 int player_id() { return player_id_; }
97 GURL frame_url() { return frame_url_; }
99 // Attach/Detaches |listener_| for listening to all the media events. If
100 // |j_media_player| is NULL, |listener_| only listens to the system media
101 // events. Otherwise, it also listens to the events from |j_media_player|.
102 void AttachListener(jobject j_media_player);
103 void DetachListener();
105 protected:
106 MediaPlayerAndroid(int player_id,
107 MediaPlayerManager* manager,
108 const RequestMediaResourcesCB& request_media_resources_cb,
109 const GURL& frame_url);
111 // TODO(qinmin): Simplify the MediaPlayerListener class to only listen to
112 // media interrupt events. And have a separate child class to listen to all
113 // the events needed by MediaPlayerBridge. http://crbug.com/422597.
114 // MediaPlayerListener callbacks.
115 virtual void OnVideoSizeChanged(int width, int height);
116 virtual void OnMediaError(int error_type);
117 virtual void OnBufferingUpdate(int percent);
118 virtual void OnPlaybackComplete();
119 virtual void OnMediaInterrupted();
120 virtual void OnSeekComplete();
121 virtual void OnMediaPrepared();
123 // When destroying a subclassed object on a non-UI thread
124 // it is still required to destroy the |listener_| related stuff
125 // on the UI thread.
126 void DestroyListenerOnUIThread();
128 MediaPlayerManager* manager() { return manager_; }
130 base::WeakPtr<MediaPlayerAndroid> WeakPtrForUIThread();
132 RequestMediaResourcesCB request_media_resources_cb_;
134 private:
135 friend class MediaPlayerListener;
137 // Player ID assigned to this player.
138 int player_id_;
140 // Resource manager for all the media players.
141 MediaPlayerManager* manager_;
143 // Url for the frame that contains this player.
144 GURL frame_url_;
146 // Listener object that listens to all the media player events.
147 scoped_ptr<MediaPlayerListener> listener_;
149 // Weak pointer passed to |listener_| for callbacks.
150 // NOTE: Weak pointers must be invalidated before all other member variables.
151 base::WeakPtrFactory<MediaPlayerAndroid> weak_factory_;
153 DISALLOW_COPY_AND_ASSIGN(MediaPlayerAndroid);
156 } // namespace media
158 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_ANDROID_H_