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
) {}
18 ~Internal() override
{
19 DCHECK(ClientIDExists()) << demuxer_client_id_
;
20 demuxer_
->RemoveDemuxerClient(demuxer_client_id_
);
23 // media::DemuxerAndroid implementation.
24 void Initialize(media::DemuxerAndroidClient
* client
) override
{
25 DCHECK(!ClientIDExists()) << demuxer_client_id_
;
26 demuxer_
->AddDemuxerClient(demuxer_client_id_
, client
);
29 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 void RequestDemuxerSeek(const base::TimeDelta
& time_to_seek
,
36 bool is_browser_seek
) override
{
37 DCHECK(ClientIDExists()) << demuxer_client_id_
;
38 demuxer_
->Send(new MediaPlayerMsg_DemuxerSeekRequest(
39 demuxer_client_id_
, time_to_seek
, is_browser_seek
));
43 // Helper for DCHECKing that the ID is still registered.
44 bool ClientIDExists() {
45 return demuxer_
->demuxer_clients_
.Lookup(demuxer_client_id_
);
48 scoped_refptr
<BrowserDemuxerAndroid
> demuxer_
;
49 int demuxer_client_id_
;
51 DISALLOW_COPY_AND_ASSIGN(Internal
);
54 BrowserDemuxerAndroid::BrowserDemuxerAndroid()
55 : BrowserMessageFilter(MediaPlayerMsgStart
) {}
57 BrowserDemuxerAndroid::~BrowserDemuxerAndroid() {}
59 void BrowserDemuxerAndroid::OverrideThreadForMessage(
60 const IPC::Message
& message
,
61 BrowserThread::ID
* thread
) {
62 switch (message
.type()) {
63 case MediaPlayerHostMsg_DemuxerReady::ID
:
64 case MediaPlayerHostMsg_ReadFromDemuxerAck::ID
:
65 case MediaPlayerHostMsg_DurationChanged::ID
:
66 case MediaPlayerHostMsg_DemuxerSeekDone::ID
:
67 *thread
= BrowserThread::UI
;
72 bool BrowserDemuxerAndroid::OnMessageReceived(const IPC::Message
& message
) {
74 IPC_BEGIN_MESSAGE_MAP(BrowserDemuxerAndroid
, message
)
75 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DemuxerReady
, OnDemuxerReady
)
76 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_ReadFromDemuxerAck
,
78 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DurationChanged
,
80 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DemuxerSeekDone
,
82 IPC_MESSAGE_UNHANDLED(handled
= false)
87 scoped_ptr
<media::DemuxerAndroid
> BrowserDemuxerAndroid::CreateDemuxer(
88 int demuxer_client_id
) {
89 return scoped_ptr
<media::DemuxerAndroid
>(
90 new Internal(this, demuxer_client_id
));
93 void BrowserDemuxerAndroid::AddDemuxerClient(
94 int demuxer_client_id
,
95 media::DemuxerAndroidClient
* client
) {
96 DVLOG(1) << __FUNCTION__
<< " peer_pid=" << peer_pid()
97 << " demuxer_client_id=" << demuxer_client_id
;
98 demuxer_clients_
.AddWithID(client
, demuxer_client_id
);
101 void BrowserDemuxerAndroid::RemoveDemuxerClient(int demuxer_client_id
) {
102 DVLOG(1) << __FUNCTION__
<< " peer_pid=" << peer_pid()
103 << " demuxer_client_id=" << demuxer_client_id
;
104 demuxer_clients_
.Remove(demuxer_client_id
);
107 void BrowserDemuxerAndroid::OnDemuxerReady(
108 int demuxer_client_id
,
109 const media::DemuxerConfigs
& configs
) {
110 media::DemuxerAndroidClient
* client
=
111 demuxer_clients_
.Lookup(demuxer_client_id
);
113 client
->OnDemuxerConfigsAvailable(configs
);
116 void BrowserDemuxerAndroid::OnReadFromDemuxerAck(
117 int demuxer_client_id
,
118 const media::DemuxerData
& data
) {
119 media::DemuxerAndroidClient
* client
=
120 demuxer_clients_
.Lookup(demuxer_client_id
);
122 client
->OnDemuxerDataAvailable(data
);
125 void BrowserDemuxerAndroid::OnDemuxerSeekDone(
126 int demuxer_client_id
,
127 const base::TimeDelta
& actual_browser_seek_time
) {
128 media::DemuxerAndroidClient
* client
=
129 demuxer_clients_
.Lookup(demuxer_client_id
);
131 client
->OnDemuxerSeekDone(actual_browser_seek_time
);
134 void BrowserDemuxerAndroid::OnDurationChanged(int demuxer_client_id
,
135 const base::TimeDelta
& duration
) {
136 media::DemuxerAndroidClient
* client
=
137 demuxer_clients_
.Lookup(demuxer_client_id
);
139 client
->OnDemuxerDurationChanged(duration
);
142 } // namespace content