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/browser/media/android/browser_demuxer_android.h"
      6 
      7 #include "content/common/media/media_player_messages_android.h"
      8 
      9 namespace content {
     10 
     11 class BrowserDemuxerAndroid::Internal : public media::DemuxerAndroid {
     12  public:
     13   Internal(const scoped_refptr<BrowserDemuxerAndroid>& demuxer,
     14            int demuxer_client_id)
     15       : demuxer_(demuxer),
     16         demuxer_client_id_(demuxer_client_id) {}
     17 
     18   virtual ~Internal() {
     19     DCHECK(ClientIDExists()) << demuxer_client_id_;
     20     demuxer_->RemoveDemuxerClient(demuxer_client_id_);
     21   }
     22 
     23   // media::DemuxerAndroid implementation.
     24   virtual void Initialize(media::DemuxerAndroidClient* client) OVERRIDE {
     25     DCHECK(!ClientIDExists()) << demuxer_client_id_;
     26     demuxer_->AddDemuxerClient(demuxer_client_id_, client);
     27   }
     28 
     29   virtual void RequestDemuxerConfigs() OVERRIDE {
     30     DCHECK(ClientIDExists()) << demuxer_client_id_;
     31     demuxer_->Send(new MediaPlayerMsg_MediaConfigRequest(demuxer_client_id_));
     32   }
     33 
     34   virtual void RequestDemuxerData(media::DemuxerStream::Type type) OVERRIDE {
     35     DCHECK(ClientIDExists()) << demuxer_client_id_;
     36     demuxer_->Send(new MediaPlayerMsg_ReadFromDemuxer(
     37         demuxer_client_id_, type));
     38   }
     39 
     40   virtual void RequestDemuxerSeek(
     41       const base::TimeDelta& time_to_seek,
     42       bool is_browser_seek) OVERRIDE {
     43     DCHECK(ClientIDExists()) << demuxer_client_id_;
     44     demuxer_->Send(new MediaPlayerMsg_DemuxerSeekRequest(
     45         demuxer_client_id_, time_to_seek, is_browser_seek));
     46   }
     47 
     48  private:
     49   // Helper for DCHECKing that the ID is still registered.
     50   bool ClientIDExists() {
     51     return demuxer_->demuxer_clients_.Lookup(demuxer_client_id_);
     52   }
     53 
     54   scoped_refptr<BrowserDemuxerAndroid> demuxer_;
     55   int demuxer_client_id_;
     56 
     57   DISALLOW_COPY_AND_ASSIGN(Internal);
     58 };
     59 
     60 BrowserDemuxerAndroid::BrowserDemuxerAndroid() {}
     61 
     62 BrowserDemuxerAndroid::~BrowserDemuxerAndroid() {}
     63 
     64 void BrowserDemuxerAndroid::OverrideThreadForMessage(
     65     const IPC::Message& message,
     66     BrowserThread::ID* thread) {
     67   switch (message.type()) {
     68     case MediaPlayerHostMsg_DemuxerReady::ID:
     69     case MediaPlayerHostMsg_ReadFromDemuxerAck::ID:
     70     case MediaPlayerHostMsg_DurationChanged::ID:
     71     case MediaPlayerHostMsg_DemuxerSeekDone::ID:
     72       *thread = BrowserThread::UI;
     73       return;
     74   }
     75 }
     76 
     77 bool BrowserDemuxerAndroid::OnMessageReceived(const IPC::Message& message,
     78                                               bool* message_was_ok) {
     79   bool handled = true;
     80   IPC_BEGIN_MESSAGE_MAP_EX(BrowserDemuxerAndroid, message, *message_was_ok)
     81     IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DemuxerReady, OnDemuxerReady)
     82     IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_ReadFromDemuxerAck,
     83                         OnReadFromDemuxerAck)
     84     IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DurationChanged,
     85                         OnDurationChanged)
     86     IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DemuxerSeekDone,
     87                         OnDemuxerSeekDone)
     88     IPC_MESSAGE_UNHANDLED(handled = false)
     89   IPC_END_MESSAGE_MAP()
     90   return handled;
     91 }
     92 
     93 scoped_ptr<media::DemuxerAndroid> BrowserDemuxerAndroid::CreateDemuxer(
     94     int demuxer_client_id) {
     95   return scoped_ptr<media::DemuxerAndroid>(
     96       new Internal(this, demuxer_client_id));
     97 }
     98 
     99 void BrowserDemuxerAndroid::AddDemuxerClient(
    100     int demuxer_client_id,
    101     media::DemuxerAndroidClient* client) {
    102   DVLOG(1) << __FUNCTION__ << " peer_pid=" << peer_pid()
    103            << " demuxer_client_id=" << demuxer_client_id;
    104   demuxer_clients_.AddWithID(client, demuxer_client_id);
    105 }
    106 
    107 void BrowserDemuxerAndroid::RemoveDemuxerClient(int demuxer_client_id) {
    108   DVLOG(1) << __FUNCTION__ << " peer_pid=" << peer_pid()
    109            << " demuxer_client_id=" << demuxer_client_id;
    110   demuxer_clients_.Remove(demuxer_client_id);
    111 }
    112 
    113 void BrowserDemuxerAndroid::OnDemuxerReady(
    114     int demuxer_client_id,
    115     const media::DemuxerConfigs& configs) {
    116   media::DemuxerAndroidClient* client =
    117       demuxer_clients_.Lookup(demuxer_client_id);
    118   if (client)
    119     client->OnDemuxerConfigsAvailable(configs);
    120 }
    121 
    122 void BrowserDemuxerAndroid::OnReadFromDemuxerAck(
    123     int demuxer_client_id,
    124     const media::DemuxerData& data) {
    125   media::DemuxerAndroidClient* client =
    126       demuxer_clients_.Lookup(demuxer_client_id);
    127   if (client)
    128     client->OnDemuxerDataAvailable(data);
    129 }
    130 
    131 void BrowserDemuxerAndroid::OnDemuxerSeekDone(
    132     int demuxer_client_id,
    133     const base::TimeDelta& actual_browser_seek_time) {
    134   media::DemuxerAndroidClient* client =
    135       demuxer_clients_.Lookup(demuxer_client_id);
    136   if (client)
    137     client->OnDemuxerSeekDone(actual_browser_seek_time);
    138 }
    139 
    140 void BrowserDemuxerAndroid::OnDurationChanged(int demuxer_client_id,
    141                                               const base::TimeDelta& duration) {
    142   media::DemuxerAndroidClient* client =
    143       demuxer_clients_.Lookup(demuxer_client_id);
    144   if (client)
    145     client->OnDemuxerDurationChanged(duration);
    146 }
    147 
    148 }  // namespace content
    149