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 | 2b058e8 | 2014-02-26 06:10:46 +0000 | [diff] [blame] | 7 | #include "base/debug/trace_event.h" |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 8 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 9 | namespace media { |
| 10 | |
yhirano@chromium.org | 881fec4 | 2014-02-12 08:23:48 +0000 | [diff] [blame] | 11 | #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ |
toyoshim@chromium.org | 2b058e8 | 2014-02-26 06:10:46 +0000 | [diff] [blame] | 12 | !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 13 | MidiManager* MidiManager::Create() { |
| 14 | return new MidiManager; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 15 | } |
| 16 | #endif |
| 17 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 18 | MidiManager::MidiManager() |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 19 | : initialized_(false), |
| 20 | result_(MIDI_NOT_SUPPORTED) { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 21 | } |
| 22 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 23 | MidiManager::~MidiManager() { |
yukawa@chromium.org | db7ad8b | 2013-12-04 15:42:41 +0000 | [diff] [blame] | 24 | } |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 25 | |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 26 | void MidiManager::StartSession(MidiManagerClient* client, int client_id) { |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 27 | bool session_is_ready; |
| 28 | bool session_needs_initialization = false; |
| 29 | |
| 30 | { |
| 31 | base::AutoLock auto_lock(clients_lock_); |
| 32 | session_is_ready = initialized_; |
| 33 | if (!session_is_ready) { |
| 34 | // Call StartInitialization() only for the first request. |
| 35 | session_needs_initialization = pending_clients_.empty(); |
| 36 | pending_clients_.insert( |
| 37 | std::pair<int, MidiManagerClient*>(client_id, client)); |
| 38 | } |
toyoshim@chromium.org | f77a1e6 | 2014-04-11 13:16:24 +0000 | [diff] [blame] | 39 | } |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 40 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 41 | // Lazily initialize the MIDI back-end. |
| 42 | if (!session_is_ready) { |
| 43 | if (session_needs_initialization) { |
| 44 | TRACE_EVENT0("midi", "MidiManager::StartInitialization"); |
| 45 | StartInitialization(); |
| 46 | } |
| 47 | // CompleteInitialization() will be called asynchronously when platform |
| 48 | // dependent initialization is finished. |
| 49 | return; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 50 | } |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 51 | |
| 52 | // Platform dependent initialization was already finished for previously |
| 53 | // initialized clients. |
| 54 | MidiResult result; |
| 55 | { |
| 56 | base::AutoLock auto_lock(clients_lock_); |
| 57 | if (result_ == MIDI_OK) |
| 58 | clients_.insert(client); |
| 59 | result = result_; |
| 60 | } |
| 61 | client->CompleteStartSession(client_id, result); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 62 | } |
| 63 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 64 | void MidiManager::EndSession(MidiManagerClient* client) { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 65 | base::AutoLock auto_lock(clients_lock_); |
| 66 | ClientList::iterator i = clients_.find(client); |
| 67 | if (i != clients_.end()) |
| 68 | clients_.erase(i); |
| 69 | } |
| 70 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 71 | void MidiManager::DispatchSendMidiData(MidiManagerClient* client, |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 72 | uint32 port_index, |
| 73 | const std::vector<uint8>& data, |
| 74 | double timestamp) { |
| 75 | NOTREACHED(); |
| 76 | } |
| 77 | |
toyoshim@chromium.org | 51c7f53 | 2014-05-01 17:17:32 +0000 | [diff] [blame] | 78 | void MidiManager::StartInitialization() { |
| 79 | CompleteInitialization(MIDI_NOT_SUPPORTED); |
| 80 | } |
| 81 | |
| 82 | void MidiManager::CompleteInitialization(MidiResult result) { |
| 83 | TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); |
| 84 | |
| 85 | base::AutoLock auto_lock(clients_lock_); |
| 86 | DCHECK(clients_.empty()); |
| 87 | DCHECK(!pending_clients_.empty()); |
| 88 | DCHECK(!initialized_); |
| 89 | initialized_ = true; |
| 90 | result_ = result; |
| 91 | |
| 92 | for (PendingClientMap::iterator it = pending_clients_.begin(); |
| 93 | it != pending_clients_.end(); |
| 94 | ++it) { |
| 95 | if (result_ == MIDI_OK) |
| 96 | clients_.insert(it->second); |
| 97 | it->second->CompleteStartSession(it->first, result_); |
| 98 | } |
| 99 | pending_clients_.clear(); |
yhirano@chromium.org | c6d5b7b | 2013-12-20 07:27:23 +0000 | [diff] [blame] | 100 | } |
| 101 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 102 | void MidiManager::AddInputPort(const MidiPortInfo& info) { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 103 | input_ports_.push_back(info); |
| 104 | } |
| 105 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 106 | void MidiManager::AddOutputPort(const MidiPortInfo& info) { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 107 | output_ports_.push_back(info); |
| 108 | } |
| 109 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 110 | void MidiManager::ReceiveMidiData( |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame] | 111 | uint32 port_index, |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 112 | const uint8* data, |
| 113 | size_t length, |
| 114 | double timestamp) { |
| 115 | base::AutoLock auto_lock(clients_lock_); |
| 116 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 117 | for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 118 | (*i)->ReceiveMidiData(port_index, data, length, timestamp); |
crogers@google.com | 542a43a | 2013-07-31 05:16:49 +0000 | [diff] [blame] | 119 | } |
| 120 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 121 | } // namespace media |