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"
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"
14 MidiManager::MidiManager()
15 : initialized_(false),
16 result_(MIDI_NOT_SUPPORTED
) {
19 MidiManager::~MidiManager() {
22 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \
23 !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
24 MidiManager
* MidiManager::Create() {
25 return new MidiManager
;
29 void MidiManager::StartSession(MidiManagerClient
* client
) {
30 bool session_is_ready
;
31 bool session_needs_initialization
= false;
32 bool too_many_pending_clients_exist
= false;
35 base::AutoLock
auto_lock(lock_
);
36 session_is_ready
= initialized_
;
37 if (clients_
.find(client
) != clients_
.end() ||
38 pending_clients_
.find(client
) != pending_clients_
.end()) {
39 // Should not happen. But just in case the renderer is compromised.
43 if (!session_is_ready
) {
44 // Do not accept a new request if the pending client list contains too
46 too_many_pending_clients_exist
=
47 pending_clients_
.size() >= kMaxPendingClientCount
;
49 if (!too_many_pending_clients_exist
) {
50 // Call StartInitialization() only for the first request.
51 session_needs_initialization
= pending_clients_
.empty();
52 pending_clients_
.insert(client
);
57 // Lazily initialize the MIDI back-end.
58 if (!session_is_ready
) {
59 if (session_needs_initialization
) {
60 TRACE_EVENT0("midi", "MidiManager::StartInitialization");
61 session_thread_runner_
=
62 base::MessageLoop::current()->message_loop_proxy();
63 StartInitialization();
65 if (too_many_pending_clients_exist
) {
66 // Return an error immediately if there are too many requests.
67 client
->CompleteStartSession(MIDI_INITIALIZATION_ERROR
);
70 // CompleteInitialization() will be called asynchronously when platform
71 // dependent initialization is finished.
75 // Platform dependent initialization was already finished for previously
76 // initialized clients.
79 base::AutoLock
auto_lock(lock_
);
80 if (result_
== MIDI_OK
) {
81 AddInitialPorts(client
);
82 clients_
.insert(client
);
86 client
->CompleteStartSession(result
);
89 void MidiManager::EndSession(MidiManagerClient
* client
) {
90 // At this point, |client| can be in the destruction process, and calling
91 // any method of |client| is dangerous.
92 base::AutoLock
auto_lock(lock_
);
93 clients_
.erase(client
);
94 pending_clients_
.erase(client
);
97 void MidiManager::AccumulateMidiBytesSent(MidiManagerClient
* client
, size_t n
) {
99 base::AutoLock
auto_lock(lock_
);
100 if (clients_
.find(client
) == clients_
.end())
103 client
->AccumulateMidiBytesSent(n
);
106 void MidiManager::DispatchSendMidiData(MidiManagerClient
* client
,
108 const std::vector
<uint8
>& data
,
113 void MidiManager::StartInitialization() {
114 CompleteInitialization(MIDI_NOT_SUPPORTED
);
117 void MidiManager::CompleteInitialization(MidiResult result
) {
118 DCHECK(session_thread_runner_
.get());
119 // It is safe to post a task to the IO thread from here because the IO thread
120 // should have stopped if the MidiManager is going to be destructed.
121 session_thread_runner_
->PostTask(
123 base::Bind(&MidiManager::CompleteInitializationInternal
,
124 base::Unretained(this),
128 void MidiManager::AddInputPort(const MidiPortInfo
& info
) {
129 base::AutoLock
auto_lock(lock_
);
130 input_ports_
.push_back(info
);
131 for (auto client
: clients_
)
132 client
->AddInputPort(info
);
135 void MidiManager::AddOutputPort(const MidiPortInfo
& info
) {
136 base::AutoLock
auto_lock(lock_
);
137 output_ports_
.push_back(info
);
138 for (auto client
: clients_
)
139 client
->AddOutputPort(info
);
142 void MidiManager::SetInputPortState(uint32 port_index
, MidiPortState state
) {
143 base::AutoLock
auto_lock(lock_
);
144 DCHECK_LT(port_index
, input_ports_
.size());
145 input_ports_
[port_index
].state
= state
;
146 for (auto client
: clients_
)
147 client
->SetInputPortState(port_index
, state
);
150 void MidiManager::SetOutputPortState(uint32 port_index
, MidiPortState state
) {
151 base::AutoLock
auto_lock(lock_
);
152 DCHECK_LT(port_index
, output_ports_
.size());
153 output_ports_
[port_index
].state
= state
;
154 for (auto client
: clients_
)
155 client
->SetOutputPortState(port_index
, state
);
158 void MidiManager::ReceiveMidiData(
163 base::AutoLock
auto_lock(lock_
);
165 for (auto client
: clients_
)
166 client
->ReceiveMidiData(port_index
, data
, length
, timestamp
);
169 void MidiManager::CompleteInitializationInternal(MidiResult result
) {
170 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization");
172 base::AutoLock
auto_lock(lock_
);
173 DCHECK(clients_
.empty());
174 DCHECK(!initialized_
);
178 for (auto client
: pending_clients_
) {
179 if (result_
== MIDI_OK
) {
180 AddInitialPorts(client
);
181 clients_
.insert(client
);
183 client
->CompleteStartSession(result_
);
185 pending_clients_
.clear();
188 void MidiManager::AddInitialPorts(MidiManagerClient
* client
) {
189 lock_
.AssertAcquired();
191 for (const auto& info
: input_ports_
)
192 client
->AddInputPort(info
);
193 for (const auto& info
: output_ports_
)
194 client
->AddOutputPort(info
);