Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / media / midi / midi_manager.cc
blobd61bd7a14a3d7e9bb315e3b5daab53140a77a627
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/metrics/histogram_macros.h"
10 #include "base/trace_event/trace_event.h"
12 namespace media {
13 namespace midi {
15 MidiManager::MidiManager()
16 : initialized_(false), result_(Result::NOT_INITIALIZED) {
19 MidiManager::~MidiManager() {
20 UMA_HISTOGRAM_ENUMERATION("Media.Midi.ResultOnShutdown",
21 static_cast<int>(result_),
22 static_cast<int>(Result::MAX) + 1);
25 #if !defined(OS_MACOSX) && !defined(OS_WIN) && \
26 !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID)
27 MidiManager* MidiManager::Create() {
28 return new MidiManager;
30 #endif
32 void MidiManager::StartSession(MidiManagerClient* client) {
33 bool session_is_ready;
34 bool session_needs_initialization = false;
35 bool too_many_pending_clients_exist = false;
38 base::AutoLock auto_lock(lock_);
39 session_is_ready = initialized_;
40 if (clients_.find(client) != clients_.end() ||
41 pending_clients_.find(client) != pending_clients_.end()) {
42 // Should not happen. But just in case the renderer is compromised.
43 NOTREACHED();
44 return;
46 if (!session_is_ready) {
47 // Do not accept a new request if the pending client list contains too
48 // many clients.
49 too_many_pending_clients_exist =
50 pending_clients_.size() >= kMaxPendingClientCount;
52 if (!too_many_pending_clients_exist) {
53 // Call StartInitialization() only for the first request.
54 session_needs_initialization = pending_clients_.empty();
55 pending_clients_.insert(client);
60 // Lazily initialize the MIDI back-end.
61 if (!session_is_ready) {
62 if (session_needs_initialization) {
63 TRACE_EVENT0("midi", "MidiManager::StartInitialization");
64 session_thread_runner_ =
65 base::MessageLoop::current()->task_runner();
66 StartInitialization();
68 if (too_many_pending_clients_exist) {
69 // Return an error immediately if there are too many requests.
70 client->CompleteStartSession(Result::INITIALIZATION_ERROR);
71 return;
73 // CompleteInitialization() will be called asynchronously when platform
74 // dependent initialization is finished.
75 return;
78 // Platform dependent initialization was already finished for previously
79 // initialized clients.
80 Result result;
82 base::AutoLock auto_lock(lock_);
83 if (result_ == Result::OK) {
84 AddInitialPorts(client);
85 clients_.insert(client);
87 result = result_;
89 client->CompleteStartSession(result);
92 void MidiManager::EndSession(MidiManagerClient* client) {
93 // At this point, |client| can be in the destruction process, and calling
94 // any method of |client| is dangerous.
95 base::AutoLock auto_lock(lock_);
96 clients_.erase(client);
97 pending_clients_.erase(client);
100 void MidiManager::AccumulateMidiBytesSent(MidiManagerClient* client, size_t n) {
102 base::AutoLock auto_lock(lock_);
103 if (clients_.find(client) == clients_.end())
104 return;
106 client->AccumulateMidiBytesSent(n);
109 void MidiManager::DispatchSendMidiData(MidiManagerClient* client,
110 uint32 port_index,
111 const std::vector<uint8>& data,
112 double timestamp) {
113 NOTREACHED();
116 void MidiManager::StartInitialization() {
117 CompleteInitialization(Result::NOT_SUPPORTED);
120 void MidiManager::CompleteInitialization(Result result) {
121 DCHECK(session_thread_runner_.get());
122 // It is safe to post a task to the IO thread from here because the IO thread
123 // should have stopped if the MidiManager is going to be destructed.
124 session_thread_runner_->PostTask(
125 FROM_HERE,
126 base::Bind(&MidiManager::CompleteInitializationInternal,
127 base::Unretained(this),
128 result));
131 void MidiManager::AddInputPort(const MidiPortInfo& info) {
132 base::AutoLock auto_lock(lock_);
133 input_ports_.push_back(info);
134 for (auto client : clients_)
135 client->AddInputPort(info);
138 void MidiManager::AddOutputPort(const MidiPortInfo& info) {
139 base::AutoLock auto_lock(lock_);
140 output_ports_.push_back(info);
141 for (auto client : clients_)
142 client->AddOutputPort(info);
145 void MidiManager::SetInputPortState(uint32 port_index, MidiPortState state) {
146 base::AutoLock auto_lock(lock_);
147 DCHECK_LT(port_index, input_ports_.size());
148 input_ports_[port_index].state = state;
149 for (auto client : clients_)
150 client->SetInputPortState(port_index, state);
153 void MidiManager::SetOutputPortState(uint32 port_index, MidiPortState state) {
154 base::AutoLock auto_lock(lock_);
155 DCHECK_LT(port_index, output_ports_.size());
156 output_ports_[port_index].state = state;
157 for (auto client : clients_)
158 client->SetOutputPortState(port_index, state);
161 void MidiManager::ReceiveMidiData(
162 uint32 port_index,
163 const uint8* data,
164 size_t length,
165 double timestamp) {
166 base::AutoLock auto_lock(lock_);
168 for (auto client : clients_)
169 client->ReceiveMidiData(port_index, data, length, timestamp);
172 void MidiManager::CompleteInitializationInternal(Result result) {
173 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization");
175 base::AutoLock auto_lock(lock_);
176 DCHECK(clients_.empty());
177 DCHECK(!initialized_);
178 initialized_ = true;
179 result_ = result;
181 for (auto client : pending_clients_) {
182 if (result_ == Result::OK) {
183 AddInitialPorts(client);
184 clients_.insert(client);
186 client->CompleteStartSession(result_);
188 pending_clients_.clear();
191 void MidiManager::AddInitialPorts(MidiManagerClient* client) {
192 lock_.AssertAcquired();
194 for (const auto& info : input_ports_)
195 client->AddInputPort(info);
196 for (const auto& info : output_ports_)
197 client->AddOutputPort(info);
200 } // namespace midi
201 } // namespace media