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 "base/command_line.h"
8 #include "content/common/media/media_player_messages_android.h"
9 #include "media/base/android/media_codec_player.h"
10 #include "media/base/media_switches.h"
14 class BrowserDemuxerAndroid::Internal
: public media::DemuxerAndroid
{
16 Internal(const scoped_refptr
<BrowserDemuxerAndroid
>& demuxer
,
17 int demuxer_client_id
)
19 demuxer_client_id_(demuxer_client_id
) {}
21 ~Internal() override
{
22 DCHECK(ClientIDExists()) << demuxer_client_id_
;
23 demuxer_
->RemoveDemuxerClient(demuxer_client_id_
);
26 // media::DemuxerAndroid implementation.
27 void Initialize(media::DemuxerAndroidClient
* client
) override
{
28 DCHECK(!ClientIDExists()) << demuxer_client_id_
;
29 demuxer_
->AddDemuxerClient(demuxer_client_id_
, client
);
32 void RequestDemuxerData(media::DemuxerStream::Type type
) override
{
33 DCHECK(ClientIDExists()) << demuxer_client_id_
;
34 demuxer_
->Send(new MediaPlayerMsg_ReadFromDemuxer(
35 demuxer_client_id_
, type
));
38 void RequestDemuxerSeek(const base::TimeDelta
& time_to_seek
,
39 bool is_browser_seek
) override
{
40 DCHECK(ClientIDExists()) << demuxer_client_id_
;
41 demuxer_
->Send(new MediaPlayerMsg_DemuxerSeekRequest(
42 demuxer_client_id_
, time_to_seek
, is_browser_seek
));
46 // Helper for DCHECKing that the ID is still registered.
47 bool ClientIDExists() {
48 return demuxer_
->demuxer_clients_
.Lookup(demuxer_client_id_
);
51 scoped_refptr
<BrowserDemuxerAndroid
> demuxer_
;
52 int demuxer_client_id_
;
54 DISALLOW_COPY_AND_ASSIGN(Internal
);
57 BrowserDemuxerAndroid::BrowserDemuxerAndroid()
58 : BrowserMessageFilter(MediaPlayerMsgStart
) {
59 bool enable_media_thread
=
60 base::CommandLine::ForCurrentProcess()->
61 HasSwitch(switches::kEnableMediaThreadForMediaPlayback
);
65 media::GetMediaTaskRunner().get() :
66 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
).get();
70 BrowserDemuxerAndroid::~BrowserDemuxerAndroid() {}
72 base::TaskRunner
* BrowserDemuxerAndroid::OverrideTaskRunnerForMessage(
73 const IPC::Message
& message
) {
75 switch (message
.type()) {
76 case MediaPlayerHostMsg_DemuxerReady::ID
:
77 case MediaPlayerHostMsg_ReadFromDemuxerAck::ID
:
78 case MediaPlayerHostMsg_DurationChanged::ID
:
79 case MediaPlayerHostMsg_DemuxerSeekDone::ID
:
86 bool BrowserDemuxerAndroid::OnMessageReceived(const IPC::Message
& message
) {
88 IPC_BEGIN_MESSAGE_MAP(BrowserDemuxerAndroid
, message
)
89 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DemuxerReady
, OnDemuxerReady
)
90 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_ReadFromDemuxerAck
,
92 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DurationChanged
,
94 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DemuxerSeekDone
,
96 IPC_MESSAGE_UNHANDLED(handled
= false)
101 scoped_ptr
<media::DemuxerAndroid
> BrowserDemuxerAndroid::CreateDemuxer(
102 int demuxer_client_id
) {
103 return scoped_ptr
<media::DemuxerAndroid
>(
104 new Internal(this, demuxer_client_id
));
107 void BrowserDemuxerAndroid::AddDemuxerClient(
108 int demuxer_client_id
,
109 media::DemuxerAndroidClient
* client
) {
110 DVLOG(1) << __FUNCTION__
<< " peer_pid=" << peer_pid()
111 << " demuxer_client_id=" << demuxer_client_id
;
112 DCHECK(task_runner_
->BelongsToCurrentThread());
114 demuxer_clients_
.AddWithID(client
, demuxer_client_id
);
116 if (pending_configs_
.count(demuxer_client_id
)) {
117 client
->OnDemuxerConfigsAvailable(pending_configs_
[demuxer_client_id
]);
118 pending_configs_
.erase(demuxer_client_id
);
122 void BrowserDemuxerAndroid::RemoveDemuxerClient(int demuxer_client_id
) {
123 DVLOG(1) << __FUNCTION__
<< " peer_pid=" << peer_pid()
124 << " demuxer_client_id=" << demuxer_client_id
;
125 DCHECK(task_runner_
->BelongsToCurrentThread());
127 demuxer_clients_
.Remove(demuxer_client_id
);
130 void BrowserDemuxerAndroid::OnDemuxerReady(
131 int demuxer_client_id
,
132 const media::DemuxerConfigs
& configs
) {
133 DCHECK(task_runner_
->BelongsToCurrentThread());
135 media::DemuxerAndroidClient
* client
=
136 demuxer_clients_
.Lookup(demuxer_client_id
);
139 client
->OnDemuxerConfigsAvailable(configs
);
141 pending_configs_
[demuxer_client_id
] = configs
;
144 void BrowserDemuxerAndroid::OnReadFromDemuxerAck(
145 int demuxer_client_id
,
146 const media::DemuxerData
& data
) {
147 DCHECK(task_runner_
->BelongsToCurrentThread());
149 media::DemuxerAndroidClient
* client
=
150 demuxer_clients_
.Lookup(demuxer_client_id
);
153 client
->OnDemuxerDataAvailable(data
);
156 void BrowserDemuxerAndroid::OnDemuxerSeekDone(
157 int demuxer_client_id
,
158 const base::TimeDelta
& actual_browser_seek_time
) {
159 DCHECK(task_runner_
->BelongsToCurrentThread());
161 media::DemuxerAndroidClient
* client
=
162 demuxer_clients_
.Lookup(demuxer_client_id
);
165 client
->OnDemuxerSeekDone(actual_browser_seek_time
);
168 void BrowserDemuxerAndroid::OnDurationChanged(int demuxer_client_id
,
169 const base::TimeDelta
& duration
) {
170 DCHECK(task_runner_
->BelongsToCurrentThread());
172 media::DemuxerAndroidClient
* client
=
173 demuxer_clients_
.Lookup(demuxer_client_id
);
176 client
->OnDemuxerDurationChanged(duration
);
178 pending_configs_
[demuxer_client_id
].duration
= duration
;
181 } // namespace content