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