crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "media/midi/midi_manager.h" |
| 6 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 7 | #include "base/bind.h" |
fdoray | a05bf57 | 2016-06-09 08:42:38 -0700 | [diff] [blame] | 8 | #include "base/location.h" |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 9 | #include "base/metrics/histogram_macros.h" |
fdoray | a05bf57 | 2016-06-09 08:42:38 -0700 | [diff] [blame] | 10 | #include "base/single_thread_task_runner.h" |
| 11 | #include "base/threading/thread_task_runner_handle.h" |
ssid | 9134444 | 2015-01-28 04:13:15 -0800 | [diff] [blame] | 12 | #include "base/trace_event/trace_event.h" |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame] | 13 | #include "build/build_config.h" |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 14 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 15 | namespace midi { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 16 | |
toyoshim | 715385c | 2015-08-09 23:00:26 -0700 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | using Sample = base::HistogramBase::Sample; |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame^] | 20 | using midi::mojom::PortState; |
toyoshim | 2f3a48f | 2016-10-17 01:54:13 -0700 | [diff] [blame] | 21 | using midi::mojom::Result; |
toyoshim | 715385c | 2015-08-09 23:00:26 -0700 | [diff] [blame] | 22 | |
| 23 | // If many users have more devices, this number will be increased. |
| 24 | // But the number is expected to be big enough for now. |
| 25 | const Sample kMaxUmaDevices = 31; |
| 26 | |
| 27 | // Used to count events for usage histogram. |
| 28 | enum class Usage { |
| 29 | CREATED, |
| 30 | CREATED_ON_UNSUPPORTED_PLATFORMS, |
| 31 | SESSION_STARTED, |
| 32 | SESSION_ENDED, |
| 33 | INITIALIZED, |
| 34 | INPUT_PORT_ADDED, |
| 35 | OUTPUT_PORT_ADDED, |
| 36 | |
| 37 | // New items should be inserted here, and |MAX| should point the last item. |
| 38 | MAX = INITIALIZED, |
| 39 | }; |
| 40 | |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 41 | // Used in StartSession. |
| 42 | enum class Completion { |
| 43 | COMPLETE_SYNCHRONOUSLY, |
| 44 | INVOKE_INITIALIZATION, |
| 45 | COMPLETE_ASYNCHRONOUSLY, |
| 46 | }; |
| 47 | |
toyoshim | 715385c | 2015-08-09 23:00:26 -0700 | [diff] [blame] | 48 | void ReportUsage(Usage usage) { |
| 49 | UMA_HISTOGRAM_ENUMERATION("Media.Midi.Usage", |
| 50 | static_cast<Sample>(usage), |
| 51 | static_cast<Sample>(Usage::MAX) + 1); |
| 52 | } |
| 53 | |
| 54 | } // namespace |
| 55 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 56 | MidiManager::MidiManager() |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 57 | : initialized_(false), finalized_(false), result_(Result::NOT_INITIALIZED) { |
toyoshim | 715385c | 2015-08-09 23:00:26 -0700 | [diff] [blame] | 58 | ReportUsage(Usage::CREATED); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 59 | } |
| 60 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 61 | MidiManager::~MidiManager() { |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 62 | // Make sure that Finalize() is called to clean up resources allocated on |
| 63 | // the Chrome_IOThread. |
toyoshim | 131beb5 | 2016-07-25 00:42:41 -0700 | [diff] [blame] | 64 | base::AutoLock auto_lock(lock_); |
| 65 | CHECK(finalized_); |
yukawa@chromium.org | db7ad8b | 2013-12-04 15:42:41 +0000 | [diff] [blame] | 66 | } |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 67 | |
agoode | 7de413f | 2015-04-24 00:13:39 -0700 | [diff] [blame] | 68 | #if !defined(OS_MACOSX) && !defined(OS_WIN) && \ |
| 69 | !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID) |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 70 | MidiManager* MidiManager::Create() { |
toyoshim | 715385c | 2015-08-09 23:00:26 -0700 | [diff] [blame] | 71 | ReportUsage(Usage::CREATED_ON_UNSUPPORTED_PLATFORMS); |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 72 | return new MidiManager; |
| 73 | } |
| 74 | #endif |
| 75 | |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 76 | void MidiManager::Shutdown() { |
| 77 | UMA_HISTOGRAM_ENUMERATION("Media.Midi.ResultOnShutdown", |
| 78 | static_cast<int>(result_), |
| 79 | static_cast<int>(Result::MAX) + 1); |
| 80 | base::AutoLock auto_lock(lock_); |
| 81 | if (session_thread_runner_) { |
| 82 | session_thread_runner_->PostTask( |
| 83 | FROM_HERE, base::Bind(&MidiManager::ShutdownOnSessionThread, |
| 84 | base::Unretained(this))); |
| 85 | session_thread_runner_ = nullptr; |
| 86 | } else { |
| 87 | finalized_ = true; |
| 88 | } |
| 89 | } |
| 90 | |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 91 | void MidiManager::StartSession(MidiManagerClient* client) { |
toyoshim | 715385c | 2015-08-09 23:00:26 -0700 | [diff] [blame] | 92 | ReportUsage(Usage::SESSION_STARTED); |
| 93 | |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 94 | Completion completion = Completion::COMPLETE_SYNCHRONOUSLY; |
| 95 | Result result = Result::NOT_INITIALIZED; |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 96 | |
| 97 | { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 98 | base::AutoLock auto_lock(lock_); |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 99 | if (clients_.find(client) != clients_.end() || |
| 100 | pending_clients_.find(client) != pending_clients_.end()) { |
| 101 | // Should not happen. But just in case the renderer is compromised. |
| 102 | NOTREACHED(); |
| 103 | return; |
| 104 | } |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 105 | |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 106 | if (initialized_) { |
| 107 | // Platform dependent initialization was already finished for previously |
| 108 | // initialized clients. |
| 109 | if (result_ == Result::OK) { |
| 110 | AddInitialPorts(client); |
| 111 | clients_.insert(client); |
| 112 | } |
| 113 | // Complete synchronously with |result_|; |
| 114 | result = result_; |
| 115 | } else { |
| 116 | bool too_many_pending_clients_exist = |
| 117 | pending_clients_.size() >= kMaxPendingClientCount; |
| 118 | // Do not accept a new request if the pending client list contains too |
| 119 | // many clients, or Shutdown() was already called. |
| 120 | if (too_many_pending_clients_exist || finalized_) { |
| 121 | result = Result::INITIALIZATION_ERROR; |
| 122 | } else { |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 123 | // Call StartInitialization() only for the first request. |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 124 | if (pending_clients_.empty()) { |
| 125 | completion = Completion::INVOKE_INITIALIZATION; |
fdoray | a05bf57 | 2016-06-09 08:42:38 -0700 | [diff] [blame] | 126 | session_thread_runner_ = base::ThreadTaskRunnerHandle::Get(); |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 127 | } else { |
| 128 | completion = Completion::COMPLETE_ASYNCHRONOUSLY; |
| 129 | } |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 130 | pending_clients_.insert(client); |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 131 | } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 132 | } |
ochang | ae3bae9 | 2016-01-12 19:42:10 -0800 | [diff] [blame] | 133 | |
| 134 | if (completion == Completion::COMPLETE_SYNCHRONOUSLY) { |
| 135 | client->CompleteStartSession(result); |
| 136 | return; |
| 137 | } |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 138 | } |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 139 | |
ochang | ae3bae9 | 2016-01-12 19:42:10 -0800 | [diff] [blame] | 140 | if (completion == Completion::INVOKE_INITIALIZATION) { |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 141 | // Lazily initialize the MIDI back-end. |
| 142 | TRACE_EVENT0("midi", "MidiManager::StartInitialization"); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 143 | // CompleteInitialization() will be called asynchronously when platform |
| 144 | // dependent initialization is finished. |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 145 | StartInitialization(); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 146 | } |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 147 | } |
| 148 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 149 | void MidiManager::EndSession(MidiManagerClient* client) { |
toyoshim | 715385c | 2015-08-09 23:00:26 -0700 | [diff] [blame] | 150 | ReportUsage(Usage::SESSION_ENDED); |
| 151 | |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 152 | // At this point, |client| can be in the destruction process, and calling |
ochang | ae3bae9 | 2016-01-12 19:42:10 -0800 | [diff] [blame] | 153 | // any method of |client| is dangerous. Calls on clients *must* be protected |
| 154 | // by |lock_| to prevent race conditions. |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 155 | base::AutoLock auto_lock(lock_); |
toyoshim@chromium.org | 1fa678a | 2014-06-13 09:40:33 +0000 | [diff] [blame] | 156 | clients_.erase(client); |
| 157 | pending_clients_.erase(client); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 158 | } |
| 159 | |
toyoshim | 7dd1248 | 2015-04-07 07:02:49 -0700 | [diff] [blame] | 160 | void MidiManager::AccumulateMidiBytesSent(MidiManagerClient* client, size_t n) { |
agoode | ad116b2 | 2015-12-07 00:00:35 -0800 | [diff] [blame] | 161 | base::AutoLock auto_lock(lock_); |
| 162 | if (clients_.find(client) == clients_.end()) |
| 163 | return; |
| 164 | |
agoode | b02d096 | 2015-12-07 19:45:54 -0800 | [diff] [blame] | 165 | // Continue to hold lock_ here in case another thread is currently doing |
| 166 | // EndSession. |
toyoshim | 7dd1248 | 2015-04-07 07:02:49 -0700 | [diff] [blame] | 167 | client->AccumulateMidiBytesSent(n); |
| 168 | } |
| 169 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 170 | void MidiManager::DispatchSendMidiData(MidiManagerClient* client, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 171 | uint32_t port_index, |
| 172 | const std::vector<uint8_t>& data, |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 173 | double timestamp) { |
| 174 | NOTREACHED(); |
| 175 | } |
| 176 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 177 | void MidiManager::StartInitialization() { |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 178 | CompleteInitialization(Result::NOT_SUPPORTED); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 179 | } |
| 180 | |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 181 | void MidiManager::CompleteInitialization(Result result) { |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 182 | base::AutoLock auto_lock(lock_); |
| 183 | if (session_thread_runner_) { |
| 184 | session_thread_runner_->PostTask( |
| 185 | FROM_HERE, base::Bind(&MidiManager::CompleteInitializationInternal, |
| 186 | base::Unretained(this), result)); |
| 187 | } |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 188 | } |
| 189 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 190 | void MidiManager::AddInputPort(const MidiPortInfo& info) { |
toyoshim | 715385c | 2015-08-09 23:00:26 -0700 | [diff] [blame] | 191 | ReportUsage(Usage::INPUT_PORT_ADDED); |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 192 | base::AutoLock auto_lock(lock_); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 193 | input_ports_.push_back(info); |
vmpstr | 0205abb | 2016-06-28 18:50:56 -0700 | [diff] [blame] | 194 | for (auto* client : clients_) |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 195 | client->AddInputPort(info); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 196 | } |
| 197 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 198 | void MidiManager::AddOutputPort(const MidiPortInfo& info) { |
toyoshim | 715385c | 2015-08-09 23:00:26 -0700 | [diff] [blame] | 199 | ReportUsage(Usage::OUTPUT_PORT_ADDED); |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 200 | base::AutoLock auto_lock(lock_); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 201 | output_ports_.push_back(info); |
vmpstr | 0205abb | 2016-06-28 18:50:56 -0700 | [diff] [blame] | 202 | for (auto* client : clients_) |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 203 | client->AddOutputPort(info); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 204 | } |
| 205 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame^] | 206 | void MidiManager::SetInputPortState(uint32_t port_index, PortState state) { |
toyoshim | 5c6fe4b | 2015-02-18 23:28:09 -0800 | [diff] [blame] | 207 | base::AutoLock auto_lock(lock_); |
| 208 | DCHECK_LT(port_index, input_ports_.size()); |
| 209 | input_ports_[port_index].state = state; |
vmpstr | 0205abb | 2016-06-28 18:50:56 -0700 | [diff] [blame] | 210 | for (auto* client : clients_) |
toyoshim | 5c6fe4b | 2015-02-18 23:28:09 -0800 | [diff] [blame] | 211 | client->SetInputPortState(port_index, state); |
| 212 | } |
| 213 | |
toyoshim | ec2570a | 2016-10-21 02:15:27 -0700 | [diff] [blame^] | 214 | void MidiManager::SetOutputPortState(uint32_t port_index, PortState state) { |
toyoshim | 5c6fe4b | 2015-02-18 23:28:09 -0800 | [diff] [blame] | 215 | base::AutoLock auto_lock(lock_); |
| 216 | DCHECK_LT(port_index, output_ports_.size()); |
| 217 | output_ports_[port_index].state = state; |
vmpstr | 0205abb | 2016-06-28 18:50:56 -0700 | [diff] [blame] | 218 | for (auto* client : clients_) |
toyoshim | 5c6fe4b | 2015-02-18 23:28:09 -0800 | [diff] [blame] | 219 | client->SetOutputPortState(port_index, state); |
| 220 | } |
| 221 | |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 222 | void MidiManager::ReceiveMidiData(uint32_t port_index, |
| 223 | const uint8_t* data, |
| 224 | size_t length, |
| 225 | double timestamp) { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 226 | base::AutoLock auto_lock(lock_); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 227 | |
vmpstr | 0205abb | 2016-06-28 18:50:56 -0700 | [diff] [blame] | 228 | for (auto* client : clients_) |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 229 | client->ReceiveMidiData(port_index, data, length, timestamp); |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 230 | } |
| 231 | |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 232 | void MidiManager::CompleteInitializationInternal(Result result) { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 233 | TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); |
toyoshim | 715385c | 2015-08-09 23:00:26 -0700 | [diff] [blame] | 234 | ReportUsage(Usage::INITIALIZED); |
| 235 | UMA_HISTOGRAM_ENUMERATION("Media.Midi.InputPorts", |
| 236 | static_cast<Sample>(input_ports_.size()), |
| 237 | kMaxUmaDevices + 1); |
| 238 | UMA_HISTOGRAM_ENUMERATION("Media.Midi.OutputPorts", |
| 239 | static_cast<Sample>(output_ports_.size()), |
| 240 | kMaxUmaDevices + 1); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 241 | |
| 242 | base::AutoLock auto_lock(lock_); |
| 243 | DCHECK(clients_.empty()); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 244 | DCHECK(!initialized_); |
| 245 | initialized_ = true; |
| 246 | result_ = result; |
| 247 | |
vmpstr | 0205abb | 2016-06-28 18:50:56 -0700 | [diff] [blame] | 248 | for (auto* client : pending_clients_) { |
toyoshim | f1b8896 | 2015-07-09 14:14:51 -0700 | [diff] [blame] | 249 | if (result_ == Result::OK) { |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 250 | AddInitialPorts(client); |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 251 | clients_.insert(client); |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 252 | } |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 253 | client->CompleteStartSession(result_); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 254 | } |
| 255 | pending_clients_.clear(); |
| 256 | } |
| 257 | |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 258 | void MidiManager::AddInitialPorts(MidiManagerClient* client) { |
| 259 | lock_.AssertAcquired(); |
| 260 | |
| 261 | for (const auto& info : input_ports_) |
| 262 | client->AddInputPort(info); |
| 263 | for (const auto& info : output_ports_) |
| 264 | client->AddOutputPort(info); |
| 265 | } |
| 266 | |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 267 | void MidiManager::ShutdownOnSessionThread() { |
| 268 | Finalize(); |
| 269 | base::AutoLock auto_lock(lock_); |
| 270 | finalized_ = true; |
| 271 | |
| 272 | // Detach all clients so that they do not call MidiManager methods any more. |
vmpstr | 0205abb | 2016-06-28 18:50:56 -0700 | [diff] [blame] | 273 | for (auto* client : clients_) |
toyoshim | 7a80966 | 2015-10-06 00:54:21 -0700 | [diff] [blame] | 274 | client->Detach(); |
| 275 | } |
| 276 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 277 | } // namespace midi |