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