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