Home | History | Annotate | Download | only in android
      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 
     18 namespace media {
     19 
     20 class MediaDrmBridge;
     21 class 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.
     26 class 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   static MediaPlayerAndroid* Create(int player_id,
     51                                     const GURL& url,
     52                                     SourceType source_type,
     53                                     const GURL& first_party_for_cookies,
     54                                     bool hide_url_log,
     55                                     MediaPlayerManager* manager);
     56 
     57   // Passing an external java surface object to the player.
     58   virtual void SetVideoSurface(gfx::ScopedJavaSurface surface) = 0;
     59 
     60   // Start playing the media.
     61   virtual void Start() = 0;
     62 
     63   // Pause the media.
     64   virtual void Pause() = 0;
     65 
     66   // Seek to a particular position. When succeeds, OnSeekComplete() will be
     67   // called. Otherwise, nothing will happen.
     68   virtual void SeekTo(base::TimeDelta time) = 0;
     69 
     70   // Release the player resources.
     71   virtual void Release() = 0;
     72 
     73   // Set the player volume.
     74   virtual void SetVolume(double volume) = 0;
     75 
     76   // Get the media information from the player.
     77   virtual int GetVideoWidth() = 0;
     78   virtual int GetVideoHeight() = 0;
     79   virtual base::TimeDelta GetDuration() = 0;
     80   virtual base::TimeDelta GetCurrentTime() = 0;
     81   virtual bool IsPlaying() = 0;
     82   virtual bool IsPlayerReady() = 0;
     83   virtual bool CanPause() = 0;
     84   virtual bool CanSeekForward() = 0;
     85   virtual bool CanSeekBackward() = 0;
     86   virtual GURL GetUrl();
     87   virtual GURL GetFirstPartyForCookies();
     88 
     89   // Methods for DemuxerStreamPlayer.
     90   // Informs DemuxerStreamPlayer that the demuxer is ready.
     91   virtual void DemuxerReady(
     92       const MediaPlayerHostMsg_DemuxerReady_Params& params);
     93   // Called when the requested data is received from the demuxer.
     94   virtual void ReadFromDemuxerAck(
     95       const MediaPlayerHostMsg_ReadFromDemuxerAck_Params& params);
     96 
     97   // Called when a seek request is acked by the render process.
     98   virtual void OnSeekRequestAck(unsigned seek_request_id);
     99 
    100   // Called when the demuxer has changed the duration.
    101   virtual void DurationChanged(const base::TimeDelta& duration);
    102 
    103   // Pass a drm bridge to a player.
    104   virtual void SetDrmBridge(MediaDrmBridge* drm_bridge);
    105 
    106   int player_id() { return player_id_; }
    107 
    108  protected:
    109   MediaPlayerAndroid(int player_id,
    110                      MediaPlayerManager* manager);
    111 
    112   // Called when player status changes.
    113   virtual void OnMediaError(int error_type);
    114   virtual void OnVideoSizeChanged(int width, int height);
    115   virtual void OnBufferingUpdate(int percent);
    116   virtual void OnPlaybackComplete();
    117   virtual void OnSeekComplete();
    118   virtual void OnMediaMetadataChanged(
    119       base::TimeDelta duration, int width, int height, bool success);
    120   virtual void OnMediaInterrupted();
    121   virtual void OnTimeUpdated();
    122 
    123   // Request or release decoding resources from |manager_|.
    124   virtual void RequestMediaResourcesFromManager();
    125   virtual void ReleaseMediaResourcesFromManager();
    126 
    127   MediaPlayerManager* manager() { return manager_; }
    128 
    129  private:
    130   // Player ID assigned to this player.
    131   int player_id_;
    132 
    133   // Resource manager for all the media players.
    134   MediaPlayerManager* manager_;
    135 
    136   DISALLOW_COPY_AND_ASSIGN(MediaPlayerAndroid);
    137 };
    138 
    139 }  // namespace media
    140 
    141 #endif  // MEDIA_BASE_ANDROID_MEDIA_PLAYER_ANDROID_H_
    142