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