Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / media / base / android / media_player_android.h
blobd9091d4195b414f065ab9a5ae319a373a43a2b11
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/gl/android/scoped_java_surface.h"
17 #include "url/gurl.h"
19 namespace media {
21 class BrowserCdm;
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 {
28 public:
29 virtual ~MediaPlayerAndroid();
31 // Error types for MediaErrorCB.
32 enum MediaErrorType {
33 MEDIA_ERROR_FORMAT,
34 MEDIA_ERROR_DECODE,
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;
48 // Pause the media.
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
53 // called.
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_; }
82 protected:
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();
105 void SetAudible(bool is_audible);
107 MediaPlayerManager* manager() { return manager_; }
109 RequestMediaResourcesCB request_media_resources_cb_;
111 private:
112 friend class MediaPlayerListener;
114 // Player ID assigned to this player.
115 int player_id_;
117 // Resource manager for all the media players.
118 MediaPlayerManager* manager_;
120 // Url for the frame that contains this player.
121 GURL frame_url_;
123 // Listener object that listens to all the media player events.
124 scoped_ptr<MediaPlayerListener> listener_;
126 // Maintains the audible state of the player, true if it is playing sound.
127 bool is_audible_;
129 // Weak pointer passed to |listener_| for callbacks.
130 // NOTE: Weak pointers must be invalidated before all other member variables.
131 base::WeakPtrFactory<MediaPlayerAndroid> weak_factory_;
133 DISALLOW_COPY_AND_ASSIGN(MediaPlayerAndroid);
136 } // namespace media
138 #endif // MEDIA_BASE_ANDROID_MEDIA_PLAYER_ANDROID_H_