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" |
| 9 | #include "base/message_loop/message_loop_proxy.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 { |
| 13 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 14 | MidiManager::MidiManager() |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 15 | : initialized_(false), |
| 16 | result_(MIDI_NOT_SUPPORTED) { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 17 | } |
| 18 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 19 | MidiManager::~MidiManager() { |
yukawa@chromium.org | db7ad8b | 2013-12-04 15:42:41 +0000 | [diff] [blame] | 20 | } |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 21 | |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 22 | #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ |
| 23 | !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 24 | MidiManager* MidiManager::Create() { |
| 25 | return new MidiManager; |
| 26 | } |
| 27 | #endif |
| 28 | |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 29 | void MidiManager::StartSession(MidiManagerClient* client) { |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 30 | bool session_is_ready; |
| 31 | bool session_needs_initialization = false; |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 32 | bool too_many_pending_clients_exist = false; |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 33 | |
| 34 | { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 35 | base::AutoLock auto_lock(lock_); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 36 | session_is_ready = initialized_; |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 37 | if (clients_.find(client) != clients_.end() || |
| 38 | pending_clients_.find(client) != pending_clients_.end()) { |
| 39 | // Should not happen. But just in case the renderer is compromised. |
| 40 | NOTREACHED(); |
| 41 | return; |
| 42 | } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 43 | if (!session_is_ready) { |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 44 | // Do not accept a new request if the pending client list contains too |
| 45 | // many clients. |
| 46 | too_many_pending_clients_exist = |
| 47 | pending_clients_.size() >= kMaxPendingClientCount; |
| 48 | |
| 49 | if (!too_many_pending_clients_exist) { |
| 50 | // Call StartInitialization() only for the first request. |
| 51 | session_needs_initialization = pending_clients_.empty(); |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 52 | pending_clients_.insert(client); |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 53 | } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 54 | } |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 55 | } |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 56 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 57 | // Lazily initialize the MIDI back-end. |
| 58 | if (!session_is_ready) { |
| 59 | if (session_needs_initialization) { |
| 60 | TRACE_EVENT0("midi", "MidiManager::StartInitialization"); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 61 | session_thread_runner_ = |
| 62 | base::MessageLoop::current()->message_loop_proxy(); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 63 | StartInitialization(); |
| 64 | } |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 65 | if (too_many_pending_clients_exist) { |
| 66 | // Return an error immediately if there are too many requests. |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 67 | client->CompleteStartSession(MIDI_INITIALIZATION_ERROR); |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 68 | return; |
| 69 | } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 70 | // CompleteInitialization() will be called asynchronously when platform |
| 71 | // dependent initialization is finished. |
| 72 | return; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 73 | } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 74 | |
| 75 | // Platform dependent initialization was already finished for previously |
| 76 | // initialized clients. |
| 77 | MidiResult result; |
| 78 | { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 79 | base::AutoLock auto_lock(lock_); |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 80 | if (result_ == MIDI_OK) { |
| 81 | AddInitialPorts(client); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 82 | clients_.insert(client); |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 83 | } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 84 | result = result_; |
| 85 | } |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 86 | client->CompleteStartSession(result); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 87 | } |
| 88 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 89 | void MidiManager::EndSession(MidiManagerClient* client) { |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 90 | // At this point, |client| can be in the destruction process, and calling |
| 91 | // any method of |client| is dangerous. |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 92 | base::AutoLock auto_lock(lock_); |
toyoshim@chromium.org | 1fa678a | 2014-06-13 09:40:33 +0000 | [diff] [blame] | 93 | clients_.erase(client); |
| 94 | pending_clients_.erase(client); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 97 | void MidiManager::DispatchSendMidiData(MidiManagerClient* client, |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 98 | uint32 port_index, |
| 99 | const std::vector<uint8>& data, |
| 100 | double timestamp) { |
| 101 | NOTREACHED(); |
| 102 | } |
| 103 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 104 | void MidiManager::StartInitialization() { |
| 105 | CompleteInitialization(MIDI_NOT_SUPPORTED); |
| 106 | } |
| 107 | |
| 108 | void MidiManager::CompleteInitialization(MidiResult result) { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 109 | DCHECK(session_thread_runner_.get()); |
| 110 | // It is safe to post a task to the IO thread from here because the IO thread |
| 111 | // should have stopped if the MidiManager is going to be destructed. |
| 112 | session_thread_runner_->PostTask( |
| 113 | FROM_HERE, |
| 114 | base::Bind(&MidiManager::CompleteInitializationInternal, |
| 115 | base::Unretained(this), |
| 116 | result)); |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 117 | } |
| 118 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 119 | void MidiManager::AddInputPort(const MidiPortInfo& info) { |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 120 | base::AutoLock auto_lock(lock_); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 121 | input_ports_.push_back(info); |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 122 | for (auto client : clients_) |
| 123 | client->AddInputPort(info); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 124 | } |
| 125 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 126 | void MidiManager::AddOutputPort(const MidiPortInfo& info) { |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 127 | base::AutoLock auto_lock(lock_); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 128 | output_ports_.push_back(info); |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 129 | for (auto client : clients_) |
| 130 | client->AddOutputPort(info); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 131 | } |
| 132 | |
toyoshim | 5c6fe4b | 2015-02-18 23:28:09 -0800 | [diff] [blame^] | 133 | void MidiManager::SetInputPortState(uint32 port_index, MidiPortState state) { |
| 134 | base::AutoLock auto_lock(lock_); |
| 135 | DCHECK_LT(port_index, input_ports_.size()); |
| 136 | input_ports_[port_index].state = state; |
| 137 | for (auto client : clients_) |
| 138 | client->SetInputPortState(port_index, state); |
| 139 | } |
| 140 | |
| 141 | void MidiManager::SetOutputPortState(uint32 port_index, MidiPortState state) { |
| 142 | base::AutoLock auto_lock(lock_); |
| 143 | DCHECK_LT(port_index, output_ports_.size()); |
| 144 | output_ports_[port_index].state = state; |
| 145 | for (auto client : clients_) |
| 146 | client->SetOutputPortState(port_index, state); |
| 147 | } |
| 148 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 149 | void MidiManager::ReceiveMidiData( |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame] | 150 | uint32 port_index, |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 151 | const uint8* data, |
| 152 | size_t length, |
| 153 | double timestamp) { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 154 | base::AutoLock auto_lock(lock_); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 155 | |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 156 | for (auto client : clients_) |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 157 | client->ReceiveMidiData(port_index, data, length, timestamp); |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 158 | } |
| 159 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 160 | void MidiManager::CompleteInitializationInternal(MidiResult result) { |
| 161 | TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); |
| 162 | |
| 163 | base::AutoLock auto_lock(lock_); |
| 164 | DCHECK(clients_.empty()); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 165 | DCHECK(!initialized_); |
| 166 | initialized_ = true; |
| 167 | result_ = result; |
| 168 | |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 169 | for (auto client : pending_clients_) { |
| 170 | if (result_ == MIDI_OK) { |
| 171 | AddInitialPorts(client); |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 172 | clients_.insert(client); |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 173 | } |
toyoshim | a485ff9 | 2014-10-23 00:17:59 -0700 | [diff] [blame] | 174 | client->CompleteStartSession(result_); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 175 | } |
| 176 | pending_clients_.clear(); |
| 177 | } |
| 178 | |
toyoshim | a62d674 | 2014-10-23 09:05:03 -0700 | [diff] [blame] | 179 | void MidiManager::AddInitialPorts(MidiManagerClient* client) { |
| 180 | lock_.AssertAcquired(); |
| 181 | |
| 182 | for (const auto& info : input_ports_) |
| 183 | client->AddInputPort(info); |
| 184 | for (const auto& info : output_ports_) |
| 185 | client->AddOutputPort(info); |
| 186 | } |
| 187 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 188 | } // namespace media |