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.
5 #include "content/browser/media/android/browser_demuxer_android.h"
7 #include "content/common/media/media_player_messages_android.h"
11 class BrowserDemuxerAndroid::Internal
: public media::DemuxerAndroid
{
13 Internal(const scoped_refptr
<BrowserDemuxerAndroid
>& demuxer
,
14 int demuxer_client_id
)
16 demuxer_client_id_(demuxer_client_id
) {}
19 DCHECK(ClientIDExists()) << demuxer_client_id_
;
20 demuxer_
->RemoveDemuxerClient(demuxer_client_id_
);
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
);
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
));
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
));
44 // Helper for DCHECKing that the ID is still registered.
45 bool ClientIDExists() {
46 return demuxer_
->demuxer_clients_
.Lookup(demuxer_client_id_
);
49 scoped_refptr
<BrowserDemuxerAndroid
> demuxer_
;
50 int demuxer_client_id_
;
52 DISALLOW_COPY_AND_ASSIGN(Internal
);
55 BrowserDemuxerAndroid::BrowserDemuxerAndroid()
56 : BrowserMessageFilter(MediaPlayerMsgStart
) {}
58 BrowserDemuxerAndroid::~BrowserDemuxerAndroid() {}
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
;
73 bool BrowserDemuxerAndroid::OnMessageReceived(const IPC::Message
& message
) {
75 IPC_BEGIN_MESSAGE_MAP(BrowserDemuxerAndroid
, message
)
76 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DemuxerReady
, OnDemuxerReady
)
77 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_ReadFromDemuxerAck
,
79 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DurationChanged
,
81 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DemuxerSeekDone
,
83 IPC_MESSAGE_UNHANDLED(handled
= false)
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
));
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
);
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
);
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
);
114 client
->OnDemuxerConfigsAvailable(configs
);
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
);
123 client
->OnDemuxerDataAvailable(data
);
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
);
132 client
->OnDemuxerSeekDone(actual_browser_seek_time
);
135 void BrowserDemuxerAndroid::OnDurationChanged(int demuxer_client_id
,
136 const base::TimeDelta
& duration
) {
137 media::DemuxerAndroidClient
* client
=
138 demuxer_clients_
.Lookup(demuxer_client_id
);
140 client
->OnDemuxerDurationChanged(duration
);
143 } // namespace content