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 #include "content/renderer/media/android/webmediaplayer_proxy_android.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/message_loop/message_loop.h"
      9 #include "content/common/media/media_player_messages_android.h"
     10 #include "content/renderer/media/android/renderer_media_player_manager.h"
     11 #include "content/renderer/media/android/webmediaplayer_android.h"
     12 
     13 namespace content {
     14 
     15 WebMediaPlayerProxyAndroid::WebMediaPlayerProxyAndroid(
     16     RenderView* render_view,
     17     RendererMediaPlayerManager* manager)
     18     : RenderViewObserver(render_view), manager_(manager) {}
     19 
     20 WebMediaPlayerProxyAndroid::~WebMediaPlayerProxyAndroid() {
     21   Send(new MediaPlayerHostMsg_DestroyAllMediaPlayers(routing_id()));
     22 }
     23 
     24 bool WebMediaPlayerProxyAndroid::OnMessageReceived(const IPC::Message& msg) {
     25   bool handled = true;
     26   IPC_BEGIN_MESSAGE_MAP(WebMediaPlayerProxyAndroid, msg)
     27     IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaMetadataChanged,
     28                         OnMediaMetadataChanged)
     29     IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaPlaybackCompleted,
     30                         OnMediaPlaybackCompleted)
     31     IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaBufferingUpdate,
     32                         OnMediaBufferingUpdate)
     33     IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaSeekCompleted, OnMediaSeekCompleted)
     34     IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaError, OnMediaError)
     35     IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaVideoSizeChanged,
     36                         OnVideoSizeChanged)
     37     IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaTimeUpdate, OnTimeUpdate)
     38     IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaPlayerReleased,
     39                         OnMediaPlayerReleased)
     40     IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidEnterFullscreen, OnDidEnterFullscreen)
     41     IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidExitFullscreen, OnDidExitFullscreen)
     42     IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidMediaPlayerPlay, OnPlayerPlay)
     43     IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidMediaPlayerPause, OnPlayerPause)
     44     IPC_MESSAGE_HANDLER(MediaPlayerMsg_ReadFromDemuxer, OnReadFromDemuxer)
     45     IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaSeekRequest, OnMediaSeekRequest)
     46     IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaConfigRequest, OnMediaConfigRequest)
     47     IPC_MESSAGE_HANDLER(MediaKeysMsg_KeyAdded, OnKeyAdded)
     48     IPC_MESSAGE_HANDLER(MediaKeysMsg_KeyError, OnKeyError)
     49     IPC_MESSAGE_HANDLER(MediaKeysMsg_KeyMessage, OnKeyMessage)
     50   IPC_MESSAGE_UNHANDLED(handled = false)
     51   IPC_END_MESSAGE_MAP()
     52   return handled;
     53 }
     54 
     55 void WebMediaPlayerProxyAndroid::Initialize(
     56     int player_id,
     57     const GURL& url,
     58     media::MediaPlayerAndroid::SourceType source_type,
     59     const GURL& first_party_for_cookies) {
     60   Send(new MediaPlayerHostMsg_Initialize(
     61       routing_id(), player_id, url, source_type, first_party_for_cookies));
     62 }
     63 
     64 void WebMediaPlayerProxyAndroid::Start(int player_id) {
     65   Send(new MediaPlayerHostMsg_Start(routing_id(), player_id));
     66 }
     67 
     68 void WebMediaPlayerProxyAndroid::Pause(int player_id) {
     69   Send(new MediaPlayerHostMsg_Pause(routing_id(), player_id));
     70 }
     71 
     72 void WebMediaPlayerProxyAndroid::Seek(int player_id, base::TimeDelta time) {
     73   Send(new MediaPlayerHostMsg_Seek(routing_id(), player_id, time));
     74 }
     75 
     76 void WebMediaPlayerProxyAndroid::SetVolume(int player_id, double volume) {
     77   Send(new MediaPlayerHostMsg_SetVolume(routing_id(), player_id, volume));
     78 }
     79 
     80 void WebMediaPlayerProxyAndroid::ReleaseResources(int player_id) {
     81   Send(new MediaPlayerHostMsg_Release(routing_id(), player_id));
     82 }
     83 
     84 void WebMediaPlayerProxyAndroid::DestroyPlayer(int player_id) {
     85   Send(new MediaPlayerHostMsg_DestroyMediaPlayer(routing_id(), player_id));
     86 }
     87 
     88 void WebMediaPlayerProxyAndroid::OnMediaMetadataChanged(
     89     int player_id,
     90     base::TimeDelta duration,
     91     int width,
     92     int height,
     93     bool success) {
     94   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
     95   if (player)
     96     player->OnMediaMetadataChanged(duration, width, height, success);
     97 }
     98 
     99 void WebMediaPlayerProxyAndroid::OnMediaPlaybackCompleted(int player_id) {
    100   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    101   if (player)
    102     player->OnPlaybackComplete();
    103 }
    104 
    105 void WebMediaPlayerProxyAndroid::OnMediaBufferingUpdate(int player_id,
    106                                                         int percent) {
    107   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    108   if (player)
    109     player->OnBufferingUpdate(percent);
    110 }
    111 
    112 void WebMediaPlayerProxyAndroid::OnMediaSeekCompleted(
    113     int player_id,
    114     base::TimeDelta current_time) {
    115   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    116   if (player)
    117     player->OnSeekComplete(current_time);
    118 }
    119 
    120 void WebMediaPlayerProxyAndroid::OnMediaError(int player_id, int error) {
    121   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    122   if (player)
    123     player->OnMediaError(error);
    124 }
    125 
    126 void WebMediaPlayerProxyAndroid::OnVideoSizeChanged(int player_id,
    127                                                     int width,
    128                                                     int height) {
    129   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    130   if (player)
    131     player->OnVideoSizeChanged(width, height);
    132 }
    133 
    134 void WebMediaPlayerProxyAndroid::OnTimeUpdate(int player_id,
    135                                               base::TimeDelta current_time) {
    136   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    137   if (player)
    138     player->OnTimeUpdate(current_time);
    139 }
    140 
    141 void WebMediaPlayerProxyAndroid::OnMediaPlayerReleased(int player_id) {
    142   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    143   if (player)
    144     player->OnPlayerReleased();
    145 }
    146 
    147 void WebMediaPlayerProxyAndroid::OnDidEnterFullscreen(int player_id) {
    148   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    149   if (player)
    150     player->OnDidEnterFullscreen();
    151 }
    152 
    153 void WebMediaPlayerProxyAndroid::OnDidExitFullscreen(int player_id) {
    154   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    155   if (player)
    156     player->OnDidExitFullscreen();
    157 }
    158 
    159 void WebMediaPlayerProxyAndroid::OnPlayerPlay(int player_id) {
    160   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    161   if (player)
    162     player->OnMediaPlayerPlay();
    163 }
    164 
    165 void WebMediaPlayerProxyAndroid::OnPlayerPause(int player_id) {
    166   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    167   if (player)
    168     player->OnMediaPlayerPause();
    169 }
    170 
    171 void WebMediaPlayerProxyAndroid::EnterFullscreen(int player_id) {
    172   Send(new MediaPlayerHostMsg_EnterFullscreen(routing_id(), player_id));
    173 }
    174 
    175 void WebMediaPlayerProxyAndroid::ExitFullscreen(int player_id) {
    176   Send(new MediaPlayerHostMsg_ExitFullscreen(routing_id(), player_id));
    177 }
    178 
    179 void WebMediaPlayerProxyAndroid::ReadFromDemuxerAck(
    180     int player_id,
    181     const media::MediaPlayerHostMsg_ReadFromDemuxerAck_Params& params) {
    182   Send(new MediaPlayerHostMsg_ReadFromDemuxerAck(
    183       routing_id(), player_id, params));
    184 }
    185 
    186 void WebMediaPlayerProxyAndroid::SeekRequestAck(int player_id,
    187                                                 unsigned seek_request_id) {
    188   Send(new MediaPlayerHostMsg_MediaSeekRequestAck(
    189       routing_id(), player_id, seek_request_id));
    190 }
    191 
    192 #if defined(GOOGLE_TV)
    193 void WebMediaPlayerProxyAndroid::RequestExternalSurface(
    194     int player_id,
    195     const gfx::RectF& geometry) {
    196   Send(new MediaPlayerHostMsg_NotifyExternalSurface(
    197       routing_id(), player_id, true, geometry));
    198 }
    199 
    200 void WebMediaPlayerProxyAndroid::DidCommitCompositorFrame() {
    201   std::map<int, gfx::RectF> geometry_change;
    202   manager_->RetrieveGeometryChanges(&geometry_change);
    203   for (std::map<int, gfx::RectF>::iterator it = geometry_change.begin();
    204        it != geometry_change.end();
    205        ++it) {
    206     Send(new MediaPlayerHostMsg_NotifyExternalSurface(
    207         routing_id(), it->first, false, it->second));
    208   }
    209 }
    210 #endif
    211 
    212 void WebMediaPlayerProxyAndroid::OnReadFromDemuxer(
    213     int player_id,
    214     media::DemuxerStream::Type type) {
    215   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    216   if (player)
    217     player->OnReadFromDemuxer(type);
    218 }
    219 
    220 void WebMediaPlayerProxyAndroid::DemuxerReady(
    221     int player_id,
    222     const media::MediaPlayerHostMsg_DemuxerReady_Params& params) {
    223   Send(new MediaPlayerHostMsg_DemuxerReady(routing_id(), player_id, params));
    224 }
    225 
    226 void WebMediaPlayerProxyAndroid::DurationChanged(
    227     int player_id,
    228     const base::TimeDelta& duration) {
    229   Send(new MediaPlayerHostMsg_DurationChanged(
    230       routing_id(), player_id, duration));
    231 }
    232 
    233 void WebMediaPlayerProxyAndroid::InitializeCDM(int media_keys_id,
    234                                                const std::vector<uint8>& uuid) {
    235   Send(new MediaKeysHostMsg_InitializeCDM(routing_id(), media_keys_id, uuid));
    236 }
    237 
    238 void WebMediaPlayerProxyAndroid::GenerateKeyRequest(
    239     int media_keys_id,
    240     const std::string& type,
    241     const std::vector<uint8>& init_data) {
    242   Send(new MediaKeysHostMsg_GenerateKeyRequest(
    243       routing_id(), media_keys_id, type, init_data));
    244 }
    245 
    246 void WebMediaPlayerProxyAndroid::AddKey(int media_keys_id,
    247                                         const std::vector<uint8>& key,
    248                                         const std::vector<uint8>& init_data,
    249                                         const std::string& session_id) {
    250   Send(new MediaKeysHostMsg_AddKey(
    251       routing_id(), media_keys_id, key, init_data, session_id));
    252 }
    253 
    254 void WebMediaPlayerProxyAndroid::CancelKeyRequest(
    255     int media_keys_id,
    256     const std::string& session_id) {
    257   Send(new MediaKeysHostMsg_CancelKeyRequest(
    258       routing_id(), media_keys_id, session_id));
    259 }
    260 
    261 WebMediaPlayerAndroid* WebMediaPlayerProxyAndroid::GetWebMediaPlayer(
    262     int player_id) {
    263   return static_cast<WebMediaPlayerAndroid*>(
    264       manager_->GetMediaPlayer(player_id));
    265 }
    266 
    267 void WebMediaPlayerProxyAndroid::OnMediaSeekRequest(
    268     int player_id,
    269     base::TimeDelta time_to_seek,
    270     unsigned seek_request_id) {
    271   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    272   if (player)
    273     player->OnMediaSeekRequest(time_to_seek, seek_request_id);
    274 }
    275 
    276 void WebMediaPlayerProxyAndroid::OnMediaConfigRequest(int player_id) {
    277   WebMediaPlayerAndroid* player = GetWebMediaPlayer(player_id);
    278   if (player)
    279     player->OnMediaConfigRequest();
    280 }
    281 
    282 void WebMediaPlayerProxyAndroid::OnKeyAdded(int media_keys_id,
    283                                             const std::string& session_id) {
    284   WebMediaPlayerAndroid* player = GetWebMediaPlayer(media_keys_id);
    285   if (player)
    286     player->OnKeyAdded(session_id);
    287 }
    288 
    289 void WebMediaPlayerProxyAndroid::OnKeyError(
    290     int media_keys_id,
    291     const std::string& session_id,
    292     media::MediaKeys::KeyError error_code,
    293     int system_code) {
    294   WebMediaPlayerAndroid* player = GetWebMediaPlayer(media_keys_id);
    295   if (player)
    296     player->OnKeyError(session_id, error_code, system_code);
    297 }
    298 
    299 void WebMediaPlayerProxyAndroid::OnKeyMessage(
    300     int media_keys_id,
    301     const std::string& session_id,
    302     const std::vector<uint8>& message,
    303     const std::string& destination_url) {
    304   WebMediaPlayerAndroid* player = GetWebMediaPlayer(media_keys_id);
    305   if (player)
    306     player->OnKeyMessage(session_id, message, destination_url);
    307 }
    308 
    309 }  // namespace content
    310