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/debug/trace_event.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/message_loop/message_loop_proxy.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 clients_
.insert(client
);
84 client
->CompleteStartSession(result
);
87 void MidiManager::EndSession(MidiManagerClient
* client
) {
88 // At this point, |client| can be in the destruction process, and calling
89 // any method of |client| is dangerous.
90 base::AutoLock
auto_lock(lock_
);
91 clients_
.erase(client
);
92 pending_clients_
.erase(client
);
95 void MidiManager::DispatchSendMidiData(MidiManagerClient
* client
,
97 const std::vector
<uint8
>& data
,
102 void MidiManager::StartInitialization() {
103 CompleteInitialization(MIDI_NOT_SUPPORTED
);
106 void MidiManager::CompleteInitialization(MidiResult result
) {
107 DCHECK(session_thread_runner_
.get());
108 // It is safe to post a task to the IO thread from here because the IO thread
109 // should have stopped if the MidiManager is going to be destructed.
110 session_thread_runner_
->PostTask(
112 base::Bind(&MidiManager::CompleteInitializationInternal
,
113 base::Unretained(this),
117 void MidiManager::AddInputPort(const MidiPortInfo
& info
) {
118 input_ports_
.push_back(info
);
121 void MidiManager::AddOutputPort(const MidiPortInfo
& info
) {
122 output_ports_
.push_back(info
);
125 void MidiManager::ReceiveMidiData(
130 base::AutoLock
auto_lock(lock_
);
132 for (MidiManagerClient
* client
: clients_
)
133 client
->ReceiveMidiData(port_index
, data
, length
, timestamp
);
136 void MidiManager::CompleteInitializationInternal(MidiResult result
) {
137 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization");
139 base::AutoLock
auto_lock(lock_
);
140 DCHECK(clients_
.empty());
141 DCHECK(!initialized_
);
145 for (MidiManagerClient
* client
: pending_clients_
) {
146 if (result_
== MIDI_OK
)
147 clients_
.insert(client
);
148 client
->CompleteStartSession(result_
);
150 pending_clients_
.clear();