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/renderer_demuxer_android.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/message_loop/message_loop.h"
      9 #include "content/child/thread_safe_sender.h"
     10 #include "content/common/media/media_player_messages_android.h"
     11 #include "content/renderer/media/android/media_source_delegate.h"
     12 #include "content/renderer/media/android/renderer_media_player_manager.h"
     13 #include "content/renderer/media/android/webmediaplayer_android.h"
     14 #include "content/renderer/render_thread_impl.h"
     15 
     16 namespace content {
     17 
     18 RendererDemuxerAndroid::RendererDemuxerAndroid()
     19     : thread_safe_sender_(RenderThreadImpl::current()->thread_safe_sender()),
     20       media_message_loop_(
     21           RenderThreadImpl::current()->GetMediaThreadMessageLoopProxy()) {}
     22 
     23 RendererDemuxerAndroid::~RendererDemuxerAndroid() {}
     24 
     25 int RendererDemuxerAndroid::GetNextDemuxerClientID() {
     26   // Don't use zero for IDs since it can be interpreted as having no ID.
     27   return next_demuxer_client_id_.GetNext() + 1;
     28 }
     29 
     30 void RendererDemuxerAndroid::AddDelegate(int demuxer_client_id,
     31                                          MediaSourceDelegate* delegate) {
     32   DCHECK(media_message_loop_->BelongsToCurrentThread());
     33   delegates_.AddWithID(delegate, demuxer_client_id);
     34 }
     35 
     36 void RendererDemuxerAndroid::RemoveDelegate(int demuxer_client_id) {
     37   DCHECK(media_message_loop_->BelongsToCurrentThread());
     38   delegates_.Remove(demuxer_client_id);
     39 }
     40 
     41 bool RendererDemuxerAndroid::OnMessageReceived(const IPC::Message& message) {
     42   switch (message.type()) {
     43     case MediaPlayerMsg_DemuxerSeekRequest::ID:
     44     case MediaPlayerMsg_ReadFromDemuxer::ID:
     45     case MediaPlayerMsg_MediaConfigRequest::ID:
     46       media_message_loop_->PostTask(FROM_HERE, base::Bind(
     47           &RendererDemuxerAndroid::DispatchMessage, this, message));
     48       return true;
     49    }
     50   return false;
     51 }
     52 
     53 void RendererDemuxerAndroid::DemuxerReady(
     54     int demuxer_client_id,
     55     const media::DemuxerConfigs& configs) {
     56   thread_safe_sender_->Send(new MediaPlayerHostMsg_DemuxerReady(
     57       demuxer_client_id, configs));
     58 }
     59 
     60 void RendererDemuxerAndroid::ReadFromDemuxerAck(
     61     int demuxer_client_id,
     62     const media::DemuxerData& data) {
     63   thread_safe_sender_->Send(new MediaPlayerHostMsg_ReadFromDemuxerAck(
     64       demuxer_client_id, data));
     65 }
     66 
     67 void RendererDemuxerAndroid::DemuxerSeekDone(
     68     int demuxer_client_id,
     69     const base::TimeDelta& actual_browser_seek_time) {
     70   thread_safe_sender_->Send(new MediaPlayerHostMsg_DemuxerSeekDone(
     71       demuxer_client_id, actual_browser_seek_time));
     72 }
     73 
     74 void RendererDemuxerAndroid::DurationChanged(int demuxer_client_id,
     75                                              const base::TimeDelta& duration) {
     76   thread_safe_sender_->Send(new MediaPlayerHostMsg_DurationChanged(
     77       demuxer_client_id, duration));
     78 }
     79 
     80 void RendererDemuxerAndroid::DispatchMessage(const IPC::Message& message) {
     81   IPC_BEGIN_MESSAGE_MAP(RendererDemuxerAndroid, message)
     82     IPC_MESSAGE_HANDLER(MediaPlayerMsg_DemuxerSeekRequest, OnDemuxerSeekRequest)
     83     IPC_MESSAGE_HANDLER(MediaPlayerMsg_ReadFromDemuxer, OnReadFromDemuxer)
     84     IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaConfigRequest, OnMediaConfigRequest)
     85   IPC_END_MESSAGE_MAP()
     86 }
     87 
     88 void RendererDemuxerAndroid::OnReadFromDemuxer(
     89     int demuxer_client_id,
     90     media::DemuxerStream::Type type) {
     91   MediaSourceDelegate* delegate = delegates_.Lookup(demuxer_client_id);
     92   if (delegate)
     93     delegate->OnReadFromDemuxer(type);
     94 }
     95 
     96 void RendererDemuxerAndroid::OnDemuxerSeekRequest(
     97     int demuxer_client_id,
     98     const base::TimeDelta& time_to_seek,
     99     bool is_browser_seek) {
    100   MediaSourceDelegate* delegate = delegates_.Lookup(demuxer_client_id);
    101   if (delegate)
    102     delegate->Seek(time_to_seek, is_browser_seek);
    103 }
    104 
    105 void RendererDemuxerAndroid::OnMediaConfigRequest(int demuxer_client_id) {
    106   MediaSourceDelegate* delegate = delegates_.Lookup(demuxer_client_id);
    107   if (delegate)
    108     delegate->OnMediaConfigRequest();
    109 }
    110 
    111 }  // namespace content
    112