Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / media / android / browser_demuxer_android.cc
blobddaca7efa3cedbe025f748914a01b633141d1621
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"
12 namespace content {
14 class BrowserDemuxerAndroid::Internal : public media::DemuxerAndroid {
15 public:
16 Internal(const scoped_refptr<BrowserDemuxerAndroid>& demuxer,
17 int demuxer_client_id)
18 : demuxer_(demuxer),
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));
45 private:
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);
63 task_runner_ =
64 enable_media_thread ?
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:
80 return task_runner_;
83 return nullptr;
86 bool BrowserDemuxerAndroid::OnMessageReceived(const IPC::Message& message) {
87 bool handled = true;
88 IPC_BEGIN_MESSAGE_MAP(BrowserDemuxerAndroid, message)
89 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DemuxerReady, OnDemuxerReady)
90 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_ReadFromDemuxerAck,
91 OnReadFromDemuxerAck)
92 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DurationChanged,
93 OnDurationChanged)
94 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_DemuxerSeekDone,
95 OnDemuxerSeekDone)
96 IPC_MESSAGE_UNHANDLED(handled = false)
97 IPC_END_MESSAGE_MAP()
98 return handled;
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);
138 if (client)
139 client->OnDemuxerConfigsAvailable(configs);
140 else
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);
152 if (client)
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);
164 if (client)
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);
175 if (client)
176 client->OnDemuxerDurationChanged(duration);
177 else
178 pending_configs_[demuxer_client_id].duration = duration;
181 } // namespace content