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/time/time.h"
13#include "media/base/android/demuxer_stream_player_params.h"
14#include "media/base/media_export.h"
15#include "ui/gl/android/scoped_java_surface.h"
16#include "url/gurl.h"
17
18namespace media {
19
20class MediaDrmBridge;
21class MediaPlayerManager;
22
23// This class serves as the base class for different media player
24// implementations on Android. Subclasses need to provide their own
25// MediaPlayerAndroid::Create() implementation.
26class MEDIA_EXPORT MediaPlayerAndroid {
27 public:
28  virtual ~MediaPlayerAndroid();
29
30  // Error types for MediaErrorCB.
31  enum MediaErrorType {
32    MEDIA_ERROR_FORMAT,
33    MEDIA_ERROR_DECODE,
34    MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK,
35    MEDIA_ERROR_INVALID_CODE,
36  };
37
38  // Types of media source that this object will play.
39  enum SourceType {
40    SOURCE_TYPE_URL,
41    SOURCE_TYPE_MSE,     // W3C Media Source Extensions
42    SOURCE_TYPE_STREAM,  // W3C Media Stream, e.g. getUserMedia().
43  };
44
45  // Construct a MediaPlayerAndroid object with all the needed media player
46  // callbacks. This object needs to call |manager_|'s RequestMediaResources()
47  // before decoding the media stream. This allows |manager_| to track
48  // unused resources and free them when needed. On the other hand, it needs
49  // to call ReleaseMediaResources() when it is done with decoding.
50  // |load_media_resource| indcates whether the media resource should be
51  // initialized after the player created.
52  static MediaPlayerAndroid* Create(int player_id,
53                                    const GURL& url,
54                                    SourceType source_type,
55                                    const GURL& first_party_for_cookies,
56                                    bool hide_url_log,
57                                    MediaPlayerManager* manager,
58                                    bool load_media_resource);
59
60  // Passing an external java surface object to the player.
61  virtual void SetVideoSurface(gfx::ScopedJavaSurface surface) = 0;
62
63  // Start playing the media.
64  virtual void Start() = 0;
65
66  // Pause the media.
67  virtual void Pause() = 0;
68
69  // Seek to a particular position. When succeeds, OnSeekComplete() will be
70  // called. Otherwise, nothing will happen.
71  virtual void SeekTo(base::TimeDelta time) = 0;
72
73  // Release the player resources.
74  virtual void Release() = 0;
75
76  // Set the player volume.
77  virtual void SetVolume(double volume) = 0;
78
79  // Get the media information from the player.
80  virtual int GetVideoWidth() = 0;
81  virtual int GetVideoHeight() = 0;
82  virtual base::TimeDelta GetDuration() = 0;
83  virtual base::TimeDelta GetCurrentTime() = 0;
84  virtual bool IsPlaying() = 0;
85  virtual bool IsPlayerReady() = 0;
86  virtual bool CanPause() = 0;
87  virtual bool CanSeekForward() = 0;
88  virtual bool CanSeekBackward() = 0;
89  virtual GURL GetUrl();
90  virtual GURL GetFirstPartyForCookies();
91
92  // Methods for DemuxerStreamPlayer.
93  // Informs DemuxerStreamPlayer that the demuxer is ready.
94  virtual void DemuxerReady(
95      const MediaPlayerHostMsg_DemuxerReady_Params& params);
96  // Called when the requested data is received from the demuxer.
97  virtual void ReadFromDemuxerAck(
98      const MediaPlayerHostMsg_ReadFromDemuxerAck_Params& params);
99
100  // Called when a seek request is acked by the render process.
101  virtual void OnSeekRequestAck(unsigned seek_request_id);
102
103  // Called when the demuxer has changed the duration.
104  virtual void DurationChanged(const base::TimeDelta& duration);
105
106  // Pass a drm bridge to a player.
107  virtual void SetDrmBridge(MediaDrmBridge* drm_bridge);
108
109  int player_id() { return player_id_; }
110
111 protected:
112  MediaPlayerAndroid(int player_id,
113                     MediaPlayerManager* manager);
114
115  // Called when player status changes.
116  virtual void OnMediaError(int error_type);
117  virtual void OnVideoSizeChanged(int width, int height);
118  virtual void OnBufferingUpdate(int percent);
119  virtual void OnPlaybackComplete();
120  virtual void OnSeekComplete();
121  virtual void OnMediaMetadataChanged(
122      base::TimeDelta duration, int width, int height, bool success);
123  virtual void OnMediaInterrupted();
124  virtual void OnTimeUpdated();
125
126  // Request or release decoding resources from |manager_|.
127  virtual void RequestMediaResourcesFromManager();
128  virtual void ReleaseMediaResourcesFromManager();
129
130  MediaPlayerManager* manager() { return manager_; }
131
132 private:
133  // Player ID assigned to this player.
134  int player_id_;
135
136  // Resource manager for all the media players.
137  MediaPlayerManager* manager_;
138
139  DISALLOW_COPY_AND_ASSIGN(MediaPlayerAndroid);
140};
141
142}  // namespace media
143
144#endif  // MEDIA_BASE_ANDROID_MEDIA_PLAYER_ANDROID_H_
145