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