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_
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"
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
{
30 virtual ~MediaPlayerAndroid();
32 // Error types for MediaErrorCB.
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;
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
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 // Overridden in MediaCodecPlayer to pass data between threads.
84 virtual void OnMediaMetadataChanged(base::TimeDelta duration
,
85 const gfx::Size
& video_size
) {}
87 // Overridden in MediaCodecPlayer to pass data between threads.
88 virtual void OnTimeUpdate(base::TimeDelta current_timestamp
,
89 base::TimeTicks current_time_ticks
) {}
91 int player_id() { return player_id_
; }
93 GURL
frame_url() { return frame_url_
; }
95 // Attach/Detaches |listener_| for listening to all the media events. If
96 // |j_media_player| is NULL, |listener_| only listens to the system media
97 // events. Otherwise, it also listens to the events from |j_media_player|.
98 void AttachListener(jobject j_media_player
);
99 void DetachListener();
102 MediaPlayerAndroid(int player_id
,
103 MediaPlayerManager
* manager
,
104 const RequestMediaResourcesCB
& request_media_resources_cb
,
105 const GURL
& frame_url
);
107 // TODO(qinmin): Simplify the MediaPlayerListener class to only listen to
108 // media interrupt events. And have a separate child class to listen to all
109 // the events needed by MediaPlayerBridge. http://crbug.com/422597.
110 // MediaPlayerListener callbacks.
111 virtual void OnVideoSizeChanged(int width
, int height
);
112 virtual void OnMediaError(int error_type
);
113 virtual void OnBufferingUpdate(int percent
);
114 virtual void OnPlaybackComplete();
115 virtual void OnMediaInterrupted();
116 virtual void OnSeekComplete();
117 virtual void OnMediaPrepared();
119 // When destroying a subclassed object on a non-UI thread
120 // it is still required to destroy the |listener_| related stuff
122 void DestroyListenerOnUIThread();
124 MediaPlayerManager
* manager() { return manager_
; }
126 base::WeakPtr
<MediaPlayerAndroid
> WeakPtrForUIThread();
128 RequestMediaResourcesCB request_media_resources_cb_
;
131 friend class MediaPlayerListener
;
133 // Player ID assigned to this player.
136 // Resource manager for all the media players.
137 MediaPlayerManager
* manager_
;
139 // Url for the frame that contains this player.
142 // Listener object that listens to all the media player events.
143 scoped_ptr
<MediaPlayerListener
> listener_
;
145 // Weak pointer passed to |listener_| for callbacks.
146 // NOTE: Weak pointers must be invalidated before all other member variables.
147 base::WeakPtrFactory
<MediaPlayerAndroid
> weak_factory_
;
149 DISALLOW_COPY_AND_ASSIGN(MediaPlayerAndroid
);
154 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_ANDROID_H_