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