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/gl/android/scoped_java_surface.h"
22 class MediaPlayerManager;
24 // This class serves as the base class for different media player
25 // implementations on Android. Subclasses need to provide their own
26 // MediaPlayerAndroid::Create() implementation.
27 class MEDIA_EXPORT MediaPlayerAndroid {
29 virtual ~MediaPlayerAndroid();
31 // Error types for MediaErrorCB.
35 MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK,
36 MEDIA_ERROR_INVALID_CODE,
39 // Callback when the player needs decoding resources.
40 typedef base::Callback<void(int player_id)> RequestMediaResourcesCB;
42 // Passing an external java surface object to the player.
43 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface) = 0;
45 // Start playing the media.
46 virtual void Start() = 0;
49 virtual void Pause(bool is_media_related_action) = 0;
51 // Seek to a particular position, based on renderer signaling actual seek
52 // with MediaPlayerHostMsg_Seek. If eventual success, OnSeekComplete() will be
54 virtual void SeekTo(base::TimeDelta timestamp) = 0;
56 // Release the player resources.
57 virtual void Release() = 0;
59 // Set the player volume.
60 virtual void SetVolume(double volume) = 0;
62 // Get the media information from the player.
63 virtual int GetVideoWidth() = 0;
64 virtual int GetVideoHeight() = 0;
65 virtual base::TimeDelta GetDuration() = 0;
66 virtual base::TimeDelta GetCurrentTime() = 0;
67 virtual bool IsPlaying() = 0;
68 virtual bool IsPlayerReady() = 0;
69 virtual bool CanPause() = 0;
70 virtual bool CanSeekForward() = 0;
71 virtual bool CanSeekBackward() = 0;
72 virtual GURL GetUrl();
73 virtual GURL GetFirstPartyForCookies();
75 // Associates the |cdm| with this player.
76 virtual void SetCdm(BrowserCdm* cdm);
78 int player_id() { return player_id_; }
80 GURL frame_url() { return frame_url_; }
83 MediaPlayerAndroid(int player_id,
84 MediaPlayerManager* manager,
85 const RequestMediaResourcesCB& request_media_resources_cb,
86 const GURL& frame_url);
88 // TODO(qinmin): Simplify the MediaPlayerListener class to only listen to
89 // media interrupt events. And have a separate child class to listen to all
90 // the events needed by MediaPlayerBridge. http://crbug.com/422597.
91 // MediaPlayerListener callbacks.
92 virtual void OnVideoSizeChanged(int width, int height);
93 virtual void OnMediaError(int error_type);
94 virtual void OnBufferingUpdate(int percent);
95 virtual void OnPlaybackComplete();
96 virtual void OnMediaInterrupted();
97 virtual void OnSeekComplete();
98 virtual void OnMediaPrepared();
100 // Attach/Detaches |listener_| for listening to all the media events. If
101 // |j_media_player| is NULL, |listener_| only listens to the system media
102 // events. Otherwise, it also listens to the events from |j_media_player|.
103 void AttachListener(jobject j_media_player);
104 void DetachListener();
106 MediaPlayerManager* manager() { return manager_; }
108 RequestMediaResourcesCB request_media_resources_cb_;
111 friend class MediaPlayerListener;
113 // Player ID assigned to this player.
116 // Resource manager for all the media players.
117 MediaPlayerManager* manager_;
119 // Url for the frame that contains this player.
122 // Listener object that listens to all the media player events.
123 scoped_ptr<MediaPlayerListener> listener_;
125 // Weak pointer passed to |listener_| for callbacks.
126 // NOTE: Weak pointers must be invalidated before all other member variables.
127 base::WeakPtrFactory<MediaPlayerAndroid> weak_factory_;
129 DISALLOW_COPY_AND_ASSIGN(MediaPlayerAndroid);
134 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_ANDROID_H_