[MediaRouter] Update MR-2-Extension's PostMessage to return boolean.
[chromium-blink-merge.git] / media / midi / midi_manager.cc
blob31f819aa0e6d6bda6c51b74a1e36a310c38ff19d
1 // Copyright (c) 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 "media/midi/midi_manager.h"
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/trace_event/trace_event.h"
12 namespace media {
13 namespace midi {
15 MidiManager::MidiManager()
16 : initialized_(false),
17 result_(MIDI_NOT_SUPPORTED) {
20 MidiManager::~MidiManager() {
23 #if !defined(OS_MACOSX) && !defined(OS_WIN) && \
24 !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID)
25 MidiManager* MidiManager::Create() {
26 return new MidiManager;
28 #endif
30 void MidiManager::StartSession(MidiManagerClient* client) {
31 bool session_is_ready;
32 bool session_needs_initialization = false;
33 bool too_many_pending_clients_exist = false;
36 base::AutoLock auto_lock(lock_);
37 session_is_ready = initialized_;
38 if (clients_.find(client) != clients_.end() ||
39 pending_clients_.find(client) != pending_clients_.end()) {
40 // Should not happen. But just in case the renderer is compromised.
41 NOTREACHED();
42 return;
44 if (!session_is_ready) {
45 // Do not accept a new request if the pending client list contains too
46 // many clients.
47 too_many_pending_clients_exist =
48 pending_clients_.size() >= kMaxPendingClientCount;
50 if (!too_many_pending_clients_exist) {
51 // Call StartInitialization() only for the first request.
52 session_needs_initialization = pending_clients_.empty();
53 pending_clients_.insert(client);
58 // Lazily initialize the MIDI back-end.
59 if (!session_is_ready) {
60 if (session_needs_initialization) {
61 TRACE_EVENT0("midi", "MidiManager::StartInitialization");
62 session_thread_runner_ =
63 base::MessageLoop::current()->message_loop_proxy();
64 StartInitialization();
66 if (too_many_pending_clients_exist) {
67 // Return an error immediately if there are too many requests.
68 client->CompleteStartSession(MIDI_INITIALIZATION_ERROR);
69 return;
71 // CompleteInitialization() will be called asynchronously when platform
72 // dependent initialization is finished.
73 return;
76 // Platform dependent initialization was already finished for previously
77 // initialized clients.
78 MidiResult result;
80 base::AutoLock auto_lock(lock_);
81 if (result_ == MIDI_OK) {
82 AddInitialPorts(client);
83 clients_.insert(client);
85 result = result_;
87 client->CompleteStartSession(result);
90 void MidiManager::EndSession(MidiManagerClient* client) {
91 // At this point, |client| can be in the destruction process, and calling
92 // any method of |client| is dangerous.
93 base::AutoLock auto_lock(lock_);
94 clients_.erase(client);
95 pending_clients_.erase(client);
98 void MidiManager::AccumulateMidiBytesSent(MidiManagerClient* client, size_t n) {
100 base::AutoLock auto_lock(lock_);
101 if (clients_.find(client) == clients_.end())
102 return;
104 client->AccumulateMidiBytesSent(n);
107 void MidiManager::DispatchSendMidiData(MidiManagerClient* client,
108 uint32 port_index,
109 const std::vector<uint8>& data,
110 double timestamp) {
111 NOTREACHED();
114 void MidiManager::StartInitialization() {
115 CompleteInitialization(MIDI_NOT_SUPPORTED);
118 void MidiManager::CompleteInitialization(MidiResult result) {
119 DCHECK(session_thread_runner_.get());
120 // It is safe to post a task to the IO thread from here because the IO thread
121 // should have stopped if the MidiManager is going to be destructed.
122 session_thread_runner_->PostTask(
123 FROM_HERE,
124 base::Bind(&MidiManager::CompleteInitializationInternal,
125 base::Unretained(this),
126 result));
129 void MidiManager::AddInputPort(const MidiPortInfo& info) {
130 base::AutoLock auto_lock(lock_);
131 input_ports_.push_back(info);
132 for (auto client : clients_)
133 client->AddInputPort(info);
136 void MidiManager::AddOutputPort(const MidiPortInfo& info) {
137 base::AutoLock auto_lock(lock_);
138 output_ports_.push_back(info);
139 for (auto client : clients_)
140 client->AddOutputPort(info);
143 void MidiManager::SetInputPortState(uint32 port_index, MidiPortState state) {
144 base::AutoLock auto_lock(lock_);
145 DCHECK_LT(port_index, input_ports_.size());
146 input_ports_[port_index].state = state;
147 for (auto client : clients_)
148 client->SetInputPortState(port_index, state);
151 void MidiManager::SetOutputPortState(uint32 port_index, MidiPortState state) {
152 base::AutoLock auto_lock(lock_);
153 DCHECK_LT(port_index, output_ports_.size());
154 output_ports_[port_index].state = state;
155 for (auto client : clients_)
156 client->SetOutputPortState(port_index, state);
159 void MidiManager::ReceiveMidiData(
160 uint32 port_index,
161 const uint8* data,
162 size_t length,
163 double timestamp) {
164 base::AutoLock auto_lock(lock_);
166 for (auto client : clients_)
167 client->ReceiveMidiData(port_index, data, length, timestamp);
170 void MidiManager::CompleteInitializationInternal(MidiResult result) {
171 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization");
173 base::AutoLock auto_lock(lock_);
174 DCHECK(clients_.empty());
175 DCHECK(!initialized_);
176 initialized_ = true;
177 result_ = result;
179 for (auto client : pending_clients_) {
180 if (result_ == MIDI_OK) {
181 AddInitialPorts(client);
182 clients_.insert(client);
184 client->CompleteStartSession(result_);
186 pending_clients_.clear();
189 void MidiManager::AddInitialPorts(MidiManagerClient* client) {
190 lock_.AssertAcquired();
192 for (const auto& info : input_ports_)
193 client->AddInputPort(info);
194 for (const auto& info : output_ports_)
195 client->AddOutputPort(info);
198 } // namespace midi
199 } // namespace media