Home | History | Annotate | Download | only in android
      1 // Copyright 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 CONTENT_RENDERER_MEDIA_ANDROID_RENDERER_MEDIA_PLAYER_MANAGER_H_
      6 #define CONTENT_RENDERER_MEDIA_ANDROID_RENDERER_MEDIA_PLAYER_MANAGER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/time/time.h"
     13 #include "content/common/media/media_player_messages_enums_android.h"
     14 #include "content/public/renderer/render_frame_observer.h"
     15 #include "media/base/android/media_player_android.h"
     16 #include "url/gurl.h"
     17 
     18 namespace blink {
     19 class WebFrame;
     20 }
     21 
     22 namespace gfx {
     23 class RectF;
     24 }
     25 
     26 struct MediaPlayerHostMsg_Initialize_Params;
     27 
     28 namespace content {
     29 class WebMediaPlayerAndroid;
     30 
     31 // Class for managing all the WebMediaPlayerAndroid objects in the same
     32 // RenderFrame.
     33 class RendererMediaPlayerManager : public RenderFrameObserver {
     34  public:
     35   // Constructs a RendererMediaPlayerManager object for the |render_frame|.
     36   explicit RendererMediaPlayerManager(RenderFrame* render_frame);
     37   virtual ~RendererMediaPlayerManager();
     38 
     39   // RenderFrameObserver overrides.
     40   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
     41 
     42   // Initializes a MediaPlayerAndroid object in browser process.
     43   void Initialize(MediaPlayerHostMsg_Initialize_Type type,
     44                   int player_id,
     45                   const GURL& url,
     46                   const GURL& first_party_for_cookies,
     47                   int demuxer_client_id,
     48                   const GURL& frame_url,
     49                   bool allow_credentials);
     50 
     51   // Starts the player.
     52   void Start(int player_id);
     53 
     54   // Pauses the player.
     55   // is_media_related_action should be true if this pause is coming from an
     56   // an action that explicitly pauses the video (user pressing pause, JS, etc.)
     57   // Otherwise it should be false if Pause is being called due to other reasons
     58   // (cleanup, freeing resources, etc.)
     59   void Pause(int player_id, bool is_media_related_action);
     60 
     61   // Performs seek on the player.
     62   void Seek(int player_id, const base::TimeDelta& time);
     63 
     64   // Sets the player volume.
     65   void SetVolume(int player_id, double volume);
     66 
     67   // Sets the poster image.
     68   void SetPoster(int player_id, const GURL& poster);
     69 
     70   // Releases resources for the player.
     71   void ReleaseResources(int player_id);
     72 
     73   // Destroys the player in the browser process
     74   void DestroyPlayer(int player_id);
     75 
     76   // Requests the player to enter fullscreen.
     77   void EnterFullscreen(int player_id, blink::WebFrame* frame);
     78 
     79   // Requests the player to exit fullscreen.
     80   void ExitFullscreen(int player_id);
     81 
     82   // Requests the player with |player_id| to use the CDM with |cdm_id|.
     83   // Does nothing if |cdm_id| is kInvalidCdmId.
     84   // TODO(xhwang): Update this when we implement setCdm(0).
     85   void SetCdm(int player_id, int cdm_id);
     86 
     87 #if defined(VIDEO_HOLE)
     88   // Requests an external surface for out-of-band compositing.
     89   void RequestExternalSurface(int player_id, const gfx::RectF& geometry);
     90 
     91   // RenderFrameObserver overrides.
     92   virtual void DidCommitCompositorFrame() OVERRIDE;
     93 
     94   // Returns true if a media player should use video-overlay for the embedded
     95   // encrypted video.
     96   bool ShouldUseVideoOverlayForEmbeddedEncryptedVideo();
     97 #endif  // defined(VIDEO_HOLE)
     98 
     99   // Registers and unregisters a WebMediaPlayerAndroid object.
    100   int RegisterMediaPlayer(WebMediaPlayerAndroid* player);
    101   void UnregisterMediaPlayer(int player_id);
    102 
    103   // Checks whether a player can enter fullscreen.
    104   bool CanEnterFullscreen(blink::WebFrame* frame);
    105 
    106   // Called when a player entered or exited fullscreen.
    107   void DidEnterFullscreen(blink::WebFrame* frame);
    108   void DidExitFullscreen();
    109 
    110   // Checks whether the Webframe is in fullscreen.
    111   bool IsInFullscreen(blink::WebFrame* frame);
    112 
    113   // True if a newly created media player should enter fullscreen.
    114   bool ShouldEnterFullscreen(blink::WebFrame* frame);
    115 
    116   // Gets the pointer to WebMediaPlayerAndroid given the |player_id|.
    117   WebMediaPlayerAndroid* GetMediaPlayer(int player_id);
    118 
    119 #if defined(VIDEO_HOLE)
    120   // Gets the list of media players with video geometry changes.
    121   void RetrieveGeometryChanges(std::map<int, gfx::RectF>* changes);
    122 #endif  // defined(VIDEO_HOLE)
    123 
    124  private:
    125   // Message handlers.
    126   void OnMediaMetadataChanged(int player_id,
    127                               base::TimeDelta duration,
    128                               int width,
    129                               int height,
    130                               bool success);
    131   void OnMediaPlaybackCompleted(int player_id);
    132   void OnMediaBufferingUpdate(int player_id, int percent);
    133   void OnSeekRequest(int player_id, const base::TimeDelta& time_to_seek);
    134   void OnSeekCompleted(int player_id, const base::TimeDelta& current_time);
    135   void OnMediaError(int player_id, int error);
    136   void OnVideoSizeChanged(int player_id, int width, int height);
    137   void OnTimeUpdate(int player_id, base::TimeDelta current_time);
    138   void OnMediaPlayerReleased(int player_id);
    139   void OnConnectedToRemoteDevice(int player_id,
    140       const std::string& remote_playback_message);
    141   void OnDisconnectedFromRemoteDevice(int player_id);
    142   void OnDidExitFullscreen(int player_id);
    143   void OnDidEnterFullscreen(int player_id);
    144   void OnPlayerPlay(int player_id);
    145   void OnPlayerPause(int player_id);
    146   void OnRequestFullscreen(int player_id);
    147   void OnPauseVideo();
    148 
    149   // Release all video player resources.
    150   // If something is in progress the resource will not be freed. It will
    151   // only be freed once the tab is destroyed or if the user navigates away
    152   // via WebMediaPlayerAndroid::Destroy.
    153   void ReleaseVideoResources();
    154 
    155   // Info for all available WebMediaPlayerAndroid on a page; kept so that
    156   // we can enumerate them to send updates about tab focus and visibility.
    157   std::map<int, WebMediaPlayerAndroid*> media_players_;
    158 
    159   int next_media_player_id_;
    160 
    161   // WebFrame of the fullscreen video.
    162   blink::WebFrame* fullscreen_frame_;
    163 
    164   // WebFrame of pending fullscreen request.
    165   blink::WebFrame* pending_fullscreen_frame_;
    166 
    167   DISALLOW_COPY_AND_ASSIGN(RendererMediaPlayerManager);
    168 };
    169 
    170 }  // namespace content
    171 
    172 #endif  // CONTENT_RENDERER_MEDIA_ANDROID_RENDERER_MEDIA_PLAYER_MANAGER_H_
    173