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 | 2b058e8 | 2014-02-26 06:10:46 +0000 | [diff] [blame] | 8 | #include "base/debug/trace_event.h" |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
| 10 | #include "base/message_loop/message_loop_proxy.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@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 29 | void MidiManager::StartSession(MidiManagerClient* client, int client_id) { |
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_; |
| 37 | if (!session_is_ready) { |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 38 | // Do not accept a new request if the pending client list contains too |
| 39 | // many clients. |
| 40 | too_many_pending_clients_exist = |
| 41 | pending_clients_.size() >= kMaxPendingClientCount; |
| 42 | |
| 43 | if (!too_many_pending_clients_exist) { |
| 44 | // Call StartInitialization() only for the first request. |
| 45 | session_needs_initialization = pending_clients_.empty(); |
| 46 | pending_clients_.insert( |
| 47 | std::pair<int, MidiManagerClient*>(client_id, client)); |
| 48 | } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 49 | } |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 50 | } |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 51 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 52 | // Lazily initialize the MIDI back-end. |
| 53 | if (!session_is_ready) { |
| 54 | if (session_needs_initialization) { |
| 55 | TRACE_EVENT0("midi", "MidiManager::StartInitialization"); |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 56 | session_thread_runner_ = |
| 57 | base::MessageLoop::current()->message_loop_proxy(); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 58 | StartInitialization(); |
| 59 | } |
toyoshim@chromium.org | fc2002e | 2014-05-07 08:10:34 +0000 | [diff] [blame] | 60 | if (too_many_pending_clients_exist) { |
| 61 | // Return an error immediately if there are too many requests. |
| 62 | client->CompleteStartSession(client_id, MIDI_INITIALIZATION_ERROR); |
| 63 | return; |
| 64 | } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 65 | // CompleteInitialization() will be called asynchronously when platform |
| 66 | // dependent initialization is finished. |
| 67 | return; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 68 | } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 69 | |
| 70 | // Platform dependent initialization was already finished for previously |
| 71 | // initialized clients. |
| 72 | MidiResult result; |
| 73 | { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 74 | base::AutoLock auto_lock(lock_); |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 75 | if (result_ == MIDI_OK) |
| 76 | clients_.insert(client); |
| 77 | result = result_; |
| 78 | } |
| 79 | client->CompleteStartSession(client_id, result); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 80 | } |
| 81 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 82 | void MidiManager::EndSession(MidiManagerClient* client) { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 83 | base::AutoLock auto_lock(lock_); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 84 | ClientList::iterator i = clients_.find(client); |
| 85 | if (i != clients_.end()) |
| 86 | clients_.erase(i); |
| 87 | } |
| 88 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 89 | void MidiManager::DispatchSendMidiData(MidiManagerClient* client, |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 90 | uint32 port_index, |
| 91 | const std::vector<uint8>& data, |
| 92 | double timestamp) { |
| 93 | NOTREACHED(); |
| 94 | } |
| 95 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 96 | void MidiManager::StartInitialization() { |
| 97 | CompleteInitialization(MIDI_NOT_SUPPORTED); |
| 98 | } |
| 99 | |
| 100 | void MidiManager::CompleteInitialization(MidiResult result) { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 101 | DCHECK(session_thread_runner_.get()); |
| 102 | // It is safe to post a task to the IO thread from here because the IO thread |
| 103 | // should have stopped if the MidiManager is going to be destructed. |
| 104 | session_thread_runner_->PostTask( |
| 105 | FROM_HERE, |
| 106 | base::Bind(&MidiManager::CompleteInitializationInternal, |
| 107 | base::Unretained(this), |
| 108 | result)); |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 109 | } |
| 110 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 111 | void MidiManager::AddInputPort(const MidiPortInfo& info) { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 112 | input_ports_.push_back(info); |
| 113 | } |
| 114 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 115 | void MidiManager::AddOutputPort(const MidiPortInfo& info) { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 116 | output_ports_.push_back(info); |
| 117 | } |
| 118 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 119 | void MidiManager::ReceiveMidiData( |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame] | 120 | uint32 port_index, |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 121 | const uint8* data, |
| 122 | size_t length, |
| 123 | double timestamp) { |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 124 | base::AutoLock auto_lock(lock_); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 125 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 126 | for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 127 | (*i)->ReceiveMidiData(port_index, data, length, timestamp); |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 128 | } |
| 129 | |
toyoshim@chromium.org | c1e05fb | 2014-05-06 16:39:20 +0000 | [diff] [blame] | 130 | void MidiManager::CompleteInitializationInternal(MidiResult result) { |
| 131 | TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); |
| 132 | |
| 133 | base::AutoLock auto_lock(lock_); |
| 134 | DCHECK(clients_.empty()); |
| 135 | DCHECK(!pending_clients_.empty()); |
| 136 | DCHECK(!initialized_); |
| 137 | initialized_ = true; |
| 138 | result_ = result; |
| 139 | |
| 140 | for (PendingClientMap::iterator it = pending_clients_.begin(); |
| 141 | it != pending_clients_.end(); |
| 142 | ++it) { |
| 143 | if (result_ == MIDI_OK) |
| 144 | clients_.insert(it->second); |
| 145 | it->second->CompleteStartSession(it->first, result_); |
| 146 | } |
| 147 | pending_clients_.clear(); |
| 148 | } |
| 149 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 150 | } // namespace media |