1d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// found in the LICENSE file. 4d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "content/renderer/media/android/renderer_demuxer_android.h" 6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 7d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/bind.h" 8d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/message_loop/message_loop.h" 968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "content/child/thread_safe_sender.h" 10d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "content/common/media/media_player_messages_android.h" 11d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "content/renderer/media/android/media_source_delegate.h" 12d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "content/renderer/media/android/renderer_media_player_manager.h" 13d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "content/renderer/media/android/webmediaplayer_android.h" 1468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)#include "content/renderer/render_thread_impl.h" 15d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)namespace content { 17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 1868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)RendererDemuxerAndroid::RendererDemuxerAndroid() 1968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) : thread_safe_sender_(RenderThreadImpl::current()->thread_safe_sender()), 2068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) media_message_loop_( 2168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) RenderThreadImpl::current()->GetMediaThreadMessageLoopProxy()) {} 22d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 23d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)RendererDemuxerAndroid::~RendererDemuxerAndroid() {} 24d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 2568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)int RendererDemuxerAndroid::GetNextDemuxerClientID() { 2668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) // Don't use zero for IDs since it can be interpreted as having no ID. 2768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) return next_demuxer_client_id_.GetNext() + 1; 2868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)} 2968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) 30d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)void RendererDemuxerAndroid::AddDelegate(int demuxer_client_id, 31d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) MediaSourceDelegate* delegate) { 3268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) DCHECK(media_message_loop_->BelongsToCurrentThread()); 33d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) delegates_.AddWithID(delegate, demuxer_client_id); 34d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 35d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 36d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)void RendererDemuxerAndroid::RemoveDelegate(int demuxer_client_id) { 3768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) DCHECK(media_message_loop_->BelongsToCurrentThread()); 38d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) delegates_.Remove(demuxer_client_id); 39d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 40d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 4168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)bool RendererDemuxerAndroid::OnMessageReceived(const IPC::Message& message) { 4268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) switch (message.type()) { 4368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) case MediaPlayerMsg_DemuxerSeekRequest::ID: 4468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) case MediaPlayerMsg_ReadFromDemuxer::ID: 4568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) case MediaPlayerMsg_MediaConfigRequest::ID: 4668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) media_message_loop_->PostTask(FROM_HERE, base::Bind( 4768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) &RendererDemuxerAndroid::DispatchMessage, this, message)); 4868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) return true; 4968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) } 5068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) return false; 51d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 52d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 53d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)void RendererDemuxerAndroid::DemuxerReady( 54d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int demuxer_client_id, 55d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) const media::DemuxerConfigs& configs) { 5668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) thread_safe_sender_->Send(new MediaPlayerHostMsg_DemuxerReady( 5768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) demuxer_client_id, configs)); 58d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 59d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 60d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)void RendererDemuxerAndroid::ReadFromDemuxerAck( 61d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int demuxer_client_id, 62d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) const media::DemuxerData& data) { 6368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) thread_safe_sender_->Send(new MediaPlayerHostMsg_ReadFromDemuxerAck( 6468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) demuxer_client_id, data)); 65d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 66d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void RendererDemuxerAndroid::DemuxerSeekDone( 681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) int demuxer_client_id, 691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const base::TimeDelta& actual_browser_seek_time) { 7068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) thread_safe_sender_->Send(new MediaPlayerHostMsg_DemuxerSeekDone( 711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) demuxer_client_id, actual_browser_seek_time)); 72d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 73d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 74d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)void RendererDemuxerAndroid::DurationChanged(int demuxer_client_id, 75d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) const base::TimeDelta& duration) { 7668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) thread_safe_sender_->Send(new MediaPlayerHostMsg_DurationChanged( 7768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) demuxer_client_id, duration)); 7868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)} 7968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) 8068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)void RendererDemuxerAndroid::DispatchMessage(const IPC::Message& message) { 8168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) IPC_BEGIN_MESSAGE_MAP(RendererDemuxerAndroid, message) 8268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) IPC_MESSAGE_HANDLER(MediaPlayerMsg_DemuxerSeekRequest, OnDemuxerSeekRequest) 8368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) IPC_MESSAGE_HANDLER(MediaPlayerMsg_ReadFromDemuxer, OnReadFromDemuxer) 8468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaConfigRequest, OnMediaConfigRequest) 8568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) IPC_END_MESSAGE_MAP() 86d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 87d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 88d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)void RendererDemuxerAndroid::OnReadFromDemuxer( 89d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int demuxer_client_id, 90d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) media::DemuxerStream::Type type) { 91d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) MediaSourceDelegate* delegate = delegates_.Lookup(demuxer_client_id); 92d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (delegate) 93d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) delegate->OnReadFromDemuxer(type); 94d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 95d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 9668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)void RendererDemuxerAndroid::OnDemuxerSeekRequest( 97d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) int demuxer_client_id, 981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const base::TimeDelta& time_to_seek, 991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) bool is_browser_seek) { 100d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) MediaSourceDelegate* delegate = delegates_.Lookup(demuxer_client_id); 101d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (delegate) 1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) delegate->Seek(time_to_seek, is_browser_seek); 103d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 104d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 105d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)void RendererDemuxerAndroid::OnMediaConfigRequest(int demuxer_client_id) { 106d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) MediaSourceDelegate* delegate = delegates_.Lookup(demuxer_client_id); 107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) if (delegate) 108d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) delegate->OnMediaConfigRequest(); 109d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} 110d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 111d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} // namespace content 112