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