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.
4
5#ifndef MEDIA_BASE_ANDROID_MEDIA_PLAYER_ANDROID_H_
6#define MEDIA_BASE_ANDROID_MEDIA_PLAYER_ANDROID_H_
7
8#include <jni.h>
9#include <string>
10
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"
18
19namespace media {
20
21class BrowserCdm;
22class MediaPlayerManager;
23
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.
27class MEDIA_EXPORT MediaPlayerAndroid {
28 public:
29  virtual ~MediaPlayerAndroid();
30
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,
37  };
38
39  // Callback when the player needs decoding resources.
40  typedef base::Callback<void(int player_id)> RequestMediaResourcesCB;
41
42  // Passing an external java surface object to the player.
43  virtual void SetVideoSurface(gfx::ScopedJavaSurface surface) = 0;
44
45  // Start playing the media.
46  virtual void Start() = 0;
47
48  // Pause the media.
49  virtual void Pause(bool is_media_related_action) = 0;
50
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;
55
56  // Release the player resources.
57  virtual void Release() = 0;
58
59  // Set the player volume.
60  virtual void SetVolume(double volume) = 0;
61
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();
74
75  // Associates the |cdm| with this player.
76  virtual void SetCdm(BrowserCdm* cdm);
77
78  int player_id() { return player_id_; }
79
80  GURL frame_url() { return frame_url_; }
81
82 protected:
83  MediaPlayerAndroid(int player_id,
84                     MediaPlayerManager* manager,
85                     const RequestMediaResourcesCB& request_media_resources_cb,
86                     const GURL& frame_url);
87
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();
99
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
106  MediaPlayerManager* manager() { return manager_; }
107
108  RequestMediaResourcesCB request_media_resources_cb_;
109
110 private:
111  friend class MediaPlayerListener;
112
113  // Player ID assigned to this player.
114  int player_id_;
115
116  // Resource manager for all the media players.
117  MediaPlayerManager* manager_;
118
119  // Url for the frame that contains this player.
120  GURL frame_url_;
121
122  // Listener object that listens to all the media player events.
123  scoped_ptr<MediaPlayerListener> listener_;
124
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_;
128
129  DISALLOW_COPY_AND_ASSIGN(MediaPlayerAndroid);
130};
131
132}  // namespace media
133
134#endif  // MEDIA_BASE_ANDROID_MEDIA_PLAYER_ANDROID_H_
135